mirror of
https://github.com/amigan/rustypaste-pretty.git
synced 2025-01-31 13:02:43 -05:00
fix(server): gracefully handle the hot-reloading errors
This commit is contained in:
parent
27cfa6aca3
commit
8b17137c52
2 changed files with 20 additions and 10 deletions
22
src/main.rs
22
src/main.rs
|
@ -38,20 +38,26 @@ async fn main() -> IoResult<()> {
|
||||||
.expect("failed to initialize configuration file watcher");
|
.expect("failed to initialize configuration file watcher");
|
||||||
|
|
||||||
// Hot-reload the configuration file.
|
// Hot-reload the configuration file.
|
||||||
hotwatch
|
let config_watcher = move |event: Event| {
|
||||||
.watch(&config_path, move |event: Event| {
|
if let Event::Write(path) = event {
|
||||||
if let Event::Write(path) = event {
|
match Config::parse(&path) {
|
||||||
match Config::parse(&path) {
|
Ok(config) => match cloned_config.lock() {
|
||||||
Ok(config) => {
|
Ok(mut cloned_config) => {
|
||||||
*cloned_config.lock().expect("cannot acquire config") = config;
|
*cloned_config = config;
|
||||||
log::info!("Configuration has been updated.");
|
log::info!("Configuration has been updated.");
|
||||||
}
|
}
|
||||||
Err(e) => {
|
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));
|
.unwrap_or_else(|_| panic!("failed to watch {:?}", config_path));
|
||||||
|
|
||||||
// Create a HTTP server.
|
// Create a HTTP server.
|
||||||
|
|
|
@ -31,7 +31,9 @@ async fn serve(
|
||||||
file: web::Path<String>,
|
file: web::Path<String>,
|
||||||
config: web::Data<Arc<Mutex<Config>>>,
|
config: web::Data<Arc<Mutex<Config>>>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> 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 path = config.server.upload_path.join(&*file);
|
||||||
let mut path = util::glob_match_file(path)?;
|
let mut path = util::glob_match_file(path)?;
|
||||||
let mut paste_type = PasteType::File;
|
let mut paste_type = PasteType::File;
|
||||||
|
@ -90,7 +92,9 @@ async fn upload(
|
||||||
auth::check(host, request.headers(), env::var("AUTH_TOKEN").ok())?;
|
auth::check(host, request.headers(), env::var("AUTH_TOKEN").ok())?;
|
||||||
let expiry_date = header::parse_expiry_date(request.headers())?;
|
let expiry_date = header::parse_expiry_date(request.headers())?;
|
||||||
let mut urls: Vec<String> = Vec::new();
|
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 {
|
while let Some(item) = payload.next().await {
|
||||||
let mut field = item?;
|
let mut field = item?;
|
||||||
let content = ContentDisposition::try_from(field.content_disposition())?;
|
let content = ContentDisposition::try_from(field.content_disposition())?;
|
||||||
|
|
Loading…
Reference in a new issue