lc-core/src/main/scala/lc/Server.scala
Rahul Rudragoudar c10a66a23b
Remove POST support for media endpoint
Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
2021-02-18 15:24:47 +05:30

55 lines
1.4 KiB
Scala

package lc
import java.io.File
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization.{read, write}
import lc.HTTPServer._
case class Secret(token: Int)
class Server(port: Int, captcha: Captcha, dbConn: DBConn){
val server = new HTTPServer(port)
val host = server.getVirtualHost(null)
implicit val formats = DefaultFormats
host.addContext("/v1/captcha",(req, resp) => {
val body = req.getJson()
val json = parse(body)
val param = json.extract[Parameters]
val id = captcha.getChallenge(param)
resp.getHeaders().add("Content-Type","application/json")
resp.send(200, write(id))
0
},"POST")
host.addContext("/v1/media",(req, resp) => {
val params = req.getParams()
val id = Id(params.get("id"))
val image = captcha.getCaptcha(id)
resp.getHeaders().add("Content-Type","image/png")
resp.send(200, image)
0
},"GET")
host.addContext("/v1/answer",(req, resp) => {
val body = req.getJson()
val json = parse(body)
val answer = json.extract[Answer]
val result = captcha.checkAnswer(answer)
resp.getHeaders().add("Content-Type","application/json")
val responseContent = s"""{"result":"$result"}"""
resp.send(200,responseContent)
0
},"POST")
def start(): Unit = {
println("Starting server on port:" + port)
server.start()
}
}