peach-workspace/peach-web/src/config.rs

55 lines
1.7 KiB
Rust

//! Define the configuration parameters for the web application.
//!
//! Sets default values and updates them if the corresponding environment
//! variables have been set.
use std::env;
use peach_lib::config_manager::get_config_value;
// environment variable keys to check for
const CONFIG_KEYS: [&str; 4] = ["STANDALONE_MODE", "DISABLE_AUTH", "ADDR", "PORT"];
pub struct RouilleConfig {
pub standalone_mode: bool,
pub disable_auth: bool,
pub addr: String,
pub port: String,
}
impl Default for RouilleConfig {
fn default() -> Self {
Self {
standalone_mode: true,
disable_auth: false,
addr: "127.0.0.1".to_string(),
port: "8000".to_string(),
}
}
}
impl RouilleConfig {
pub fn new() -> RouilleConfig {
// define default config values
let mut config = RouilleConfig::default();
// check for the environment variables in our config
for key in CONFIG_KEYS {
// if a variable (key) has been set, check the value
if let Ok(val) = get_config_value(key) {
// if the value is of the correct type, update the config value
match key {
"STANDALONE_MODE" if val.as_str() == "true" => config.standalone_mode = true,
"STANDALONE_MODE" if val.as_str() == "false" => config.standalone_mode = false,
"DISABLE_AUTH" if val.as_str() == "true" => config.disable_auth = true,
"DISABLE_AUTH" if val.as_str() == "false" => config.disable_auth = false,
"ADDR" => config.addr = val,
"PORT" => config.port = val,
_ => (),
}
}
}
config
}
}