diff --git a/smart-house-web/backend/Cargo.toml b/smart-house-web/backend/Cargo.toml index 00ddd0c..fcb0467 100644 --- a/smart-house-web/backend/Cargo.toml +++ b/smart-house-web/backend/Cargo.toml @@ -7,4 +7,4 @@ edition = "2024" tracing = "0.1" tracing-subscriber = "0.3" axum = "0.8" -tokio = { version = "1.52", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1.52", features = ["rt", "rt-multi-thread", "signal"] } diff --git a/smart-house-web/backend/src/lib.rs b/smart-house-web/backend/src/lib.rs index 1036c99..b24503e 100644 --- a/smart-house-web/backend/src/lib.rs +++ b/smart-house-web/backend/src/lib.rs @@ -2,6 +2,7 @@ const CODE_LOGGER_INITIALIZATION_ERROR: i32 = 1; const CODE_TOKIO_RUNTIME_CREATION_ERROR: i32 = 2; const CODE_LISTENER_BINDING_ERROR: i32 = 3; const CODE_STARTIG_SERVER_ERROR: i32 = 4; +const CODE_CTRL_C_SIGNAL_INSTALL_ERROR: i32 = 5; pub fn init_logger() { use tracing_subscriber::{Layer, layer::SubscriberExt, util::SubscriberInitExt}; diff --git a/smart-house-web/backend/src/server.rs b/smart-house-web/backend/src/server.rs index 21df591..e4695bc 100644 --- a/smart-house-web/backend/src/server.rs +++ b/smart-house-web/backend/src/server.rs @@ -1,7 +1,7 @@ -pub fn run_server() { - use std::process::exit; - use tracing::{error, info}; +use std::process::exit; +use tracing::{error, info}; +pub fn run_server() { let runtime = match tokio::runtime::Builder::new_multi_thread() .worker_threads(2) .enable_all() @@ -40,4 +40,12 @@ async fn fallback() -> axum::response::Response { (axum::http::StatusCode::NOT_FOUND, "404 NOT FOUND").into_response() } -async fn shutdown_signal() {} +async fn shutdown_signal() { + let ctrl_c = match tokio::signal::ctrl_c().await { + Ok(signal) => signal, + Err(e) => { + error!("Can't install Ctrl+C signal handler: {:?}", e); + exit(crate::CODE_CTRL_C_SIGNAL_INSTALL_ERROR); + } + }; +}