moved configuration validation to a separate function. Declared an Application.configureApplication extension to validate the configs at the start.
This commit is contained in:
parent
46df0377ee
commit
d3ab9c4766
@ -2,13 +2,14 @@ package net.ipksindia
|
|||||||
|
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
import io.ktor.server.netty.*
|
import io.ktor.server.netty.*
|
||||||
import net.ipksindia.config.AppConfig
|
import net.ipksindia.config.configureApplication
|
||||||
import net.ipksindia.plugins.*
|
import net.ipksindia.plugins.configureRouting
|
||||||
|
import net.ipksindia.plugins.configureSerialization
|
||||||
|
|
||||||
fun main(args: Array<String>):Unit = EngineMain.main(args)
|
fun main(args: Array<String>):Unit = EngineMain.main(args)
|
||||||
|
|
||||||
fun Application.module() {
|
fun Application.module() {
|
||||||
AppConfig.validateConfig()
|
configureApplication()
|
||||||
configureSerialization()
|
configureSerialization()
|
||||||
configureRouting()
|
configureRouting()
|
||||||
}
|
}
|
||||||
|
@ -1,54 +1,19 @@
|
|||||||
package net.ipksindia.config
|
package net.ipksindia.config
|
||||||
|
|
||||||
|
import com.typesafe.config.Config
|
||||||
import com.typesafe.config.ConfigFactory
|
import com.typesafe.config.ConfigFactory
|
||||||
|
import net.ipksindia.config.AppConfig.config
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import kotlin.system.exitProcess
|
import kotlin.system.exitProcess
|
||||||
|
|
||||||
object AppConfig {
|
object AppConfig {
|
||||||
|
|
||||||
private val logger = LoggerFactory.getLogger(AppConfig::class.java)
|
|
||||||
|
|
||||||
private val environment = ConfigFactory.load().getString("ktor.environment")
|
private val environment = ConfigFactory.load().getString("ktor.environment")
|
||||||
private val config = when (environment) {
|
val config: Config = when (environment) {
|
||||||
"prod" -> ConfigFactory.load("application-prod.conf")
|
"prod" -> ConfigFactory.load("application-prod.conf")
|
||||||
else -> ConfigFactory.load("application-dev.conf")
|
else -> ConfigFactory.load("application-dev.conf")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun validateConfig() {
|
|
||||||
val requiredKeys = listOf(
|
|
||||||
"database.host",
|
|
||||||
"database.port",
|
|
||||||
"database.name",
|
|
||||||
"database.user",
|
|
||||||
"database.password",
|
|
||||||
"bank.server.protocol",
|
|
||||||
"bank.server.host",
|
|
||||||
"bank.server.port",
|
|
||||||
"bank.server.rootRoute",
|
|
||||||
"bank.server.transactionRoute",
|
|
||||||
"bank.codes"
|
|
||||||
)
|
|
||||||
|
|
||||||
val missingKeys = requiredKeys.filterNot { config.hasPath(it) }
|
|
||||||
|
|
||||||
if (missingKeys.isNotEmpty()) {
|
|
||||||
logger.error("Missing configuration keys: {}", missingKeys.toString())
|
|
||||||
exitProcess(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
val dbPort = config.getInt("database.port")
|
|
||||||
val remoteServerPort = config.getInt("bank.server.port")
|
|
||||||
if (dbPort !in 1..65535) {
|
|
||||||
logger.error("Invalid database port: {}", dbPort)
|
|
||||||
exitProcess(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (remoteServerPort !in 1..65535) {
|
|
||||||
logger.error("Invalid remote server port: {}", remoteServerPort)
|
|
||||||
exitProcess(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
val databaseConfig by lazy { DatabaseConfig(
|
val databaseConfig by lazy { DatabaseConfig(
|
||||||
config.getString("database.host"),
|
config.getString("database.host"),
|
||||||
config.getInt("database.port"),
|
config.getInt("database.port"),
|
||||||
@ -88,4 +53,40 @@ data class DatabaseConfig(
|
|||||||
val password: String
|
val password: String
|
||||||
) {
|
) {
|
||||||
val url = "jdbc:oracle:thin:@$host:$port:$name"
|
val url = "jdbc:oracle:thin:@$host:$port:$name"
|
||||||
|
}
|
||||||
|
|
||||||
|
fun validateAppConfigs() {
|
||||||
|
val logger = LoggerFactory.getLogger(AppConfig::class.java)
|
||||||
|
val requiredKeys = listOf(
|
||||||
|
"database.host",
|
||||||
|
"database.port",
|
||||||
|
"database.name",
|
||||||
|
"database.user",
|
||||||
|
"database.password",
|
||||||
|
"bank.server.protocol",
|
||||||
|
"bank.server.host",
|
||||||
|
"bank.server.port",
|
||||||
|
"bank.server.rootRoute",
|
||||||
|
"bank.server.transactionRoute",
|
||||||
|
"bank.codes"
|
||||||
|
)
|
||||||
|
|
||||||
|
val missingKeys = requiredKeys.filterNot { config.hasPath(it) }
|
||||||
|
|
||||||
|
if (missingKeys.isNotEmpty()) {
|
||||||
|
logger.error("Missing configuration keys: {}", missingKeys.toString())
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
val dbPort = config.getInt("database.port")
|
||||||
|
val remoteServerPort = config.getInt("bank.server.port")
|
||||||
|
if (dbPort !in 1..65535) {
|
||||||
|
logger.error("Invalid database port: {}", dbPort)
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (remoteServerPort !in 1..65535) {
|
||||||
|
logger.error("Invalid remote server port: {}", remoteServerPort)
|
||||||
|
exitProcess(1)
|
||||||
|
}
|
||||||
}
|
}
|
8
src/main/kotlin/net/ipksindia/config/Configuration.kt
Normal file
8
src/main/kotlin/net/ipksindia/config/Configuration.kt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package net.ipksindia.config
|
||||||
|
|
||||||
|
import io.ktor.server.application.*
|
||||||
|
|
||||||
|
|
||||||
|
fun Application.configureApplication() {
|
||||||
|
validateAppConfigs()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user