fix(server): gracefully handle the hot-reloading errors

This commit is contained in:
Orhun Parmaksız 2021-11-16 19:27:18 +03:00
parent 27cfa6aca3
commit 8b17137c52
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
2 changed files with 20 additions and 10 deletions

View file

@ -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| {
let config_watcher = move |event: Event| {
if let Event::Write(path) = event {
match Config::parse(&path) {
Ok(config) => {
*cloned_config.lock().expect("cannot acquire config") = config;
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 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.

View file

@ -31,7 +31,9 @@ async fn serve(
file: web::Path<String>,
config: web::Data<Arc<Mutex<Config>>>,
) -> Result<HttpResponse, Error> {
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<String> = 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())?;