diff --git a/src/main/scala/lc/core/config.scala b/src/main/scala/lc/core/config.scala index 6e57a89..367104a 100644 --- a/src/main/scala/lc/core/config.scala +++ b/src/main/scala/lc/core/config.scala @@ -52,9 +52,9 @@ object Config { private val random = new scala.util.Random(seed) def getNextRandomInt(max: Int): Int = - random.synchronized { - random.nextInt(max) - } + random.synchronized { + random.nextInt(max) + } private def getDefaultConfig(): String = { val defaultConfigMap = diff --git a/src/main/scala/lc/database/statements.scala b/src/main/scala/lc/database/statements.scala index fdbd637..2d3d276 100644 --- a/src/main/scala/lc/database/statements.scala +++ b/src/main/scala/lc/database/statements.scala @@ -116,7 +116,7 @@ object Statements { org.h2.jdbc.JdbcSQLNonTransientException: General error: "java.lang.NullPointerException"; SQL statement: SELECT image FROM challenge c, mapId m WHERE c.token=m.token AND m.uuid = ? [50000-200] ``` - */ + */ private val dbConn: DBConn = new DBConn() private val maxAttempts = 10 val tlStmts: ThreadLocal[Statements] = ThreadLocal.withInitial(() => new Statements(dbConn, maxAttempts)) diff --git a/src/main/scala/lc/server/Server.scala b/src/main/scala/lc/server/Server.scala index efc3d33..ffb3d62 100644 --- a/src/main/scala/lc/server/Server.scala +++ b/src/main/scala/lc/server/Server.scala @@ -11,37 +11,50 @@ import scala.io.Source import org.limium.picoserve.Server.StringResponse class Server(port: Int) { - val server: picoserve.Server = picoserve.Server.builder() + val server: picoserve.Server = picoserve.Server + .builder() .port(port) .backlog(32) - .POST("/v1/captcha", (request) => { - val json = parse(request.getBodyString()) - val param = json.extract[Parameters] - val id = Captcha.getChallenge(param) - getResponse(id) - }) - .GET("/v1/media", (request) => { - val params = request.getQueryParams() - val result = if (params.containsKey("id")) { - val paramId = params.get("id").get(0) - val id = Id(paramId) - Captcha.getCaptcha(id) - } else { - Left(Error(ErrorMessageEnum.INVALID_PARAM.toString + "=> id")) + .POST( + "/v1/captcha", + (request) => { + val json = parse(request.getBodyString()) + val param = json.extract[Parameters] + val id = Captcha.getChallenge(param) + getResponse(id) } - getResponse(result) - }) - .POST("/v1/answer", (request) => { - val json = parse(request.getBodyString()) - val answer = json.extract[Answer] - val result = Captcha.checkAnswer(answer) - getResponse(result) - }) - .GET("/demo/index.html", (_) => { - val resStream = getClass().getResourceAsStream("/index.html") - val str = Source.fromInputStream(resStream).mkString - new StringResponse(200, str) - }) + ) + .GET( + "/v1/media", + (request) => { + val params = request.getQueryParams() + val result = if (params.containsKey("id")) { + val paramId = params.get("id").get(0) + val id = Id(paramId) + Captcha.getCaptcha(id) + } else { + Left(Error(ErrorMessageEnum.INVALID_PARAM.toString + "=> id")) + } + getResponse(result) + } + ) + .POST( + "/v1/answer", + (request) => { + val json = parse(request.getBodyString()) + val answer = json.extract[Answer] + val result = Captcha.checkAnswer(answer) + getResponse(result) + } + ) + .GET( + "/demo/index.html", + (_) => { + val resStream = getClass().getResourceAsStream("/index.html") + val str = Source.fromInputStream(resStream).mkString + new StringResponse(200, str) + } + ) .build() private def getResponse(response: Either[Error, ByteConvert]): ByteResponse = {