atuin/atuin-server/src/lib.rs

45 lines
1,001 B
Rust
Raw Normal View History

#![forbid(unsafe_code)]
2022-04-12 16:06:19 -06:00
use std::net::{IpAddr, SocketAddr};
use atuin_server_database::Database;
2022-04-12 16:06:19 -06:00
use axum::Server;
use eyre::{Context, Result};
mod handlers;
mod router;
mod settings;
mod utils;
pub use settings::Settings;
use tokio::signal;
async fn shutdown_signal() {
signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to register signal handler")
.recv()
.await;
eprintln!("Shutting down gracefully...");
}
pub async fn launch<Db: Database>(
settings: Settings<Db::Settings>,
host: String,
port: u16,
) -> Result<()> {
let host = host.parse::<IpAddr>()?;
let db = Db::new(&settings.db_settings)
2022-04-12 16:06:19 -06:00
.await
.wrap_err_with(|| format!("failed to connect to db: {:?}", settings.db_settings))?;
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())
.with_graceful_shutdown(shutdown_signal())
2022-04-12 16:06:19 -06:00
.await?;
Ok(())
}