65 lines
1.6 KiB
Rust
65 lines
1.6 KiB
Rust
mod routes;
|
|
pub use routes::app;
|
|
|
|
use std::sync::mpsc;
|
|
|
|
use axum::{Router, serve::WithGracefulShutdown};
|
|
use tokio::signal;
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
|
|
pub fn run(
|
|
listener: tokio::net::TcpListener,
|
|
) -> Result<
|
|
WithGracefulShutdown<
|
|
tokio::net::TcpListener,
|
|
Router,
|
|
Router,
|
|
impl std::future::Future<Output = ()>,
|
|
>,
|
|
std::io::Error,
|
|
> {
|
|
tracing_subscriber::registry()
|
|
.with(
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
|
.unwrap_or_else(|_| format!("{}=debug", env!("CARGO_CRATE_NAME")).into()),
|
|
)
|
|
.with(tracing_subscriber::fmt::layer())
|
|
.init();
|
|
|
|
// build our application with some routes
|
|
let (app, rx) = routes::app();
|
|
|
|
// run it
|
|
tracing::debug!("listening on {}", listener.local_addr()?);
|
|
let server = axum::serve(listener, app);
|
|
Ok(server.with_graceful_shutdown(shutdown_signal(rx)))
|
|
}
|
|
|
|
async fn shutdown_signal(rx: mpsc::Receiver<bool>) {
|
|
let ctrl_c = async {
|
|
signal::ctrl_c()
|
|
.await
|
|
.expect("failed to install Ctrl+C handler");
|
|
};
|
|
let endpoint = tokio::spawn(async move {
|
|
let _ = rx.recv();
|
|
});
|
|
|
|
#[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 => {},
|
|
_ = endpoint => {},
|
|
}
|
|
}
|