diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 16 | ||||
-rw-r--r-- | src/main.rs | 16 |
2 files changed, 25 insertions, 7 deletions
diff --git a/src/config.rs b/src/config.rs index be41420..9e7f720 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,12 +12,14 @@ use std::{ fs::read_to_string, marker::PhantomData, net::SocketAddr, - path::PathBuf, + path::{Path, PathBuf}, sync::Arc, }; #[derive(Debug, Serialize, Deserialize)] pub struct Config { + #[serde(default = "true_default")] + pub watch_config: bool, pub http: Option<HttpConfig>, pub https: Option<HttpsConfig>, #[serde(default)] @@ -26,6 +28,10 @@ pub struct Config { pub hosts: HashMap<String, Route>, } +fn true_default() -> bool { + true +} + #[derive(Debug, Serialize, Deserialize)] #[serde(default)] pub struct Limits { @@ -157,7 +163,8 @@ where } impl Config { - pub fn load(path: &str) -> anyhow::Result<Config> { + pub fn load(path: &Path) -> anyhow::Result<Config> { + info!("loading config from {path:?}"); let raw = read_to_string(path).context("reading config file")?; let config: Config = serde_yaml::from_str(&raw).context("parsing config")?; Ok(config) @@ -173,13 +180,13 @@ impl Default for Limits { } } -pub fn setup_file_watch(config_path: String, state: Arc<State>) { +pub fn setup_file_watch(config_path: PathBuf, state: Arc<State>) { std::thread::spawn(move || { let mut inotify = Inotify::init().unwrap(); inotify .watches() .add( - ".", + config_path.parent().unwrap(), WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE, ) .unwrap(); @@ -191,7 +198,6 @@ pub fn setup_file_watch(config_path: String, state: Arc<State>) { for event in events { if event.mask.contains(EventMask::MODIFY) { - info!("reloading config"); match Config::load(&config_path) { Ok(conf) => { let mut r = state.config.blocking_write(); diff --git a/src/main.rs b/src/main.rs index d11bfe1..807d0ab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,13 @@ use log::{debug, error, info, warn}; #[cfg(feature = "mond")] use reporting::Reporting; use std::{ - fs::File, io::BufReader, net::SocketAddr, ops::ControlFlow, path::Path, process::exit, + fs::File, + io::BufReader, + net::SocketAddr, + ops::ControlFlow, + path::{Path, PathBuf}, + process::exit, + str::FromStr, sync::Arc, }; use tokio::{ @@ -65,6 +71,10 @@ async fn main() -> anyhow::Result<()> { eprintln!("error: first argument is expected to be the configuration file"); exit(1) }; + let config_path = PathBuf::from_str(&config_path) + .unwrap() + .canonicalize() + .unwrap(); let config = match Config::load(&config_path) { Ok(c) => c, @@ -81,7 +91,9 @@ async fn main() -> anyhow::Result<()> { config: RwLock::new(Arc::new(config)), }); - setup_file_watch(config_path.to_owned(), state.clone()); + if state.config.read().await.watch_config { + setup_file_watch(config_path, state.clone()); + } { let state = state.clone(); |