Reformat:Scalafmt

Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
Rahul Rudragoudar 2021-04-21 14:43:00 +05:30
parent 98c304ccd4
commit e38c3b680a
No known key found for this signature in database
GPG Key ID: 0D2CE231A7287EBC
3 changed files with 45 additions and 32 deletions

View File

@ -52,9 +52,9 @@ object Config {
private val random = new scala.util.Random(seed) private val random = new scala.util.Random(seed)
def getNextRandomInt(max: Int): Int = def getNextRandomInt(max: Int): Int =
random.synchronized { random.synchronized {
random.nextInt(max) random.nextInt(max)
} }
private def getDefaultConfig(): String = { private def getDefaultConfig(): String = {
val defaultConfigMap = val defaultConfigMap =

View File

@ -116,7 +116,7 @@ object Statements {
org.h2.jdbc.JdbcSQLNonTransientException: General error: "java.lang.NullPointerException"; SQL statement: 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] 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 dbConn: DBConn = new DBConn()
private val maxAttempts = 10 private val maxAttempts = 10
val tlStmts: ThreadLocal[Statements] = ThreadLocal.withInitial(() => new Statements(dbConn, maxAttempts)) val tlStmts: ThreadLocal[Statements] = ThreadLocal.withInitial(() => new Statements(dbConn, maxAttempts))

View File

@ -11,37 +11,50 @@ import scala.io.Source
import org.limium.picoserve.Server.StringResponse import org.limium.picoserve.Server.StringResponse
class Server(port: Int) { class Server(port: Int) {
val server: picoserve.Server = picoserve.Server.builder() val server: picoserve.Server = picoserve.Server
.builder()
.port(port) .port(port)
.backlog(32) .backlog(32)
.POST("/v1/captcha", (request) => { .POST(
val json = parse(request.getBodyString()) "/v1/captcha",
val param = json.extract[Parameters] (request) => {
val id = Captcha.getChallenge(param) val json = parse(request.getBodyString())
getResponse(id) val param = json.extract[Parameters]
}) val id = Captcha.getChallenge(param)
.GET("/v1/media", (request) => { getResponse(id)
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) )
}) .GET(
.POST("/v1/answer", (request) => { "/v1/media",
val json = parse(request.getBodyString()) (request) => {
val answer = json.extract[Answer] val params = request.getQueryParams()
val result = Captcha.checkAnswer(answer) val result = if (params.containsKey("id")) {
getResponse(result) val paramId = params.get("id").get(0)
}) val id = Id(paramId)
.GET("/demo/index.html", (_) => { Captcha.getCaptcha(id)
val resStream = getClass().getResourceAsStream("/index.html") } else {
val str = Source.fromInputStream(resStream).mkString Left(Error(ErrorMessageEnum.INVALID_PARAM.toString + "=> id"))
new StringResponse(200, str) }
}) 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() .build()
private def getResponse(response: Either[Error, ByteConvert]): ByteResponse = { private def getResponse(response: Either[Error, ByteConvert]): ByteResponse = {