Skip to content

Commit 8bcd651

Browse files
authored
feat: Handle SIGINT/SIGTERM (#92)
1 parent 7c0aff8 commit 8bcd651

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/main.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,40 @@ async fn main() {
3838

3939
let addr: SocketAddr = cfg.address().unwrap();
4040
log::info!("Running flagpole on {:?}", addr);
41-
axum::Server::bind(&addr).serve(router.into_make_service()).await.unwrap();
41+
42+
axum::Server::bind(&addr)
43+
.serve(router.into_make_service())
44+
.with_graceful_shutdown(shutdown_signal())
45+
.await
46+
.unwrap();
47+
48+
log::info!("Server shutdown complete");
49+
}
50+
51+
async fn shutdown_signal() {
52+
let ctrl_c = async {
53+
tokio::signal::ctrl_c().await.expect("failed to install CTRL+C signal handler");
54+
};
55+
56+
#[cfg(unix)]
57+
let terminate = async {
58+
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
59+
.expect("failed to install SIGTERM signal handler")
60+
.recv()
61+
.await;
62+
};
63+
64+
#[cfg(not(unix))]
65+
let terminate = std::future::pending::<()>();
66+
67+
tokio::select! {
68+
_ = ctrl_c => {
69+
log::info!("Received SIGINT (CTRL+C), initiating graceful shutdown");
70+
},
71+
_ = terminate => {
72+
log::info!("Received SIGTERM, initiating graceful shutdown");
73+
},
74+
}
4275
}
4376

4477
#[derive(Clone)]

0 commit comments

Comments
 (0)