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
|
|
|
|
2023-06-12 02:04:35 -06:00
|
|
|
use atuin_server_database::Database;
|
2022-04-12 16:06:19 -06:00
|
|
|
use axum::Server;
|
|
|
|
use eyre::{Context, Result};
|
2021-04-20 10:07:11 -06:00
|
|
|
|
2023-06-12 02:04:35 -06:00
|
|
|
mod handlers;
|
|
|
|
mod router;
|
|
|
|
mod settings;
|
|
|
|
mod utils;
|
2021-04-20 10:07:11 -06:00
|
|
|
|
2023-06-12 02:04:35 -06:00
|
|
|
pub use settings::Settings;
|
2023-05-29 22:51:16 -06:00
|
|
|
use tokio::signal;
|
|
|
|
|
|
|
|
async fn shutdown_signal() {
|
2023-06-12 02:04:35 -06:00
|
|
|
signal::unix::signal(signal::unix::SignalKind::terminate())
|
|
|
|
.expect("failed to register signal handler")
|
|
|
|
.recv()
|
|
|
|
.await;
|
2023-05-29 22:51:16 -06:00
|
|
|
eprintln!("Shutting down gracefully...");
|
|
|
|
}
|
|
|
|
|
2023-06-12 02:04:35 -06:00
|
|
|
pub async fn launch<Db: Database>(
|
|
|
|
settings: Settings<Db::Settings>,
|
|
|
|
host: String,
|
|
|
|
port: u16,
|
|
|
|
) -> Result<()> {
|
2021-04-20 10:07:11 -06:00
|
|
|
let host = host.parse::<IpAddr>()?;
|
|
|
|
|
2023-06-12 02:04:35 -06:00
|
|
|
let db = Db::new(&settings.db_settings)
|
2022-04-12 16:06:19 -06:00
|
|
|
.await
|
2023-06-12 02:04:35 -06:00
|
|
|
.wrap_err_with(|| format!("failed to connect to db: {:?}", settings.db_settings))?;
|
2021-04-20 10:07:11 -06:00
|
|
|
|
2023-06-12 02:04:35 -06:00
|
|
|
let r = router::router(db, settings);
|
2022-04-12 16:06:19 -06:00
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|