#configuration #settings #env #environment

konfiguration

Toml configuration loader with environment variables support

8 releases (4 stable)

1.2.0 Sep 17, 2023
1.1.0 Sep 16, 2023
0.2.2 Jul 17, 2023
0.1.0 Jul 16, 2023

#198 in Configuration

Download history 35/week @ 2023-11-02 24/week @ 2023-11-09 27/week @ 2023-11-16 73/week @ 2023-11-23 64/week @ 2023-11-30 38/week @ 2023-12-07 39/week @ 2023-12-14 27/week @ 2023-12-21 10/week @ 2023-12-28 30/week @ 2024-01-04 21/week @ 2024-01-11 8/week @ 2024-01-18 38/week @ 2024-01-25 31/week @ 2024-02-01 35/week @ 2024-02-08 145/week @ 2024-02-15

249 downloads per month

MIT license

16KB
262 lines

Konfiguration

Rust Docs.rs

TOML configuration parser for Rust.

Usage

Quickstart

Add Konfiguration to your Cargo.toml:

[dependencies]
konfiguration = "1.0.1"
serde = { version = "1.0", features = ["derive"] }

Create your configuration file:

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"

Load the configuration file:

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);
}

Contribuiting

Take a look at our contributing guide if you wish to contribute.

Dependencies

~0.8–1.4MB
~32K SLoC