TOML configuration parser for Rust.
Add Konfiguration to your Cargo.toml
:
toml
[dependencies]
konfiguration = "1.0.0"
serde = { version = "1.0", features = ["derive"] }
Create your configuration file:
```toml profile = { env = "PROFILE", default = "local" } rustlog = "info" serverport = { env = "PORT", default = 8080 } corsorigin = { env = "CORSALLOWEDORIGINS", default = "*" } exponentialbackoff = { env = "EXPONENTIAL_BACKOFF", default = [1, 2, 3] }
[postgres] username = { env = "DATABASEUSERNAME", default = "root" } password = { env = "DATABASEPASSWORD", default = "root" } host = { env = "DATABASEHOST", default = "localhost" } port = { env = "DATABASEPORT", default = 5432 } database = { env = "DATABASENAME", default = "postgres" } minconnections = { env = "DATABASEMINCONNECTIONS", default = 3 } maxconnections = { env = "DATABASEMAXCONNECTIONS", default = 10 } connectionacquiretimeoutsecs = { env = "DATABASECONNECTIONACQUIRETIMEOUTSECONDS", default = 10 } enablemigration = { env = "DATABASEENABLEMIGRATIONS", default = false } migrationsdir = "./migrations" ```
Load the configuration file: ```rust,no_compile use serde::Deserialize; use konfiguration::Konfiguration;
pub struct Config {
pub profile: String,
pub rustlog: String,
pub corsorigin: String,
pub serverport: u16,
pub exponentialbackoff: Vec
pub struct PostgresConfig { pub host: String, pub username: String, pub password: String, pub database: String, pub port: u16, pub minconnections: u32, pub maxconnections: u32, pub connectionacquiretimeoutsecs: u64, pub enablemigration: bool, pub migrations_dir: String, }
fn main() {
let config = Konfiguration::from_file("filepath/config.json")
.parse::
println!("{:?}", config);
} ```
Take a look at our contributing guide if you wish to contribute.