major config rebuild using once cell. added ability to use .env.

This commit is contained in:
mtrx 2024-08-10 08:24:22 +02:00
parent 35cd018152
commit c30976f2d7
26 changed files with 207 additions and 186 deletions

View file

@ -1,72 +1,83 @@
use std::sync::Mutex;
use dotenvy::dotenv;
use once_cell::sync::Lazy;
struct Config {
pub const ENV_PREFIX: &str = "CL_";
pub struct Config {
pub submit_token: String,
pub root_path: String,
pub username: String,
pub email: String,
pub bind_port: String,
pub accounts: Accounts
pub accounts: Accounts,
}
struct Accounts {
pub github: Option<String>,
pub twitter: Option<String>,
pub mastodon: Option<String>,
pub discord: Option<String>,
pub reddit: Option<String>,
pub struct Accounts {
pub github: Option<String>,
pub twitter: Option<String>,
pub mastodon: Option<String>,
pub discord: Option<String>,
pub reddit: Option<String>,
}
fn load_config() -> Config {
dotenv().expect(".env file not found");
// return config value or panic if not set
let eval_required_conf = |variable_name| {
match std::env::var(variable_name) {
Ok(_) => {
let value = std::env::var(variable_name).unwrap();
fn eval_required_conf(variable_name: String) -> String {
match std::env::var(variable_name.clone()) {
Ok(value) => {
println!("{}: {}", variable_name, value);
return value;
},
}
Err(_) => {
panic!("{} not set!", variable_name)
}
}
};
}
// return optional value
let eval_optional_conf = |variable_name| {
match std::env::var(variable_name) {
Ok(_) => {
let value = std::env::var(variable_name).unwrap();
fn eval_optional_conf(variable_name: String, default_value: Option<&str>) -> Option<String> {
match std::env::var(variable_name.clone()) {
Ok(value) => {
println!("{}: {}", variable_name, value);
return Some(value);
},
Err(_) => {
return None
}
Err(_) => match default_value {
Some(val) => {
println!("Variable {variable_name} not set. Using default value: {val}.");
return Some(String::from(val));
}
None => {
println!("Variable {variable_name} not set. No default, leaving this empty.");
None
}
},
}
};
}
fn eval_conf_var(name: &str, required_var: bool, default: Option<&str>) -> Option<String> {
if required_var {
Some(eval_required_conf(format!("{ENV_PREFIX}{name}")))
} else {
eval_optional_conf(format!("{ENV_PREFIX}{name}"), default)
}
}
Config {
submit_token: eval_required_conf("SUBMIT_TOKEN"),
root_path: eval_required_conf("ROOT_PATH"),
username: eval_required_conf("USERNAME"),
email: eval_required_conf("EMAIL"),
bind_port: eval_required_conf("BIND_PORT"),
submit_token: eval_conf_var("SUBMIT_TOKEN", true, None).unwrap(),
root_path: eval_conf_var("ROOT_PATH", false, Some("./content")).unwrap(),
username: eval_conf_var("USERNAME", true, None).unwrap(),
email: eval_conf_var("EMAIL", false, None).unwrap(),
bind_port: eval_conf_var("BIND_PORT", false, Some("8000")).unwrap(),
accounts: Accounts {
github: eval_optional_conf("GITHUB_ACCOUNT"),
discord: eval_optional_conf("DISCORD_ACCOUNT"),
twitter: eval_optional_conf("TWITTER_ACCOUNT"),
mastodon: eval_optional_conf("MASTODON_ACCOUNT"),
reddit: eval_optional_conf("REDDIT_ACCOUNT"),
}
github: eval_conf_var("GITHUB_ACCOUNT", false, None),
discord: eval_conf_var("DISCORD_ACCOUNT", false, None),
twitter: eval_conf_var("TWITTER_ACCOUNT", false, None),
mastodon: eval_conf_var("MASTODON_ACCOUNT", false, None),
reddit: eval_conf_var("REDDIT_ACCOUNT", false, None),
},
}
}
pub static CONFIG: Lazy<Config> = Lazy::new(|| {
load_config()
});
pub static CONFIG: Lazy<Config> = Lazy::new(|| load_config());