diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..71bec9d --- /dev/null +++ b/src/config.rs @@ -0,0 +1,43 @@ +use serde::Deserialize; +use std::env; +use std::fs::File; +use std::io::BufReader; + +#[derive(Deserialize)] +pub struct EmailServer { + pub server_name: String, + pub email: String, + pub password: String, +} + +#[derive(Deserialize)] +pub struct Address { + pub name: String, + pub user: String, + pub domain: String, +} + +#[derive(Deserialize)] +pub struct Config { + pub path_prefix: String, + pub email_server: EmailServer, + pub email_from: Address, + pub email_to: Address, +} + +impl Config { + pub fn new() -> Self { + let config_file_var = "CF_CONFIG_FILE"; + let config_path = env::var(config_file_var) + .unwrap_or_else(|_| panic!("Environment variable {config_file_var} missing!")); + + let config_file = File::open(&config_path).unwrap_or_else(|e| { + panic!("Can not open the config file at the path {config_path}: {e}") + }); + let config_reader = BufReader::new(config_file); + let config: Self = + serde_json::from_reader(config_reader).expect("Can not parse the config file as JSON!"); + + config + } +}