54 lines
1.6 KiB
Rust
54 lines
1.6 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;
|
|
|
|
// environment variable keys to check for
|
|
const ENV_VARS: [&str; 4] = ["STANDALONE_MODE", "DISABLE_AUTH", "ADDR", "PORT"];
|
|
|
|
pub struct Config {
|
|
pub standalone_mode: bool,
|
|
pub disable_auth: bool,
|
|
pub addr: String,
|
|
pub port: String,
|
|
}
|
|
|
|
impl Default for Config {
|
|
fn default() -> Self {
|
|
Self {
|
|
standalone_mode: true,
|
|
disable_auth: false,
|
|
addr: "127.0.0.1".to_string(),
|
|
port: "8000".to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
pub fn new() -> Config {
|
|
// define default config values
|
|
let mut config = Config::default();
|
|
|
|
// check for the environment variables in our config
|
|
for key in ENV_VARS {
|
|
// if a variable (key) has been set, check the value
|
|
if let Ok(val) = env::var(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
|
|
}
|
|
}
|