From 6f38a77c2f67ee299b9f7df6c8f22abf58f222eb Mon Sep 17 00:00:00 2001 From: Rahul Rudragoudar Date: Sun, 2 May 2021 16:57:12 +0530 Subject: [PATCH] Support to specify config file Signed-off-by: Rahul Rudragoudar --- src/main/scala/lc/Main.scala | 29 ++++++++++++++++++++--------- src/main/scala/lc/core/config.scala | 16 ++++++++++++---- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/main/scala/lc/Main.scala b/src/main/scala/lc/Main.scala index ba7e66a..c59f94b 100644 --- a/src/main/scala/lc/Main.scala +++ b/src/main/scala/lc/Main.scala @@ -1,25 +1,36 @@ package lc -import lc.core.CaptchaProviders +import lc.core.{CaptchaProviders, Captcha, Config} import lc.server.Server import lc.background.BackgroundTask -import lc.core.Config object LCFramework { def main(args: scala.Array[String]): Unit = { - val backgroundTask = new BackgroundTask( - throttle = Config.throttle, - timeLimit = Config.captchaExpiryTimeLimit - ) - backgroundTask.beginThread(delay = Config.threadDelay) - val server = new Server(port = Config.port) + val configFilePath = if (args.length > 0){ + args(0) + } else { + "data/config.json" + } + val config = new Config(configFilePath) + val captchaProviders = new CaptchaProviders(config = config) + val captcha = new Captcha(config = config, captchaProviders = captchaProviders) + val backgroundTask = new BackgroundTask(config = config, captcha = captcha) + backgroundTask.beginThread(delay = config.threadDelay) + val server = new Server(port = config.port, captcha = captcha) server.start() } } object MakeSamples { def main(args: scala.Array[String]): Unit = { - val samples = CaptchaProviders.generateChallengeSamples() + val configFilePath = if (args.length > 0){ + args(0) + } else { + "data/config.json" + } + val config = new Config(configFilePath) + val captchaProviders = new CaptchaProviders(config = config) + val samples = captchaProviders.generateChallengeSamples() samples.foreach { case (key, sample) => val extensionMap = Map("image/png" -> "png", "image/gif" -> "gif") diff --git a/src/main/scala/lc/core/config.scala b/src/main/scala/lc/core/config.scala index f2da049..6b10612 100644 --- a/src/main/scala/lc/core/config.scala +++ b/src/main/scala/lc/core/config.scala @@ -8,11 +8,10 @@ import java.io.{FileNotFoundException, File, PrintWriter} import java.{util => ju} import lc.misc.HelperFunctions -object Config { +class Config(configFilePath: String) { - implicit val formats: DefaultFormats.type = DefaultFormats + import Config.formats - private val configFilePath = "data/config.json" private val configString = try { val configFile = fromFile(configFilePath) @@ -22,7 +21,12 @@ object Config { } catch { case _: FileNotFoundException => { val configFileContent = getDefaultConfig() - val configFile = new PrintWriter(new File(configFilePath)) + val file = if(new File(configFilePath).isDirectory){ + new File(configFilePath.concat("/config.json")) + } else { + new File(configFilePath) + } + val configFile = new PrintWriter(file) configFile.write(configFileContent) configFile.close configFileContent @@ -94,3 +98,7 @@ object Config { } } + +object Config{ + implicit val formats: DefaultFormats.type = DefaultFormats +}