what2watch/src/main.rs

69 lines
1.7 KiB
Rust
Raw Normal View History

2023-05-30 00:17:23 +00:00
use std::net::SocketAddr;
2022-04-10 06:00:33 +00:00
use rand::{thread_rng, RngCore};
2023-07-10 18:15:24 +00:00
use tokio::signal;
2022-04-10 06:00:33 +00:00
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
2023-07-09 04:21:12 +00:00
use what2watch::get_db_pool;
2023-05-10 19:08:03 +00:00
fn main() {
2022-04-10 06:00:33 +00:00
tracing_subscriber::registry()
2023-04-26 05:19:49 +00:00
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
2023-07-09 04:21:12 +00:00
.unwrap_or_else(|_| "what2watch=debug,axum::routing=info".into()),
2023-04-26 05:19:49 +00:00
)
2022-04-10 06:00:33 +00:00
.with(tracing_subscriber::fmt::layer())
.init();
let pool = get_db_pool();
2023-05-29 00:55:16 +00:00
let rt = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap();
2023-05-29 00:55:16 +00:00
let secret = {
let mut bytes = [0u8; 64];
let mut rng = thread_rng();
2023-05-29 00:55:16 +00:00
rng.fill_bytes(&mut bytes);
bytes
};
let app = rt.block_on(what2watch::app(pool.clone(), &secret));
2023-04-26 05:19:49 +00:00
rt.block_on(async {
let addr: SocketAddr = ([0, 0, 0, 0], 3000).into();
tracing::debug!("binding to {addr:?}");
axum::Server::bind(&addr)
.serve(app)
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap_or_default();
});
rt.block_on(pool.close());
2023-07-10 18:15:24 +00:00
}
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;
};
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
println!(" signal received, starting graceful shutdown");
2022-04-10 06:00:33 +00:00
}