aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2022-10-17 11:28:03 +0200
committermetamuffin <metamuffin@disroot.org>2022-10-17 11:28:03 +0200
commit6e50a340ca698ed94c11cfb156e3f5e498807e4d (patch)
tree75f680108e488572439b05359059cdf3fa6d450c /src/config.rs
parentb739641a78bad5381aa2a8f7de3d464f9c5ce01f (diff)
downloadtrash-proxy-6e50a340ca698ed94c11cfb156e3f5e498807e4d.tar
trash-proxy-6e50a340ca698ed94c11cfb156e3f5e498807e4d.tar.bz2
trash-proxy-6e50a340ca698ed94c11cfb156e3f5e498807e4d.tar.zst
clean
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs33
1 files changed, 31 insertions, 2 deletions
diff --git a/src/config.rs b/src/config.rs
index b3d4e31..8bbb052 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,6 +1,7 @@
-use std::net::SocketAddr;
-
+use inotify::{EventMask, Inotify, WatchMask};
+use log::{error, info};
use serde::{Deserialize, Serialize};
+use std::{fs::read_to_string, net::SocketAddr, sync::{Arc, RwLock}};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
@@ -15,3 +16,31 @@ pub struct PlayerConfig {
pub token: Option<String>,
pub username: String,
}
+
+pub fn watch(config: Arc<RwLock<Arc<Config>>>) {
+ std::thread::spawn(move || {
+ let mut inotify = Inotify::init().unwrap();
+ inotify
+ .add_watch(
+ ".",
+ 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 serde_yaml::from_str::<Config>(&read_to_string("proxy.yaml").unwrap()) {
+ Ok(conf) => *config.write().unwrap() = Arc::new(conf),
+ Err(e) => error!("config has errors: {e}"),
+ }
+ }
+ }
+ }
+ });
+}