renamed Captcha to CaptchaManager

Signed-off-by: hrj <harshad.rj@gmail.com>
This commit is contained in:
hrj 2022-02-24 20:11:00 +05:30
parent 497b88f0d3
commit 52b071d680
4 changed files with 13 additions and 13 deletions

View File

@ -1,6 +1,6 @@
package lc
import lc.core.{CaptchaProviders, Captcha, Config}
import lc.core.{CaptchaProviders, CaptchaManager, Config}
import lc.server.Server
import lc.background.BackgroundTask
import lc.database.Statements
@ -15,13 +15,13 @@ object LCFramework {
val config = new Config(configFilePath)
Statements.maxAttempts = config.maxAttempts
val captchaProviders = new CaptchaProviders(config = config)
val captcha = new Captcha(config = config, captchaProviders = captchaProviders)
val backgroundTask = new BackgroundTask(config = config, captcha = captcha)
val captchaManager = new CaptchaManager(config = config, captchaProviders = captchaProviders)
val backgroundTask = new BackgroundTask(config = config, captchaManager = captchaManager)
backgroundTask.beginThread(delay = config.threadDelay)
val server = new Server(
address = config.address,
port = config.port,
captcha = captcha,
captchaManager = captchaManager,
playgroundEnabled = config.playgroundEnabled,
corsHeader = config.corsHeader
)

View File

@ -2,11 +2,11 @@ package lc.background
import lc.database.Statements
import java.util.concurrent.{ScheduledThreadPoolExecutor, TimeUnit}
import lc.core.{Captcha, Config}
import lc.core.{CaptchaManager, Config}
import lc.core.{Parameters, Size}
import lc.misc.HelperFunctions
class BackgroundTask(config: Config, captcha: Captcha) {
class BackgroundTask(config: Config, captchaManager: CaptchaManager) {
private val task = new Runnable {
def run(): Unit = {
@ -28,7 +28,7 @@ class BackgroundTask(config: Config, captcha: Captcha) {
val throttle = (config.throttle * 1.1).toInt - imageNum
for (i <- 0 until throttle) {
captcha.generateChallenge(getRandomParam())
captchaManager.generateChallenge(getRandomParam())
}
} catch { case exception: Exception => println(exception) }
}

View File

@ -6,7 +6,7 @@ import java.io.ByteArrayInputStream
import java.sql.{Blob, ResultSet}
import java.util.UUID
class Captcha(config: Config, captchaProviders: CaptchaProviders) {
class CaptchaManager(config: Config, captchaProviders: CaptchaProviders) {
def getCaptcha(id: Id): Either[Error, Image] = {
val blob = getImage(id.id)

View File

@ -2,7 +2,7 @@ package lc.server
import org.json4s.jackson.JsonMethods.parse
import org.json4s.jvalue2extractable
import lc.core.Captcha
import lc.core.CaptchaManager
import lc.core.ErrorMessageEnum
import lc.core.{Answer, ByteConvert, Error, Id, Parameters}
import lc.core.Config.formats
@ -13,7 +13,7 @@ import java.net.InetSocketAddress
import java.util
import scala.jdk.CollectionConverters._
class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Boolean, corsHeader: String) {
class Server(address: String, port: Int, captchaManager: CaptchaManager, playgroundEnabled: Boolean, corsHeader: String) {
var headerMap: util.Map[String, util.List[String]] = _
if (corsHeader.nonEmpty) {
headerMap = Map("Access-Control-Allow-Origin" -> List(corsHeader).asJava).asJava
@ -27,7 +27,7 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo
(request) => {
val json = parse(request.getBodyString())
val param = json.extract[Parameters]
val id = captcha.getChallenge(param)
val id = captchaManager.getChallenge(param)
getResponse(id, headerMap)
}
)
@ -38,7 +38,7 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo
val result = if (params.containsKey("id")) {
val paramId = params.get("id").get(0)
val id = Id(paramId)
captcha.getCaptcha(id)
captchaManager.getCaptcha(id)
} else {
Left(Error(ErrorMessageEnum.INVALID_PARAM.toString + "=> id"))
}
@ -50,7 +50,7 @@ class Server(address: String, port: Int, captcha: Captcha, playgroundEnabled: Bo
(request) => {
val json = parse(request.getBodyString())
val answer = json.extract[Answer]
val result = captcha.checkAnswer(answer)
val result = captchaManager.checkAnswer(answer)
getResponse(result, headerMap)
}
)