All Projects → npryce → Konfig

npryce / Konfig

Licence: apache-2.0
Simple config properties API for Kotlin

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Konfig

Conf
Go package for loading program configuration from multiple sources.
Stars: ✭ 70 (-71.89%)
Mutual labels:  environment-variables, configuration, command-line-parser
Fsconfig
FsConfig is a F# library for reading configuration data from environment variables and AppSettings with type safety.
Stars: ✭ 108 (-56.63%)
Mutual labels:  environment-variables, configuration
Startup
🔧 R package: startup - Friendly R Startup Configuration
Stars: ✭ 107 (-57.03%)
Mutual labels:  environment-variables, configuration
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+644.98%)
Mutual labels:  environment-variables, configuration
Konf
A type-safe cascading configuration library for Kotlin/Java/Android, supporting most configuration formats
Stars: ✭ 225 (-9.64%)
Mutual labels:  properties, configuration
React Env
Runtime environment variables for react apps.
Stars: ✭ 90 (-63.86%)
Mutual labels:  environment-variables, configuration
Config
A lightweight yet powerful config package for Go projects
Stars: ✭ 126 (-49.4%)
Mutual labels:  environment-variables, configuration
Intellij Jvm Options Explained
Common JVM options used with Intellij and what they do
Stars: ✭ 636 (+155.42%)
Mutual labels:  jvm, configuration
Phpdotenv
Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
Stars: ✭ 11,648 (+4577.91%)
Mutual labels:  configuration, environment-variables
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+769.08%)
Mutual labels:  environment-variables, configuration
Aconfig
Simple, useful and opinionated config loader.
Stars: ✭ 187 (-24.9%)
Mutual labels:  environment-variables, configuration
Confucius
A lightweight Java configuration library
Stars: ✭ 51 (-79.52%)
Mutual labels:  properties, configuration
Properties
This library provides convinient way to work with properties. It can handle property-files on hard drive, in classpath or get values from system properties
Stars: ✭ 49 (-80.32%)
Mutual labels:  environment-variables, properties
Andhow
Strongly typed, validated, easy to use Java configuration
Stars: ✭ 17 (-93.17%)
Mutual labels:  configuration, command-line-parser
Environ Config
Python Application Configuration With Environment Variables
Stars: ✭ 210 (-15.66%)
Mutual labels:  environment-variables, configuration
Dynaconf
Configuration Management for Python ⚙
Stars: ✭ 2,082 (+736.14%)
Mutual labels:  environment-variables, configuration
Dotenv Kotlin
🗝️ Dotenv is a module that loads environment variables from a .env file
Stars: ✭ 326 (+30.92%)
Mutual labels:  environment-variables, jvm
Environs
simplified environment variable parsing
Stars: ✭ 631 (+153.41%)
Mutual labels:  environment-variables, configuration
Fig
A minimalist Go configuration library
Stars: ✭ 142 (-42.97%)
Mutual labels:  environment-variables, configuration
Config
🛠 A configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP
Stars: ✭ 203 (-18.47%)
Mutual labels:  environment-variables, configuration

Konfig - A Type Safe Configuration API for Kotlin

Kotlin Build Status Maven Central

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:

  1. Define typed property keys

    val server_port = Key("server.port", intType)
    val server_host = Key("server.host", stringType)
    
  2. Build a Configuration object that loads properties:

    val config = systemProperties() overriding
                 EnvironmentVariables() overriding
                 ConfigurationProperties.fromFile(File("/etc/myservice.properties")) overriding
                 ConfigurationProperties.fromResource("defaults.properties")
    
  3. Define some properties. For example, in defaults.properties:

    server.port=8080
    server.host=0.0.0.0
    
  4. 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.

Naming of Properties

Konfig's Configuration objects expect property names to follow Java property name conventions: dots to represent hierarchy, lower-case identifiers within the hierarchy, hyphens to separate words in those identifiers.

For example: servers.file-storage.s3-api-key, servers.file-storage.s3-bucket.

Each Configuration implementation maps from that naming convention to the convention used by the underlying configuration store. E.g. the EnvironmentVariables implementation maps Java property name convention to the upper-case-and-underscores convention used for Unix environment variables.

Configuration is an interface and Key is a data class. This makes it straight forward to write an implementation of Configuration that translates the names of keys to different naming conventions, if your configuration follows an unusual convention.

Reflectomagic key definition

Konfig has a few ways to reduce boilerplate code when defining configuration keys.

  1. You can use Kotlin's delgated property protocol to name keys after the constants that hold them:

    val host by stringType // defines a key named "host"
    val port by intType    // defines a key named "port"
    
    ...
    
    val client = TcpClient(configuration[host], configuration[port])
    
  2. You can declare objects that extend PropertyGroup to define hierarchies of property keys that follow the Konfig naming conventions described above:

    object server : PropertyGroup() {
        val base_uri by uriType   // defines a key named "server.base-uri"
        val api_key by stringType // defines a key named "server.api-key"
    }
    
    ...
    
    val client = HttpClient(configuration[server.base_uri], configuration[server.api_key])
    
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].