Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make config parameters optional #122

Merged
merged 11 commits into from
Jan 9, 2022
22 changes: 12 additions & 10 deletions src/main/scala/lc/core/config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import java.io.{FileNotFoundException, File, PrintWriter}
import java.{util => ju}
import lc.misc.HelperFunctions


class Config(configFilePath: String) {

import Config.formats
Expand Down Expand Up @@ -41,16 +42,17 @@ class Config(configFilePath: String) {
}

private val configJson = parse(configString)
private val configFields: ConfigField = configJson.extract[ConfigField]

val port: Int = (configJson \ AttributesEnum.PORT.toString).extract[Int]
val address: String = (configJson \ AttributesEnum.ADDRESS.toString).extract[String]
val throttle: Int = (configJson \ AttributesEnum.THROTTLE.toString).extract[Int]
val seed: Int = (configJson \ AttributesEnum.RANDOM_SEED.toString).extract[Int]
val captchaExpiryTimeLimit: Int = (configJson \ AttributesEnum.CAPTCHA_EXPIRY_TIME_LIMIT.toString).extract[Int]
val threadDelay: Int = (configJson \ AttributesEnum.THREAD_DELAY.toString).extract[Int]
val playgroundEnabled: Boolean = (configJson \ AttributesEnum.PLAYGROUND_ENABLED.toString).extract[Boolean]
val corsHeader: String = (configJson \ AttributesEnum.CORS_HEADER.toString).extract[String]
val maxAttempts: Int = (configJson \ AttributesEnum.MAX_ATTEMPTS.toString).extract[Int]
val port: Int = configFields.portInt.getOrElse(8888)
val address: String = configFields.address.getOrElse("0.0.0.0")
val throttle: Int = configFields.throttleInt.getOrElse(1000)
val seed: Int = configFields.seedInt.getOrElse(375264328)
val captchaExpiryTimeLimit: Int = configFields.captchaExpiryTimeLimitInt.getOrElse(5)
val threadDelay: Int = configFields.threadDelayInt.getOrElse(2)
val playgroundEnabled: Boolean = configFields.playgroundEnabledBool.getOrElse(true)
val corsHeader: String = configFields.corsHeader.getOrElse("")
val maxAttempts: Int = configFields.maxAttemptsInt.getOrElse(10)

private val captchaConfigJson = (configJson \ "captchas")
val captchaConfigTransform: JValue = captchaConfigJson transformField { case JField("config", JObject(config)) =>
Expand All @@ -71,7 +73,7 @@ class Config(configFilePath: String) {
(AttributesEnum.CAPTCHA_EXPIRY_TIME_LIMIT.toString -> 5) ~
(AttributesEnum.THROTTLE.toString -> 1000) ~
(AttributesEnum.THREAD_DELAY.toString -> 2) ~
(AttributesEnum.PLAYGROUND_ENABLED.toString -> true) ~
(AttributesEnum.PLAYGROUND_ENABLED.toString -> "true") ~
rr83019 marked this conversation as resolved.
Show resolved Hide resolved
(AttributesEnum.CORS_HEADER.toString -> "") ~
(AttributesEnum.MAX_ATTEMPTS.toString -> 10) ~
("captchas" -> List(
Expand Down
21 changes: 21 additions & 0 deletions src/main/scala/lc/core/models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,24 @@ case class CaptchaConfig(
allowedInputType: List[String],
config: String
)
case class ConfigField(
port: Option[Integer],
address: Option[String],
throttle: Option[Integer],
seed: Option[Integer],
captchaExpiryTimeLimit: Option[Integer],
threadDelay: Option[Integer],
playgroundEnabled: Option[java.lang.Boolean],
corsHeader: Option[String],
maxAttempts: Option[Integer]
){
lazy val portInt: Option[Int] = mapInt(port)
lazy val throttleInt: Option[Int] = mapInt(throttle)
lazy val seedInt: Option[Int] = mapInt(seed)
lazy val captchaExpiryTimeLimitInt: Option[Int] = mapInt(captchaExpiryTimeLimit)
lazy val threadDelayInt: Option[Int] = mapInt(threadDelay)
lazy val maxAttemptsInt: Option[Int] = mapInt(maxAttempts)
lazy val playgroundEnabledBool: Option[Boolean] = playgroundEnabled.map(_ || true)

private val mapInt = (x: Option[Integer]) => x.map(_ + 0)
rr83019 marked this conversation as resolved.
Show resolved Hide resolved
}