@@ -8,7 +8,7 @@ import io.vertx.config.ConfigRetrieverOptions
88import io.vertx.config.ConfigStoreOptions
99import io.vertx.core.Vertx
1010import io.vertx.core.json.JsonObject
11- import io.vertx.kotlin.coroutines.await
11+ import io.vertx.kotlin.coroutines.coAwait
1212import org.slf4j.Logger
1313import org.springframework.beans.factory.config.ConfigurableBeanFactory
1414import org.springframework.context.annotation.AnnotationConfigApplicationContext
@@ -25,33 +25,31 @@ class ConfigManager(
2525 private val logger : Logger ,
2626 applicationContext : AnnotationConfigApplicationContext
2727) {
28-
2928 companion object {
30- private const val CONFIG_VERSION = 2
31-
32- private val DEFAULT_CONFIG by lazy {
33- JsonObject (
34- mapOf (
35- " config-version" to CONFIG_VERSION ,
36- " plugins" to JsonObject (),
37- " router" to mapOf (
38- " api-prefix" to " /api"
39- ),
40- " server" to mapOf (
41- " host" to " 0.0.0.0" ,
42- " port" to 8088
43- )
44- )
45- )
46- }
47-
4829 fun JsonObject.putAll (jsonObject : Map <String , Any >) {
4930 jsonObject.forEach {
5031 this .put(it.key, it.value)
5132 }
5233 }
5334 }
5435
36+ private fun getDefaultConfig (): JsonObject {
37+ val latestVersion = migrations.maxByOrNull { it.to }?.to ? : 1
38+
39+ return JsonObject (
40+ mapOf (
41+ " config-version" to latestVersion,
42+ " router" to mapOf (
43+ " api-prefix" to " /api"
44+ ),
45+ " server" to mapOf (
46+ " host" to " 0.0.0.0" ,
47+ " port" to 8088
48+ )
49+ )
50+ )
51+ }
52+
5553 fun saveConfig (config : JsonObject = this.config) {
5654 val renderOptions = ConfigRenderOptions
5755 .defaults()
@@ -72,19 +70,21 @@ class ConfigManager(
7270 fun getConfig () = config
7371
7472 internal suspend fun init () {
73+ val defaultConfig = getDefaultConfig()
74+
7575 if (! configFile.exists()) {
76- saveConfig(DEFAULT_CONFIG )
76+ saveConfig(defaultConfig )
7777 }
7878
7979 val configValues: Map <String , Any >
8080
8181 try {
82- configValues = configRetriever.config.await ().map
82+ configValues = configRetriever.config.coAwait ().map
8383 } catch (e: Exception ) {
8484 logger.error(" Error occurred while loading config file! Error: $e " )
8585 logger.info(" Using default config!" )
8686
87- config.putAll(DEFAULT_CONFIG .map)
87+ config.putAll(defaultConfig .map)
8888
8989 return
9090 }
0 commit comments