Skip to main content

Crate dynamic_config_loco

Crate dynamic_config_loco 

Source
Expand description

A request-scoped configuration snapshot for Loco.

use dynamic_config_loco::{sections, Config, DynamicConfig};
use loco_rs::prelude::*;

async fn initializers(_ctx: &AppContext) -> Result<Vec<Box<dyn Initializer>>> {
    Ok(vec![Box::new(DynamicConfig::new(sections![Server, Features]))])
}
async fn index(
    Config(server): Config<Server>,
    Config(features): Config<Features>,
) -> Result<Response> {
    // One reading, taken when the request began. `Sections::take`
    // retries if a reload lands mid-read, so these two cannot be
    // different generations.
    format::text(&format!("{} {}", server.port, features.cache))
}

§What this crate is

Loco is axum underneath, so the layer and the extractor are dynamic-config-axum’s, re-exported here unchanged. What Loco adds is a place to register one — the Initializer trait — and that registration is all this crate is.

Writing it by hand is three lines in your own initializer, and doing so is fine. This crate exists so that it is one line in initializers, and so that there is somewhere to write down the two things below.

§Loco’s own configuration is a different thing

Loco reads config/development.yaml into ctx.config at boot, and that is where the database URL, the worker mode and the server port live. None of it reloads, and none of it should: Loco binds its listener and builds its connection pool from those values once.

This crate is for the other half — the settings an operator changes while the service runs. Keep them in their own file with their own #[dynamic_config] sections, and leave ctx.config to Loco.

§What this crate does not do

It does not load configuration, watch files, or own a WatchHandle. Loco’s Hooks::boot is where a service does that, before the router exists.

Macros§

sections
The sections a request reads, by type.

Structs§

Config
One section of this request’s configuration.
ConfigSections
The sections a request reads, and how to read each one.
DynamicConfig
The initializer that puts one reading on every request.
Snapshot
What one request may read: one Arc per section, taken together.
SnapshotLayer
Takes one snapshot per request and puts it in the request’s extensions.

Enums§

NotInScope
Why a section is not in this request’s snapshot.
SnapshotMissing
Why a Config extractor could not answer.

Functions§

snapshot
The snapshot this request began with, for code that has the parts in hand rather than an extractor — another middleware, or a handler that takes Request whole.