| Crates.io | axum-config |
| lib.rs | axum-config |
| version | 0.1.0 |
| created_at | 2025-12-31 02:16:12.334189+00 |
| updated_at | 2025-12-31 02:16:12.334189+00 |
| description | Proc macros for axum-config |
| homepage | |
| repository | https://github.com/MrRevillod/axum-config |
| max_upload_size | |
| id | 2013758 |
| size | 41,153 |
Configuration management for Axum applications. Load configuration from TOML files with environment variable support.
${VAR} and ${VAR:default})cargo add axum-config
use serde::Deserialize;
use axum_config::config;
#[config(key = "database")]
#[derive(Debug, Clone, Deserialize)]
pub struct DatabaseConfig {
pub host: String,
pub port: u16,
pub user: String,
}
config/config.toml:
[database]
host = "${DB_HOST:localhost}"
port = 5432
user = "${DB_USER:admin}"
use axum::{Router, routing::get, Extension};
use axum_config::{Config, ExtractConfig};
use std::sync::Arc;
#[tokio::main]
async fn main() {
let config = Config::new().expect("Failed to load config");
let app = Router::new()
.route("/", get(handler))
.layer(Extension(Arc::new(config)));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
axum::serve(listener, app).await.unwrap();
}
// If your handlers are separated into another modules,
// it's recommended to rename `ExtractConfig` to `Config` for clarity.
// use axum_config::ExtractConfig as Config;
async fn handler(
ExtractConfig(db_config): ExtractConfig<DatabaseConfig>,
) -> String {
format!("DB: {}:{}", db_config.host, db_config.port)
}
Config::from_path("path/to/config.toml")CONFIG_FILE_PATHconfig/config.toml<exe_dir>/config/config.tomlSupports interpolation in TOML with default values:
[server]
host = "${HOST:0.0.0.0}" # Uses HOST or defaults to "0.0.0.0"
port = "${PORT}" # Requires PORT to be defined
let config = Config::new().expect("Failed to load config");
// Get configuration
let db = config.get::<DatabaseConfig>();
// With default if missing
let db = config.get_or_default::<DatabaseConfig>();
// Panics if missing
let db = config.get_or_panic::<DatabaseConfig>();