Configuration
Documentation / Configuration / Accessing the configuration in code

Accessing the configuration in code

Loading the configuration

use runique::config::RuniqueConfig;

let config = RuniqueConfig::from_env();

println!("Development: {}", config.dev);
println!("Port: {}", config.server.port);

RuniqueConfig::from_env() reads .env and the environment once, at startup. Its main fields:

FieldContent
config.devRUNIQUE_ENV=dev
config.debugDEBUG
config.serverip_server, port, secret_key, public_url (set by .with_public_url(...))
config.static_filesFolders and prefixes: templates, static, media
config.securityHTTPS, HSTS, ACME
config.passwordThe PasswordConfig
config.timezoneTZ

In a handler

async fn my_handler(mut request: Request) -> AppResult<Response> {
    let config = &request.engine.config;

    context_update!(request => {
        "dev" => config.dev,
        "port" => config.server.port,
    });
    request.render("page.html")
}

Never hand config.server.secret_key to a template or a log: it signs the CSRF tokens and the sessions.


Conditional configuration

if request.engine.config.dev {
    // Development: templates read again on every request, no startup checks
} else {
    // Production
}

Outside a handler, is_dev() (in the prelude) gives the same answer. config.debug and is_debug() only tell one thing: whether the detailed error pages are shown.