Support to specify config file

Signed-off-by: Rahul Rudragoudar <rr83019@gmail.com>
This commit is contained in:
Rahul Rudragoudar 2021-05-02 16:57:12 +05:30
parent 1a41724fdc
commit 6f38a77c2f
No known key found for this signature in database
GPG Key ID: 0D2CE231A7287EBC
2 changed files with 32 additions and 13 deletions

View File

@ -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")

View File

@ -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
}