NOTICE: wvlet-config is now part of Airframe project: http://wvlet.org/airframe/docs/airframe-config.html
- New source code location: https://github.com/wvlet/airframe/tree/master/airframe-config
wvlet-config enables configure your Scala applications in a simple flow:
- Write config classes of your application.
- Read YAML files to populate the config objects.
- (optional) Override the configuration with Properties.
- Use it!
libraryDependencies += "org.wvlet" %% "wvlet-cofig" % "(version)"
Here is the details of the application configuration flow:
- The application specifies an environment (e.g.,
test
,staging
,production
, etc) and configuration file paths. - 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 objectA
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).
- If
- (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)
.
- Read these configurations in a secure manner and create a
- Get the configuration with
config.of[A]
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")
To see the effective configurations, use Config.getConfigChanges
method:
for(change <- config.getConfigChanges) {
println(s"[${change.key}] default:${change.default}, current:${change.current}")
}