aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 8bbb052e86131d2541921a626197e036b27c6847 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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 {
    pub protocol: u32,
    pub backend: SocketAddr,
    pub bind: SocketAddr,
    pub whitelist: Vec<PlayerConfig>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
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}"),
                    }
                }
            }
        }
    });
}