Configuration
Every setting is declared once, in code, by the module or package that reads it. Values come from
files and the environment; there is no object binding and no reflection. Java and Go implement the
same rules and are checked against the same vector (conformance/config/config.json), so the same
file and environment give the same values, the same errors and the same error texts in both.
Configuration or code
Section titled “Configuration or code”Configuration holds the settings of services and connections, and secrets: the shutdown drain, health timeouts and criticality, the metrics driver, the HTTP server’s limits, the FlowCatalyst module’s switches and timeouts, database and Redis instances with their pools, URLs, user names, client ids, passwords, tokens and keys.
Code defines the things an application can have many of, each with its own policy: rate limiters, background loops, lock stores, secret stores and named HTTP clients with their retry and authentication policies, and which driver backs each. The test:
- Could an application want several of these, with different values? Then it is code.
- Is it one setting of the service, or of a connection? Then it is configuration.
A key whose behaviour moved to code fails start-up when it is still in a file or in the environment, and the message says where it went (see Removed keys). The reasoning is in the config and code specification.
Declaring and reading a setting
Section titled “Declaring and reading a setting”Declare in register, keep the Setting in a constant, read it after start-up through Config:
static final Setting<String> HOST = Setting.string("mail.host").asRequired().describedAs("SMTP host.");static final Setting<Duration> TIMEOUT = Setting.duration("mail.timeout").withDefault(Duration.ofSeconds(10));static final Setting<Secret> PASSWORD = Setting.secret("mail.password");
@Overridepublic void register(AppBuilder app) { app.declare(HOST); app.declare(TIMEOUT); app.declare(PASSWORD); app.provide( Mailer.class, lookup -> { Config config = lookup.get(Config.class); return new Mailer(config.get(HOST), config.find(PASSWORD).orElse(Secret.of(""))); });}Settings are values, not annotations: Setting.string, integer, longValue, bool, duration,
uri, stringList, enumOf(key, values) and secret. Each starts optional with no default;
asRequired(), withDefault(value) and describedAs(text) return a modified copy.
config.get(setting)throwsIllegalStateExceptionfor an optional setting nobody set, so useconfig.find(setting)(anOptional) for those. Passing a setting that was not declared isIllegalArgumentException.- Settings are not readable in
register; read them in a provider,bootor later.
Settings are immutable descriptors with a type; config.Declare returns the handle that reads the
value after configuration:
drain: config.Declare(d, config.Duration("app.shutdown.drain", "Time each shutdown step may take before it is logged as overrun.").WithDefault(10*time.Second)),Constructors: config.String, Int (32 bits), Int64, Bool, Duration, URL, StringList,
SecretValue and Enum[E](key, description, values...). A setting takes WithDefault, a required
marker and Checked(config.Between(lo, hi)) for a range.
handle.Get(b) cannot fail for a required setting or one with a default; Lookup returns
(value, false) for an optional one that nobody set.
Rules shared by both:
- The key is dotted lowercase,
[a-z][a-z0-9_-]*(\.[a-z][a-z0-9_-]*)*. A key declared twice fails start-up. - Required means start-up fails when no source gives a value. A default counts as a value.
- Values do not change while the application runs. There is no hot reload.
- A range check is part of the setting; a value out of range is a problem naming the key, never corrected silently.
Value formats
Section titled “Value formats”| Type | Accepts |
|---|---|
| string, integer, long | as written |
| boolean | true or false only |
| duration | 500ms, 30s, 5m, 2h, 1d, or ISO-8601 such as PT30S |
| URI / URL | any URI |
| string list | a YAML list, or comma-separated text |
| enum | the constant names |
| secret | text, kept in a Secret |
A value that does not parse fails start-up with the key, the source it came from and the expected type. The raw text of a secret is never included in an error.
Sources, lowest to highest
Section titled “Sources, lowest to highest”- The declaration’s default.
config/application.yaml, thenconfig/application-{env}.yaml. Missing files are fine.config/secrets.local.yaml, only whenenvislocalortest. In any other environment this file is ignored.- Environment variables.
env is the environment variable APP_ENV, default local. The config directory is config/ under
the working directory unless you change it (builder.configDirectory(path) in Java,
fw.WithConfigDir(dir) in Go). A later source wins over an earlier one, key by key. There are no
external stores yet.
Environment variable names
Section titled “Environment variable names”The key uppercased, with . and - replaced by _: mail.host is MAIL_HOST, http.port is
HTTP_PORT. The environment is never scanned: only names derived from declared keys and instance
sections are looked up. Tests pass their own map (builder.environment(Map) in Java,
fw.WithEnvironment in Go) so they never touch the real one.
Two keys, one variable. A key may contain _, and both . and - become _, so a.b-c,
a.b_c and a.b.c all read A_B_C. Start-up fails when it finds two such keys (declared keys,
or an instance key from your files and a declared key). Rename one.
Names and components
Section titled “Names and components”Keys are one global namespace shared by every module: pick a key that cannot collide, usually
starting with your component (mail.host). The first segment is the component, the rest the
path. The framework’s own settings (app, http, database, redis, flowcatalyst, auth)
stay in environment variables and files.
Named instances
Section titled “Named instances”Connections are configured as named instances under a section. An instance section holds the connection, its secrets and its settings.
database: default: main instances: main: url: jdbc:postgresql://localhost:5432/app username: app password: "" pool: { max: 10, min-idle: 1, connect-timeout: 5s, login-timeout: 10s } migrate: true
redis: default: default instances: default: { url: "redis://localhost:6379/0", timeout: 2s }The instance must be named in a YAML file; DATABASE_INSTANCES_MAIN_URL or
DATABASE_INSTANCES_MAIN_PASSWORD then override single values. An instance cannot be created from
environment variables alone. A key an instance does not read fails start-up as unknown.
<section>.default names the default instance; with exactly one instance it may be omitted.
| Section | Keys |
|---|---|
database.instances.<n> |
url (required), username, password (secret), pool.max (10; 1 to 1000), pool.min-idle (1; 0 to pool.max), pool.connect-timeout (5s; 250 ms to 5 min), pool.login-timeout (10s; 1 s to 5 min, whole seconds; Java only), migrate (true; counts on the default instance) |
redis.instances.<n> |
url (required, redis:// or rediss://, may carry user, password and database number), password (secret, wins over the URL’s), timeout (2s; more than 0, at most 5 min), pool.max (16; 1 to 1000) |
http-clients.instances.<n> |
base-url and the credentials of the client’s auth strategy; the client itself is named in code (HTTP client) |
Settings the framework declares
Section titled “Settings the framework declares”| Key | Type | Default | Module |
|---|---|---|---|
app.shutdown.drain |
duration | 10s |
core |
app.health.timeout |
duration | 2s |
core |
app.health.critical, app.health.informational |
string lists | empty | core |
app.metrics.driver |
memory or noop |
memory |
core |
app.metrics.max-series |
integer, 1 to 100 000 | 1000 |
core |
http.host, http.port, http.max-body-bytes, http.request-timeout, http.shutdown-grace, http.metrics.*, http.cors.* |
HTTP | ||
flowcatalyst.*, flowcatalyst.receivers.* |
FlowCatalyst module, Receivers | ||
auth.issuer, client-id, client-secret, public-base-url, cookie-keys |
Sessions and login | ||
push.* |
Push |
Locks, secrets and HTTP clients declare no setting: their stores and clients are named in code.
Making a code-defined thing differ per deployment
Section titled “Making a code-defined thing differ per deployment”Service settings need nothing; they are configuration already. For a thing defined in code that must
differ per deployment, declare a setting of your own in your own component and read it where the
thing is built. In Java, store factories receive the Lookup, a loop or limiter is built in boot
with app.config(), and a named HTTP client takes a function of Config:
HttpClientModule.builder() .client( "billing", config -> HttpClientOptions.defaults().withRequestTimeout(config.get(TIMEOUT))) .build();In Go the build function reads the handle and passes the value in.
Removed keys and claimed components
Section titled “Removed keys and claimed components”Removed keys. The framework keeps one list of keys whose behaviour moved to code (Java
KeyRules.frameworkRemovedKeys(), Go config.FrameworkRemovedKeys()). Start-up fails when one is in
a file (the key or anything under it; locks covers every locks.*), or in the environment (the
variable named after the key; a * is replaced by each instance name your files have there). The
problem says where the thing is defined now, for example:
key 'http-clients.default' (from environment variable HTTP_CLIENTS_DEFAULT): moved to code: nothing: clients are named in code, HttpClientModule.builder().client(name, options)The list applies whatever modules you list, so a leftover section of a module you dropped still fails. A key that some module declares is never treated as removed.
Claimed components. A module claims a component (the first key segment) so that a typo does not
pass silently: every key under it in your files must be declared by some module, or start-up fails
naming the keys the component has. Java: app.claimComponent("mail") in register; Go:
Declarer.ClaimComponent. The framework claims app, http, http-client, locks, secrets,
flowcatalyst, auth, object-stores, push and idempotency (the last in Java; Go claims object-stores and push too). Two claims of one component fail start-up. Environment variables are not
checked for unknown keys, because they are never listed.
Migrating older configuration
Section titled “Migrating older configuration”Applications written before the code-and-config ruling had keys that now fail start-up. The mapping:
| Old configuration | Now |
|---|---|
locks.* (store with driver, database, connection, prefix, locks.default) |
LocksModule.builder().store(name, DatabaseLockStores.database(..) / RedisLockStores.redis(..) / LockStores.memory()) and .defaultStore(..) (Locks) |
secrets.* (stores, cache-ttl, cache-max, default) |
SecretsModule.builder().store(name, SecretStores.env(..) / file(..) / platform()), .withCache(CachePolicy.of(..)), .defaultStore(..) (Secrets) |
an http-clients instance’s retry, timeouts, auth.type |
HttpClientModule.builder().client(name, HttpClientOptions...); the section keeps base-url and credentials (HTTP client) |
rate-limits.* |
MemoryRateLimiter.create(..) or RedisRateLimiter.create(..) (Rate limiting) |
driver: in database or Redis instances |
delete it (one driver each) |
Start the application: each leftover key fails with a message naming it and its replacement, so
nothing is ignored silently. The Go packages have the equivalents (locks.WithStore,
secrets stores built in code, ratelimit limiters in code).
Secrets
Section titled “Secrets”Secret is its own type. It prints Secret[****] through every formatting path, cannot be
serialised, and gives its value only through reveal() (Java) or Reveal (Go). matches(candidate)
compares a presented value in constant time. A secret declaration never has a default.
- Local and tests: put it in
config/secrets.local.yaml(add the file to your.gitignore) or set the environment variable. - Anywhere else: the environment variable.
secrets.local.yamlis ignored whenAPP_ENVis notlocalortest. - Do not commit a real secret in
application.yaml.
Application-level secrets come from the environment and that local file only. Credentials that belong to a client rather than the application are per-client secrets.
Errors
Section titled “Errors”Configuration problems are collected and reported together from start-up (StartupException in Java,
*fw.StartupError in Go): an unparseable value, a missing required setting, an unknown key in an
instance section, a duplicate declaration, two keys reading one environment variable, an invalid YAML
file, a removed key, an unknown key under a claimed component. Each names the key and the source, for
example the file or environment variable MAIL_HOST.
Allowed difference. A YAML file with a duplicate key is refused by Go
(invalid YAML: ... mapping key "k" already defined), where the Java reader keeps the last value.
The specifications are Core, Config and code and Database and Redis.