diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 57 |
1 files changed, 19 insertions, 38 deletions
diff --git a/src/main.rs b/src/main.rs index 16006cb..47cec90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![feature(never_type)] + pub mod config; use anyhow::bail; @@ -12,7 +14,6 @@ use azalea_protocol::{ }; use bytes::BytesMut; use config::Config; -use inotify::{EventMask, Inotify, WatchMask}; use log::{error, info, warn}; use std::{ fs::read_to_string, @@ -26,50 +27,30 @@ use tokio::{ }, }; -#[tokio::main] -async fn main() { +fn main() { env_logger::builder() .filter_level(log::LevelFilter::Info) .parse_env("LOG") .init(); - - let config = Arc::new(RwLock::new(Arc::new( - serde_yaml::from_str::<Config>(&read_to_string("proxy.yaml").unwrap()).unwrap(), - ))); - - { - let config = config.clone(); - 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}"), - } - } - } + tokio::runtime::Builder::new_multi_thread() + .enable_all() + .build() + .unwrap() + .block_on(async move { + match run().await { + Ok(_) => {} + Err(err) => error!("fatal error: {err}"), } }); - } +} + +async fn run() -> anyhow::Result<!> { + let config = Arc::new(RwLock::new(Arc::new(serde_yaml::from_str::<Config>( + &read_to_string("proxy.yaml")?, + )?))); + config::watch(config.clone()); - let listener = TcpListener::bind(config.read().unwrap().bind) - .await - .unwrap(); + let listener = TcpListener::bind(config.read().unwrap().bind).await?; info!("listening"); loop { match listener.accept().await { |