Skip to content

Commit

Permalink
axum with server graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
gerardcl committed Dec 30, 2023
1 parent fe57fc4 commit 6cd6a08
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion be-rust-axum/rust-template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0"
[dependencies]
# one can add more dependencies via cargo to Cargo.toml as shown next: cargo add axum -F axum/http2
axum = { version = "0.7", features = ["http2"] }
tokio = { version = "1.35", features = ["rt-multi-thread", "macros"] }
tokio = { version = "1.35", features = ["rt-multi-thread", "macros", "signal"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
http = "1.0"
Expand Down
38 changes: 30 additions & 8 deletions be-rust-axum/rust-template/src/api/router.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use axum::{Json, Router};
use http::StatusCode;
use std::net::SocketAddr;
use tokio::signal::unix::{signal, SignalKind};
use tracing::info;

use crate::api::routes;
Expand All @@ -17,6 +18,17 @@ pub fn app() -> Router {
.fallback(fallback)
}

/// The serve function that starts the Axum server
pub async fn serve(address: SocketAddr) {
let listener = tokio::net::TcpListener::bind(address).await.unwrap();

info!("Server listening on {}", &address);
axum::serve(listener, app().into_make_service())
.with_graceful_shutdown(shutdown_signal())
.await
.expect("Failed to start server");
}

/// The fallback function when a non configured endpoint is reached
async fn fallback() -> (StatusCode, Json<Status>) {
(
Expand All @@ -27,12 +39,22 @@ async fn fallback() -> (StatusCode, Json<Status>) {
)
}

/// The serve function that starts the Axum server
pub async fn serve(address: SocketAddr) {
let listener = tokio::net::TcpListener::bind(address).await.unwrap();

info!("Server listening on {}", &address);
axum::serve(listener, app().into_make_service())
.await
.expect("Failed to start server");
/// Setup example of the OS signal handlers to catch for proper server graceful shutdown.
async fn shutdown_signal() {
let interrupt = async {
signal(SignalKind::interrupt())
.expect("failed to install signal handler")
.recv()
.await;
};
let terminate = async {
signal(SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
tokio::select! {
_ = interrupt => {},
_ = terminate => {},
}
}

0 comments on commit 6cd6a08

Please sign in to comment.