Moved config to project file and lazy loaded it

This commit is contained in:
rzuasti
2026-01-21 15:36:27 -05:00
parent 5fbd63df5f
commit 37ba7abc30
8 changed files with 2033 additions and 56 deletions
+47
View File
@@ -0,0 +1,47 @@
use config::{Config, ConfigError, File};
use lazy_static::lazy_static;
use log::error;
use serde::Deserialize;
// -----------------------------------------------------------
// Configuration structure
#[derive(Debug, Deserialize, Clone)]
pub struct Log {
pub level: String,
}
#[derive(Debug, Deserialize, Clone)]
pub struct Timings {
pub arp_sender_timeout: u64,
pub arp_scan_duration: u64,
}
#[derive(Debug, Deserialize, Clone)]
pub struct Settings {
pub log: Log,
pub timings: Timings,
}
// End configuration structure
// -----------------------------------------------------------
const CONFIG_FILE_PATH: &str = "./oott.toml";
impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let local_settings = Config::builder()
.add_source(File::with_name(CONFIG_FILE_PATH))
.build()?;
local_settings.try_deserialize()
}
}
lazy_static! {
pub static ref CONFIG: Settings = match Settings::new() {
Ok(value) => value,
Err(error) => {
error!("Error reading configuration file ({CONFIG_FILE_PATH}): {error}");
panic!("Error reading configuration file ({CONFIG_FILE_PATH}): {error}");
}
};
}