From fd942bb416a1c7b76f7f042da4df9c1fb8b484fb Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Fri, 10 May 2024 17:55:19 +0200 Subject: [PATCH] handle ctrl+c in containers --- Cargo.lock | 10 ++++++++++ little-hesinde/Cargo.toml | 2 +- little-hesinde/src/lib.rs | 15 ++++++++++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 652bb5f..970005b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1511,6 +1511,15 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1" +dependencies = [ + "libc", +] + [[package]] name = "siphasher" version = "0.3.11" @@ -1679,6 +1688,7 @@ dependencies = [ "mio", "num_cpus", "pin-project-lite", + "signal-hook-registry", "socket2", "tokio-macros", "windows-sys 0.48.0", diff --git a/little-hesinde/Cargo.toml b/little-hesinde/Cargo.toml index ef4fad8..4a10942 100644 --- a/little-hesinde/Cargo.toml +++ b/little-hesinde/Cargo.toml @@ -16,7 +16,7 @@ serde_with = "3.8.1" tera = "1.19.1" thiserror = { workspace = true } time = { workspace = true } -tokio = { version = "1.37.0", features = ["rt-multi-thread", "macros"] } +tokio = { version = "1.37.0", features = ["signal", "rt-multi-thread", "macros"] } tokio-util = "0.7.11" tracing = "0.1.40" tracing-subscriber = "0.3.18" diff --git a/little-hesinde/src/lib.rs b/little-hesinde/src/lib.rs index f2228aa..951b117 100644 --- a/little-hesinde/src/lib.rs +++ b/little-hesinde/src/lib.rs @@ -12,6 +12,8 @@ use poem::{ Route, Server, }; use rust_embed::RustEmbed; +use tokio::signal; +use tracing::info; pub mod app_state; pub mod cli; @@ -123,8 +125,15 @@ pub async fn run(config: Config) -> Result<(), std::io::Error> { .data(app_state) .with(Tracing); - Server::new(TcpListener::bind("[::]:3000")) + let server = Server::new(TcpListener::bind("[::]:3000")) .name("cops-web") - .run(app) - .await + .run(app); + + tokio::select! { + _ = server => {}, + _ = signal::ctrl_c() => { + info!("Received Ctrl+C, shutting down..."); + }, + } + Ok(()) }