aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/config.rs b/src/config.rs
index a7abf3b..be41420 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,4 +1,7 @@
+use crate::State;
use anyhow::Context;
+use inotify::{EventMask, Inotify, WatchMask};
+use log::{error, info};
use serde::{
de::{value, Error, SeqAccess, Visitor},
Deserialize, Deserializer, Serialize,
@@ -10,6 +13,7 @@ use std::{
marker::PhantomData,
net::SocketAddr,
path::PathBuf,
+ sync::Arc,
};
#[derive(Debug, Serialize, Deserialize)]
@@ -168,3 +172,35 @@ impl Default for Limits {
}
}
}
+
+pub fn setup_file_watch(config_path: String, state: Arc<State>) {
+ std::thread::spawn(move || {
+ let mut inotify = Inotify::init().unwrap();
+ inotify
+ .watches()
+ .add(
+ ".",
+ WatchMask::MODIFY | WatchMask::CREATE | WatchMask::DELETE,
+ )
+ .unwrap();
+ let mut buffer = [0u8; 4096];
+ loop {
+ let events = inotify
+ .read_events_blocking(&mut buffer)
+ .expect("Failed to read inotify events");
+
+ 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();
+ *r = Arc::new(conf)
+ }
+ Err(e) => error!("config has errors: {e}"),
+ }
+ }
+ }
+ }
+ });
+}