Skip to content

Commit

Permalink
fix(server): gracefully handle the hot-reloading errors
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Nov 16, 2021
1 parent 27cfa6a commit 8b17137
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
22 changes: 14 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())?;
Expand Down

0 comments on commit 8b17137

Please sign in to comment.