Add config.rs
This commit is contained in:
parent
047b5de2d1
commit
a1cd8f431d
1 changed files with 43 additions and 0 deletions
43
src/config.rs
Normal file
43
src/config.rs
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue