Konfig - A Type Safe Configuration API for Kotlin
Konfig provides an extensible, type-safe API for configuration properties gathered from multiple sources — built in resources, system properties, property files, environment variables, command-line arguments, etc.
A secondary goal of Konfig is to make configuration "self explanatory”.
Misconfiguration errors are reported with the location and “true name”
of the badly configured property. E.g. a program may look up a key
defined as Key("http.port", intType)
. At runtime, it will be parsed
from an environment variable named HTTP_PORT
. So the error message
reports the name of the environment variable, so that the user can
easily find and fix the error.
Configuration can be inspected and listed. For example, it can be exposed by HTTP to a network management system to help site reliability engineers understand the current configuration of a running application.
Getting Started
To get started, add com.natpryce:konfig:<version>
as a dependency, import com.natpryce.konfig.*
and then:
-
Define typed property keys
object server : PropertyGroup() { val port by intType val host by stringType }
-
Build a Configuration object that loads properties:
val config = systemProperties() overriding EnvironmentVariables() overriding ConfigurationProperties.fromFile(File("/etc/myservice.properties")) overriding ConfigurationProperties.fromResource("defaults.properties")
-
Define some properties. For example, in
defaults.properties
:server.port=8080 server.host=0.0.0.0
-
Look up properties by key. They are returned as typed values, not strings, and so can be used directly:
val server = Server(config[server.port], config[server.host]) server.start()
Konfig can load properties from:
- Java property files and resources
- Java system properties
- Environment variables
- Hard-coded maps (with convenient syntax)
- Command-line parameters (with long and short option syntax)
Konfig can easily be extended with new property types and sources of configuration data.
Konfig can report where configuration properties are searched for and where they were found.