Feature/make config settings changeable during runtime (#545)
* Add logic to update config during runtime * Update ConfigModule to always use the latest config * Make ServerConfig settings re-assignable
This commit is contained in:
@@ -11,6 +11,9 @@ import ch.qos.logback.classic.Level
|
||||
import com.typesafe.config.Config
|
||||
import com.typesafe.config.ConfigFactory
|
||||
import com.typesafe.config.ConfigRenderOptions
|
||||
import com.typesafe.config.ConfigValue
|
||||
import com.typesafe.config.ConfigValueFactory
|
||||
import com.typesafe.config.parser.ConfigDocumentFactory
|
||||
import mu.KotlinLogging
|
||||
import java.io.File
|
||||
|
||||
@@ -18,15 +21,17 @@ import java.io.File
|
||||
* Manages app config.
|
||||
*/
|
||||
open class ConfigManager {
|
||||
val logger = KotlinLogging.logger {}
|
||||
private val generatedModules = mutableMapOf<Class<out ConfigModule>, ConfigModule>()
|
||||
val config by lazy { loadConfigs() }
|
||||
private val userConfigFile = File(ApplicationRootDir, "server.conf")
|
||||
private var internalConfig = loadConfigs()
|
||||
val config: Config
|
||||
get() = internalConfig
|
||||
|
||||
// Public read-only view of modules
|
||||
val loadedModules: Map<Class<out ConfigModule>, ConfigModule>
|
||||
get() = generatedModules
|
||||
|
||||
val logger = KotlinLogging.logger {}
|
||||
|
||||
/**
|
||||
* Get a config module
|
||||
*/
|
||||
@@ -54,7 +59,7 @@ open class ConfigManager {
|
||||
|
||||
// Load user config
|
||||
val userConfig =
|
||||
File(ApplicationRootDir, "server.conf").let {
|
||||
userConfigFile.let {
|
||||
ConfigFactory.parseFile(it)
|
||||
}
|
||||
|
||||
@@ -86,6 +91,20 @@ open class ConfigManager {
|
||||
registerModule(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateUserConfigFile(path: String, value: ConfigValue) {
|
||||
val userConfigDoc = ConfigDocumentFactory.parseFile(userConfigFile)
|
||||
val updatedConfigDoc = userConfigDoc.withValue(path, value)
|
||||
val newFileContent = updatedConfigDoc.render()
|
||||
userConfigFile.writeText(newFileContent)
|
||||
}
|
||||
|
||||
fun updateValue(path: String, value: Any) {
|
||||
val configValue = ConfigValueFactory.fromAnyRef(value)
|
||||
|
||||
updateUserConfigFile(path, configValue)
|
||||
internalConfig = internalConfig.withValue(path, configValue)
|
||||
}
|
||||
}
|
||||
|
||||
object GlobalConfigManager : ConfigManager()
|
||||
|
||||
@@ -15,19 +15,23 @@ import kotlin.reflect.KProperty
|
||||
* Abstract config module.
|
||||
*/
|
||||
@Suppress("UNUSED_PARAMETER")
|
||||
abstract class ConfigModule(config: Config)
|
||||
abstract class ConfigModule(getConfig: () -> Config)
|
||||
|
||||
/**
|
||||
* Abstract jvm-commandline-argument-overridable config module.
|
||||
*/
|
||||
abstract class SystemPropertyOverridableConfigModule(config: Config, moduleName: String) : ConfigModule(config) {
|
||||
val overridableConfig = SystemPropertyOverrideDelegate(config, moduleName)
|
||||
abstract class SystemPropertyOverridableConfigModule(getConfig: () -> Config, moduleName: String) : ConfigModule(getConfig) {
|
||||
val overridableConfig = SystemPropertyOverrideDelegate(getConfig, moduleName)
|
||||
}
|
||||
|
||||
/** Defines a config property that is overridable with jvm `-D` commandline arguments prefixed with [CONFIG_PREFIX] */
|
||||
class SystemPropertyOverrideDelegate(val config: Config, val moduleName: String) {
|
||||
class SystemPropertyOverrideDelegate(val getConfig: () -> Config, val moduleName: String) {
|
||||
operator fun <R> setValue(thisRef: R, property: KProperty<*>, value: Any) {
|
||||
GlobalConfigManager.updateValue("$moduleName.${property.name}", value)
|
||||
}
|
||||
|
||||
inline operator fun <R, reified T> getValue(thisRef: R, property: KProperty<*>): T {
|
||||
val configValue: T = config.getValue(thisRef, property)
|
||||
val configValue: T = getConfig().getValue(thisRef, property)
|
||||
|
||||
val combined = System.getProperty(
|
||||
"$CONFIG_PREFIX.$moduleName.${property.name}",
|
||||
|
||||
Reference in New Issue
Block a user