Skip to content

Commit

Permalink
Merge pull request #22 from WISVCH/graceful-shutdown
Browse files Browse the repository at this point in the history
Add graceful shutdown to server
  • Loading branch information
JoepdeJong authored May 5, 2024
2 parents e46b3cc + f3e2f52 commit a1551e4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ axum = "0.7"
dotenv = "0.15"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.96"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread", "signal"] }
chrono = "0.4.31"
wallet-pass = "0.4.0"
33 changes: 32 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! Web service to create Apple Wallet passes.
use axum::{routing::get, Router};
use tokio::net::TcpListener;
use tokio::signal;

mod models;
mod passes;

extern crate dotenv;
use dotenv::dotenv;
use passes::passes_handler;

Expand All @@ -24,6 +26,35 @@ async fn main() {
.unwrap();

axum::serve(listener, app.into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap();
}

/// Graceful shutdown, useful for Docker containers.
///
/// Copied from the
/// [axum graceful-shutdown example](https://github.com/tokio-rs/axum/blob/87b86a7066c320cb388ad4d27f32e7092b56b52f/examples/graceful-shutdown/src/main.rs).
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 => {},
}
}

0 comments on commit a1551e4

Please sign in to comment.