From 8b17137c524ef73c67db5b5c088f618d0234175f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Tue, 16 Nov 2021 19:27:18 +0300 Subject: [PATCH] fix(server): gracefully handle the hot-reloading errors --- src/main.rs | 22 ++++++++++++++-------- src/server.rs | 8 ++++++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 25c15498..6577b1d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,20 +38,26 @@ async fn main() -> IoResult<()> { .expect("failed to initialize configuration file watcher"); // Hot-reload the configuration file. - hotwatch - .watch(&config_path, move |event: Event| { - if let Event::Write(path) = event { - match Config::parse(&path) { - Ok(config) => { - *cloned_config.lock().expect("cannot acquire config") = config; + let config_watcher = move |event: Event| { + if let Event::Write(path) = event { + match Config::parse(&path) { + Ok(config) => match cloned_config.lock() { + Ok(mut cloned_config) => { + *cloned_config = config; log::info!("Configuration has been updated."); } Err(e) => { - log::error!("Failed to update configuration: {}", e); + log::error!("Failed to acquire configuration: {}", e); } + }, + Err(e) => { + log::error!("Failed to update configuration: {}", e); } } - }) + } + }; + hotwatch + .watch(&config_path, config_watcher) .unwrap_or_else(|_| panic!("failed to watch {:?}", config_path)); // Create a HTTP server. diff --git a/src/server.rs b/src/server.rs index e1eb403d..812c7430 100644 --- a/src/server.rs +++ b/src/server.rs @@ -31,7 +31,9 @@ async fn serve( file: web::Path, config: web::Data>>, ) -> Result { - let config = config.lock().expect("cannot acquire config"); + let config = config + .lock() + .map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?; let path = config.server.upload_path.join(&*file); let mut path = util::glob_match_file(path)?; let mut paste_type = PasteType::File; @@ -90,7 +92,9 @@ async fn upload( auth::check(host, request.headers(), env::var("AUTH_TOKEN").ok())?; let expiry_date = header::parse_expiry_date(request.headers())?; let mut urls: Vec = Vec::new(); - let config = config.lock().expect("cannot acquire config"); + let config = config + .lock() + .map_err(|_| error::ErrorInternalServerError("cannot acquire config"))?; while let Some(item) = payload.next().await { let mut field = item?; let content = ContentDisposition::try_from(field.content_disposition())?;