Skip to content

Commit

Permalink
use more comprehensive config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
rtc11 committed Apr 22, 2022
1 parent 10d6fcf commit 17040d9
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions ktor-utils/main/no/nav/aap/ktor/config/Config.kt
Original file line number Diff line number Diff line change
@@ -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 <reified T : Any> Application.loadConfig(vararg resources: String = arrayOf("/application.yml")): T =
// ConfigLoader.builder()
// .addFileExtensionMapping("yml", YamlParser())
// .addKtorConfig(environment.config)
// .build()
// .loadConfigOrThrow(*resources)
inline fun <reified T : Any> Application.loadConfig(vararg resources: String = arrayOf("/application.yml")): T =
ConfigLoader.builder()
.addFileExtensionMapping("yml", YamlParser())
.addKtorConfig(environment.config)
.build()
.loadConfigOrThrow(*resources)

inline fun <reified T : Any> loadConfig(resource: String = "/application.yml"): T =
ConfigLoader.builder()
Expand All @@ -21,15 +24,15 @@ inline fun <reified T : Any> 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<String, String>
// }
//
// addPropertySource(MapPropertySource(map))
// }
//}
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<String, String>
}

addPropertySource(MapPropertySource(map))
}
}

0 comments on commit 17040d9

Please sign in to comment.