Skip to content

New entries

Configuration in Pavex is managed via your application's Blueprint.

Registration

Invoke the Blueprint::config method to add a new configuration entry:

src/base/blueprint.rs
// [...]
pub fn blueprint() -> Blueprint {
    let mut bp = Blueprint::new();
    bp.config("database", t!(super::DatabaseConfig));
    // [...]
}

config expects:

t! vs f!

t! stands for "type". It isn't f!, the macro used to register function-like components, like constructors or middlewares.
If you mix them up, Pavex will return an error.

Required Traits

Configuration types must implement the Debug, Clone, and serde::Deserialize traits. serde::Deserialize, in particular, is required to parse configuration values out of environment variables, configuration files, and other sources.

src/base/config.rs
use secrecy::Secret;

#[derive(serde::Deserialize, Debug, Clone)]
pub struct DatabaseConfig {
    pub username: String,
    pub password: Secret<String>,
    pub host: String,
    pub database_name: String,
    pub require_ssl: bool,
}

Configuration key

The configuration key uniquely identifies a configuration entry. It must start with a letter and can contain letters, digits and underscores.

Every configuration entry becomes a field in ApplicationConfig, and the configuration key is used as the field name. The key determines the expected configuration schema—e.g. which environment variables can be used to set its value and the structure of configuration files.

Lifecycle

Configuration entries are treated as singletons from Pavex's dependency injection system.

Most configuration entries are only needed to construct other singletons, so they'll be discarded after ApplicationState::new returns.

If a configuration entry is needed at runtime (e.g. to configure the behaviour of middleware or a request handler), it'll be added as a field to ApplicationState. In this case, Pavex will expect the configuration type to implement the Send and Sync traits in addition to the other trait requirements.