8 个版本 (4 个稳定版)
1.2.0 | 2023年9月17日 |
---|---|
1.1.0 | 2023年9月16日 |
0.2.2 | 2023年7月17日 |
0.1.0 | 2023年7月16日 |
#565 in 配置
每月 23 次下载
16KB
262 行
Konfiguration
Rust 的 TOML 配置解析器。
用法
快速入门
将 Konfiguration 添加到您的 Cargo.toml
[dependencies]
konfiguration = "1.0.1"
serde = { version = "1.0", features = ["derive"] }
创建您的配置文件
profile = { env = "PROFILE", default = "local" }
rust_log = "info"
server_port = { env = "PORT", default = 8080 }
cors_origin = { env = "CORS_ALLOWED_ORIGINS", default = "*" }
exponential_backoff = { env = "EXPONENTIAL_BACKOFF", default = [1, 2, 3] }
[postgres]
username = { env = "DATABASE_USERNAME", default = "root" }
password = { env = "DATABASE_PASSWORD", default = "root" }
host = { env = "DATABASE_HOST", default = "localhost" }
port = { env = "DATABASE_PORT", default = 5432 }
database = { env = "DATABASE_NAME", default = "postgres" }
min_connections = { env = "DATABASE_MIN_CONNECTIONS", default = 3 }
max_connections = { env = "DATABASE_MAX_CONNECTIONS", default = 10 }
connection_acquire_timeout_secs = { env = "DATABASE_CONNECTION_ACQUIRE_TIMEOUT_SECONDS", default = 10 }
enable_migration = { env = "DATABASE_ENABLE_MIGRATIONS", default = false }
migrations_dir = "./migrations"
加载配置文件
use serde::Deserialize;
use konfiguration::Konfiguration;
#[derive(Debug, Deserialize)]
pub struct Config {
pub profile: String,
pub rust_log: String,
pub cors_origin: String,
pub server_port: u16,
pub exponential_backoff: Vec<u16>,
pub mail: MailConfig,
pub postgres: PostgresConfig,
pub redis: RedisConfig,
}
#[derive(Debug, Deserialize)]
pub struct PostgresConfig {
pub host: String,
pub username: String,
pub password: String,
pub database: String,
pub port: u16,
pub min_connections: u32,
pub max_connections: u32,
pub connection_acquire_timeout_secs: u64,
pub enable_migration: bool,
pub migrations_dir: String,
}
fn main() {
let config = Konfiguration::from_file("filepath/config.toml")
.parse::<Config>()
.unwrap();
println!("{:?}", config);
}
贡献
如果您想贡献,请查看我们的 贡献指南。
依赖
~0.8–1.3MB
~30K SLoC