From 17040d9b5342fff5e1e5ec7d5c3aadd217aa0865 Mon Sep 17 00:00:00 2001 From: Robin Date: Fri, 22 Apr 2022 11:00:44 +0200 Subject: [PATCH] use more comprehensive config loading --- .../main/no/nav/aap/ktor/config/Config.kt | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/ktor-utils/main/no/nav/aap/ktor/config/Config.kt b/ktor-utils/main/no/nav/aap/ktor/config/Config.kt index 8abc297..e636cf1 100644 --- a/ktor-utils/main/no/nav/aap/ktor/config/Config.kt +++ b/ktor-utils/main/no/nav/aap/ktor/config/Config.kt @@ -1,15 +1,18 @@ package no.nav.aap.ktor.config import com.sksamuel.hoplite.ConfigLoader +import com.sksamuel.hoplite.ConfigLoaderBuilder +import com.sksamuel.hoplite.MapPropertySource import com.sksamuel.hoplite.yaml.YamlParser +import io.ktor.server.application.* +import io.ktor.server.config.* - -//inline fun Application.loadConfig(vararg resources: String = arrayOf("/application.yml")): T = -// ConfigLoader.builder() -// .addFileExtensionMapping("yml", YamlParser()) -// .addKtorConfig(environment.config) -// .build() -// .loadConfigOrThrow(*resources) +inline fun Application.loadConfig(vararg resources: String = arrayOf("/application.yml")): T = + ConfigLoader.builder() + .addFileExtensionMapping("yml", YamlParser()) + .addKtorConfig(environment.config) + .build() + .loadConfigOrThrow(*resources) inline fun loadConfig(resource: String = "/application.yml"): T = ConfigLoader.builder() @@ -21,15 +24,15 @@ inline fun loadConfig(resource: String = "/application.yml"): * Add Ktors MapApplicationConfig as PropertySource, * this allows the MapApplicationConfig to override config values in tests */ -//fun ConfigLoaderBuilder.addKtorConfig(config: ApplicationConfig) = apply { -// if (config is MapApplicationConfig) { -// // get access to the protected property 'map' -// @Suppress("UNCHECKED_CAST") -// val map = config.javaClass.getDeclaredField("map").let { -// it.isAccessible = true -// it.get(config) as Map -// } -// -// addPropertySource(MapPropertySource(map)) -// } -//} \ No newline at end of file +fun ConfigLoaderBuilder.addKtorConfig(config: ApplicationConfig) = apply { + if (config is MapApplicationConfig) { + // get access to the protected property 'map' + @Suppress("UNCHECKED_CAST") + val map = config.javaClass.getDeclaredField("map").let { + it.isAccessible = true + it.get(config) as Map + } + + addPropertySource(MapPropertySource(map)) + } +} \ No newline at end of file