Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Examples

Twenty-six of them, each showing one idea. All run from the workspace root.

Getting started

ExampleFeaturesShows
basicjsonLoad once, read the snapshot.
sectionsjson, watchSeveral config types over one set of files, each owning its own key, files and watcher.
errorsjsonEvery ErrorKind, what each one calls for, and reading path and origin.

Where values come from

ExampleFeaturesShows
layersjsonOne key climbing all five layers, with source_of naming each.
env_onlyjsonNo files at all: the 12-factor arrangement, nested and list values included.
discoveryjsonname + paths across two directories, plus a profile overlay.
cliclap, jsonFlags over the environment, --set key=value, and --check instead of booting.

Reloading

ExampleFeaturesShows
hot_reloadwatch, tomlEdit a file and watch the snapshot follow.
async_reloadasync, watch, jsonA task awaiting reloads instead of polling for them.
groupjsonTwo config types reloading as one step, or not at all.

Getting it right

ExampleFeaturesShows
validationjsonRejecting a configuration where every field is valid and the whole is not.
secretsjson#[config(secret)], and precisely what it does and does not cover.
testingjsonPinning configuration under test with the override layer.

With a web framework

ExampleFeaturesShows
axum_hellowatch, jsonA handler that reads current() per request, a /config/check probe, and why the listen port is start-up configuration.
actix_hellowatch, jsonThe same across Actix's worker threads, and why configuration does not belong in web::Data.

On a runtime

ExampleFeaturesShows
tokio_runtimetokio, watch, jsonTwo readers each waking on their own changes() handle, with tokio's blocking pool wired in for free.
smol_runtimeasync, watch, jsonThe whole async surface on smol, with smol's unblock installed as the blocking executor and no tokio in the build.
embassy_runtimeasync, jsonEmbassy — an executor for microcontrollers, with no threads and no reactor — driving changes(), and why two rapid reloads are one wakeup.

Reaching further

ExampleFeaturesShows
unitsjson"30s" and "64MiB", from files and from the environment.
genericjsonDb<Postgres> and Db<Mysql> with separate snapshots.
persistencejsonWriting back atomically, and reading keys with no field.
remotejsonA RemoteSource of your own: explicit fetch, where it sits between the layers, an unreachable store, and a watch loop pushing through apply_remote.
last_known_goodjsonAll three cache_modes against the same broken file, with each cache file printed.
encryptedage, jsonA secrets.json.age next to a plain config.json: generated key, real ciphertext, and what the wrong key looks like.
schemaschema, jsonA JSON Schema for the file two config types share, with secrets marked and required dropped.
no_macrojsonload, LoadSpec, Layer and ConfigCell without the attribute.
cargo run -p dynamic-config --example errors      --features json
cargo run -p dynamic-config --example layers      --features json
cargo run -p dynamic-config --example cli         --features clap,json -- --check
cargo run -p dynamic-config --example hot_reload  --features watch,toml
cargo run -p dynamic-config --example remote      --features json
cargo run -p dynamic-config --example last_known_good --features json
cargo run -p dynamic-config --example schema      --features schema,json
cargo run -p dynamic-config --example encrypted   --features age,json
cargo run -p dynamic-config --example axum_hello  --features watch,json
cargo run -p dynamic-config --example actix_hello --features watch,json
cargo run -p dynamic-config --example tokio_runtime --features tokio,watch,json
cargo run -p dynamic-config --example smol_runtime --features async,watch,json
cargo run -p dynamic-config --example embassy_runtime --features async,json
APP_ENV=production cargo run -p dynamic-config --example discovery --features json