use std::path::PathBuf;
use axum_server::tls_rustls::RustlsConfig;
use tokio::signal;
use tracing::info;
pub async fn load_tls_config(
cert_path: &str,
key_path: &str,
) -> Result<RustlsConfig, Box<dyn std::error::Error>> {
let cert = PathBuf::from(cert_path);
let key = PathBuf::from(key_path);
if !cert.exists() {
return Err(format!("Certificate file not found: {}", cert_path).into());
}
if !key.exists() {
return Err(format!("Key file not found: {}", key_path).into());
}
Ok(RustlsConfig::from_pem_file(cert, key).await?)
}
pub 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 => {},
}
info!("🛑 Shutting down gracefully...");
}