From 8e3950564411c26983a33dd2ea1993876c59bfbf Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Sat, 8 Jan 2022 01:36:50 +0530 Subject: [PATCH 1/4] Make config parameters optional with defaults Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/core/config.scala | 31 ++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/scala/lc/core/config.scala b/src/main/scala/lc/core/config.scala index cee03a4..68fe01a 100644 --- a/src/main/scala/lc/core/config.scala +++ b/src/main/scala/lc/core/config.scala @@ -42,15 +42,15 @@ class Config(configFilePath: String) { private val configJson = parse(configString) - 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 = getOptionalConfigParam(AttributesEnum.PORT.toString, "8888").toInt + val address: String = getOptionalConfigParam(AttributesEnum.ADDRESS.toString, "0.0.0.0") + val throttle: Int = getOptionalConfigParam(AttributesEnum.THROTTLE.toString, "1000").toInt + val seed: Int = getOptionalConfigParam(AttributesEnum.RANDOM_SEED.toString, "375264328").toInt + val captchaExpiryTimeLimit: Int = getOptionalConfigParam(AttributesEnum.CAPTCHA_EXPIRY_TIME_LIMIT.toString, "5").toInt + val threadDelay: Int = getOptionalConfigParam(AttributesEnum.THREAD_DELAY.toString, "2").toInt + val playgroundEnabled: Boolean = getOptionalConfigParam(AttributesEnum.PLAYGROUND_ENABLED.toString, "true").toBoolean + val corsHeader: String = getOptionalConfigParam(AttributesEnum.CORS_HEADER.toString, "") + val maxAttempts: Int = getOptionalConfigParam(AttributesEnum.MAX_ATTEMPTS.toString, "10").toInt private val captchaConfigJson = (configJson \ "captchas") val captchaConfigTransform: JValue = captchaConfigJson transformField { case JField("config", JObject(config)) => @@ -63,6 +63,19 @@ class Config(configFilePath: String) { HelperFunctions.setSeed(seed) + private def getOptionalConfigParam(paramName: String, defaultValue: String): String = { + val param: Option[(String, JValue)] = (configJson findField({ + case JField(`paramName`, _) => true + case _ => false + })) + + if(param.isDefined){ + param.get._2.extract[String] + } else { + defaultValue + } + } + private def getDefaultConfig(): String = { val defaultConfigMap = (AttributesEnum.RANDOM_SEED.toString -> new ju.Random().nextInt()) ~ From e1505a7573f3d22b351d2ea5d19d573be9b51d09 Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Sat, 8 Jan 2022 01:37:57 +0530 Subject: [PATCH 2/4] Convert playgroundEnabled: bool to string Signed-off-by: Rahul Rudragoudar --- tests/debug-config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/debug-config.json b/tests/debug-config.json index c0d91b4..5704ba4 100644 --- a/tests/debug-config.json +++ b/tests/debug-config.json @@ -5,7 +5,7 @@ "captchaExpiryTimeLimit" : 5, "throttle" : 10, "threadDelay" : 2, - "playgroundEnabled" : false, + "playgroundEnabled" : "false", "corsHeader" : "*", "maxAttempts" : 20, "captchas" : [ { From fabd5dd7ec209dbee288b5fbb93feb1b5b78a929 Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Sat, 8 Jan 2022 02:15:34 +0530 Subject: [PATCH 3/4] Convert playgroundEnabled: bool to string Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/core/config.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/lc/core/config.scala b/src/main/scala/lc/core/config.scala index 68fe01a..745af80 100644 --- a/src/main/scala/lc/core/config.scala +++ b/src/main/scala/lc/core/config.scala @@ -84,7 +84,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") ~ (AttributesEnum.CORS_HEADER.toString -> "") ~ (AttributesEnum.MAX_ATTEMPTS.toString -> 10) ~ ("captchas" -> List( From 8e66f3fa8e5de5f660a74e66b31c2b0f565ae74c Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Sat, 8 Jan 2022 02:15:34 +0530 Subject: [PATCH 4/4] WIP: Add config case class to avoid type change Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/core/config.scala | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/scala/lc/core/config.scala b/src/main/scala/lc/core/config.scala index 68fe01a..738b7a3 100644 --- a/src/main/scala/lc/core/config.scala +++ b/src/main/scala/lc/core/config.scala @@ -11,6 +11,18 @@ import java.io.{FileNotFoundException, File, PrintWriter} import java.{util => ju} import lc.misc.HelperFunctions +case class ConfigField( + port: Option[Int], + address: Option[String], + throttle: Option[Int], + seed: Option[Int], + captchaExpiryTimeLimit: Option[Int], + threadDelay: Option[Int], + playgroundEnabled: Option[String], + corsHeader: Option[String], + maxAttempts: Option[Int] + ) + class Config(configFilePath: String) { import Config.formats @@ -42,6 +54,14 @@ class Config(configFilePath: String) { private val configJson = parse(configString) + val fields = configJson.extract[ConfigField] + + val port1: Int = fields.port.getOrElse(8888) + val address1: String = fields.address.getOrElse("0.0.0.0") + + println(port1) + println(address1) + val port: Int = getOptionalConfigParam(AttributesEnum.PORT.toString, "8888").toInt val address: String = getOptionalConfigParam(AttributesEnum.ADDRESS.toString, "0.0.0.0") val throttle: Int = getOptionalConfigParam(AttributesEnum.THROTTLE.toString, "1000").toInt @@ -84,7 +104,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") ~ (AttributesEnum.CORS_HEADER.toString -> "") ~ (AttributesEnum.MAX_ATTEMPTS.toString -> 10) ~ ("captchas" -> List(