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:
| Field | Content |
|---|---|
config.dev | RUNIQUE_ENV=dev |
config.debug | DEBUG |
config.server | ip_server, port, secret_key, public_url (set by .with_public_url(...)) |
config.static_files | Folders and prefixes: templates, static, media |
config.security | HTTPS, HSTS, ACME |
config.password | The PasswordConfig |
config.timezone | TZ |
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_keyto 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.