Move .env configuration to config.toml

This commit is contained in:
Neshura 2023-12-29 00:36:01 +01:00
parent 4ba04706b5
commit f78f735b2c
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C
5 changed files with 128 additions and 40 deletions

View file

@ -140,3 +140,47 @@ impl Default for ZoneConfig {
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub(crate) struct AppConfig {
#[serde(alias="cf_api_token")]
pub(crate) cloudflare_api_token: String,
pub(crate) check_interval_seconds: Option<u16>,
pub(crate) uptime_url: Option<String>,
}
impl AppConfig {
pub(crate) fn load() -> Result<Self, Box<dyn Error>> {
let cfg: Self = match confy::load(env!("CARGO_PKG_NAME"),"config") {
Ok(data) => data,
Err(e) => {
match connected_to_journal() {
true => error!("[ERROR] {e}"),
false => eprintln!("[ERROR] {e}")
}
return Err(Box::new(e));
}
};
if cfg.cloudflare_api_token.is_empty() {
let err_msg = "Cloudflare api token not specified. The app cannot work without this";
match connected_to_journal() {
true => error!("[ERROR] {err_msg}"),
false => eprintln!("[ERROR] {err_msg}")
}
panic!("{err_msg}");
}
Ok(cfg)
}
}
impl Default for AppConfig {
fn default() -> Self {
Self {
cloudflare_api_token: "".to_owned(),
check_interval_seconds: None,
uptime_url: None
}
}
}