2024-08-17 18:59:31 +02:00
|
|
|
use std::{env, net::IpAddr, sync::OnceLock};
|
2024-06-08 00:41:43 +02:00
|
|
|
|
|
|
|
use dotenvy::dotenv;
|
|
|
|
|
|
|
|
static CONFIG: OnceLock<Config> = OnceLock::new();
|
|
|
|
|
|
|
|
pub struct Config {
|
|
|
|
pub zauth_url: String,
|
|
|
|
pub db_uri: String,
|
2024-07-14 22:00:26 +02:00
|
|
|
pub authoritative_zone: Vec<String>,
|
2024-08-17 18:59:31 +02:00
|
|
|
pub port: u16,
|
|
|
|
pub address: IpAddr,
|
2024-06-08 00:41:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
pub fn initialize() {
|
|
|
|
assert!(CONFIG.get().is_none());
|
|
|
|
|
|
|
|
Config::get();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get() -> &'static Config {
|
|
|
|
CONFIG.get_or_init(|| {
|
|
|
|
dotenv().ok();
|
|
|
|
Config {
|
|
|
|
db_uri: env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
|
|
|
|
zauth_url: env::var("ZAUTH_URL").expect("ZAUTH_URL must be set"),
|
2024-07-14 22:00:26 +02:00
|
|
|
authoritative_zone: env::var("ZONE")
|
|
|
|
.expect("ZONE must be set")
|
|
|
|
.split(".")
|
|
|
|
.map(str::to_string)
|
|
|
|
.collect(),
|
2024-08-17 18:59:31 +02:00
|
|
|
port: env::var("ZNS_PORT")
|
|
|
|
.map(|v| v.parse::<u16>().expect("ZNS_PORT is invalid"))
|
|
|
|
.unwrap_or(5333),
|
|
|
|
address: env::var("ZNS_ADDRESS")
|
|
|
|
.unwrap_or(String::from("127.0.0.1"))
|
|
|
|
.parse()
|
|
|
|
.expect("ZNS_ADDRESS is invalid"),
|
2024-06-08 00:41:43 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|