2021-05-09 14:17:24 -06:00
|
|
|
#![forbid(unsafe_code)]
|
|
|
|
|
2022-04-12 16:06:19 -06:00
|
|
|
use std::net::{IpAddr, SocketAddr};
|
2021-04-20 10:07:11 -06:00
|
|
|
|
2022-04-12 16:06:19 -06:00
|
|
|
use axum::Server;
|
|
|
|
use database::Postgres;
|
|
|
|
use eyre::{Context, Result};
|
2021-04-20 10:07:11 -06:00
|
|
|
|
|
|
|
use crate::settings::Settings;
|
|
|
|
|
2023-05-29 22:51:16 -06:00
|
|
|
use tokio::signal;
|
|
|
|
|
2021-04-20 10:07:11 -06:00
|
|
|
pub mod auth;
|
2022-04-13 11:29:18 -06:00
|
|
|
pub mod calendar;
|
2021-04-20 10:07:11 -06:00
|
|
|
pub mod database;
|
|
|
|
pub mod handlers;
|
|
|
|
pub mod models;
|
|
|
|
pub mod router;
|
2021-04-20 14:53:07 -06:00
|
|
|
pub mod settings;
|
2023-05-21 09:21:51 -06:00
|
|
|
pub mod utils;
|
2021-04-20 10:07:11 -06:00
|
|
|
|
2023-05-29 22:51:16 -06:00
|
|
|
async fn shutdown_signal() {
|
|
|
|
let terminate = async {
|
|
|
|
signal::unix::signal(signal::unix::SignalKind::terminate())
|
|
|
|
.expect("failed to register signal handler")
|
|
|
|
.recv()
|
|
|
|
.await;
|
|
|
|
};
|
|
|
|
|
|
|
|
tokio::select! {
|
|
|
|
_ = terminate => (),
|
|
|
|
}
|
|
|
|
eprintln!("Shutting down gracefully...");
|
|
|
|
}
|
|
|
|
|
2022-04-12 16:06:19 -06:00
|
|
|
pub async fn launch(settings: Settings, host: String, port: u16) -> Result<()> {
|
2021-04-20 10:07:11 -06:00
|
|
|
let host = host.parse::<IpAddr>()?;
|
|
|
|
|
2022-06-10 03:00:59 -06:00
|
|
|
let postgres = Postgres::new(settings.clone())
|
2022-04-12 16:06:19 -06:00
|
|
|
.await
|
|
|
|
.wrap_err_with(|| format!("failed to connect to db: {}", settings.db_uri))?;
|
2021-04-20 10:07:11 -06:00
|
|
|
|
2022-04-12 16:06:19 -06:00
|
|
|
let r = router::router(postgres, settings);
|
|
|
|
|
|
|
|
Server::bind(&SocketAddr::new(host, port))
|
|
|
|
.serve(r.into_make_service())
|
2023-05-29 22:51:16 -06:00
|
|
|
.with_graceful_shutdown(shutdown_signal())
|
2022-04-12 16:06:19 -06:00
|
|
|
.await?;
|
2021-04-20 10:07:11 -06:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|