Implement shutdown signal in main.rs
This commit is contained in:
@@ -5,7 +5,7 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.8.4"
|
||||
tokio = { version = "1.47.1", features = ["full"] }
|
||||
tokio = { version = "1.47.1", features = ["full", "signal"] }
|
||||
sqlx = { version = "0.8.6", features = ["runtime-tokio-rustls", "sqlite", "chrono", "uuid"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
@@ -8,6 +8,7 @@ use axum::{routing::{delete, get, post, put}, Router};
|
||||
use routes::{admin, auth as auth_routes, machines, setup};
|
||||
use tower_http::{cors::CorsLayer, services::{ServeDir, ServeFile}};
|
||||
use std::path::Path;
|
||||
use tokio::signal;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
@@ -32,7 +33,7 @@ async fn main() -> Result<()> {
|
||||
|
||||
.with_state(pool);
|
||||
|
||||
let dist_path = "../dist";
|
||||
let dist_path = "./dist";
|
||||
let app = Router::new()
|
||||
.nest("/api", api_routes)
|
||||
.nest_service("/assets", ServeDir::new(format!("{}/assets", dist_path)))
|
||||
@@ -46,6 +47,35 @@ async fn main() -> Result<()> {
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:8379").await?;
|
||||
println!("Server running on http://0.0.0.0:8379");
|
||||
axum::serve(listener, app).await?;
|
||||
|
||||
axum::serve(listener, app).with_graceful_shutdown(shutdown_signal()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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 => {
|
||||
println!("\nShutting down due to Ctrl+C...");
|
||||
},
|
||||
_ = terminate => {
|
||||
println!("\nShutting down due to terminate signal...");
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user