diff --git a/src/web/mod.rs b/src/web/mod.rs index 5bf92d20..08046558 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -18,7 +18,10 @@ use axum::{ }; use futures::{sink::SinkExt, stream::StreamExt}; use serde::Deserialize; -use tokio::sync::{broadcast, mpsc, RwLock}; +use tokio::{ + signal, + sync::{broadcast, mpsc, RwLock}, +}; use tracing::*; use uuid::Uuid; @@ -227,9 +230,16 @@ pub async fn run(address: String) { continue; } }; - if let Err(error) = axum::serve(listener, router.clone()).into_future().await { - error!("WebServer error: {}", error); + + if let Err(error) = axum::serve(listener, router.clone()) + .with_graceful_shutdown(shutdown_signal()) + .await + { + error!("WebServer error: {error}"); + continue; } + + break; } } @@ -240,3 +250,27 @@ where let mut router = SERVER.router.lock().unwrap(); modifier(&mut router); } + +async fn shutdown_signal() { + let ctrl_c = async { + signal::ctrl_c() + .await + .expect("failed to install Ctrl+C handler"); + }; + + #[cfg(unix)] + let terminate = async { + signal::unix::signal(signal::unix::SignalKind::terminate()) + .expect("failed to install signal handler") + .recv() + .await; + }; + + #[cfg(not(unix))] + let terminate = std::future::pending::<()>(); + + tokio::select! { + _ = ctrl_c => {}, + _ = terminate => {}, + } +}