This repository has been archived by the owner. It is now read-only.
Configure Scala applications in a simple flow
Scala
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
project
src
.gitignore
.travis.yml
README.md
RELEASE_NOTES.md
build.sbt
version.sbt

README.md

wvlet-config

NOTICE: wvlet-config is now part of Airframe project: http://wvlet.org/airframe/docs/airframe-config.html

wvlet-config enables configure your Scala applications in a simple flow:

  1. Write config classes of your application.
  2. Read YAML files to populate the config objects.
  3. (optional) Override the configuration with Properties.
  4. Use it!

Maven Central

libraryDependencies += "org.wvlet" %% "wvlet-cofig" % "(version)"

Detailed application configuration flow

Here is the details of the application configuration flow:

  1. The application specifies an environment (e.g., test, staging, production, etc) and configuration file paths.
  2. Read a configuration file (YAML) from configpath(s).
    • The first found YAML file in the config paths will be used.
    • config.registerFromYaml[A](yaml file) will create an object A from the YAML data.
    • If the YAML file does not contain data for the target environment, it searches for default environment instead.
      • If default environment is also not found, the provided default object will be used (optional).
  3. (optional) Provide additional configurations (e.g., confidential information such as password, apikey, etc.)
    • Read these configurations in a secure manner and create a Properties object.
    • Override your configurations with config.overideWithPropertie(Properties).
  4. Get the configuration with config.of[A]

Example

config/access-log.yml:

default:
  file: log/access.log
  max_files: 50

config/db-log.yml:

default:
  file: log/db.log

config/server.yml

default:
  host: localhost
  port: 8080

# Override the port number for development
development:
  <<: *default
  port: 9000

config.properties:

# [prefix](@[tag])?.[param]=(value)
log@access.file=/path/to/access.log
log@db.file=/path/to/db.log
log@db.log.max_files=250
server.host=mydomain.com
server.password=xxxxxyyyyyy

Sacla code:

import wvlet.config.Config
import wvlet.obj.tag.@@

// Configuration classes can have default values
// Configuration class name convention: xxxxConfig (xxxx will be the prefix for properties file)
case class LogConfig(file:String, maxFiles:Int=100, maxSize:Int=10485760)
case class ServerConfig(host:String, port:Int, password:String)

// To use the same configuration class for different purposes, use type tag (@@ Tag)
trait Access
trait Db

val config = 
  Config(env="development", configPaths="./config")
    .registerFromYaml[LogConfig @@ Access]("access-log.yml")
    .registerFromYaml[LogConfig @@ Db]("db-log.yml")
    .registerFromYaml[ServerConfig]("server.yml")
    .overrideWithPropertiesFile("config.properties")
    
val accessLogConfig = config.of[LogConfig @@ Access]
// LogConfig("/path/to/access.log",50,104857600)

val dbLogConfig = config.of[LogConfig @@ Db]
// LogConfig("/path/to/db.log",250,104857600)

val serverConfig = config.of[ServerConfig]
// ServerConfig(host="mydomain.com",port=9000,password="xxxxxyyyyyy")

Show configuration changes

To see the effective configurations, use Config.getConfigChanges method:

for(change <- config.getConfigChanges) {
  println(s"[${change.key}] default:${change.default}, current:${change.current}")
}