atuin/atuin-server/src/lib.rs

73 lines
1.8 KiB
Rust
Raw Normal View History

#![forbid(unsafe_code)]
use std::{future::Future, net::TcpListener};
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::example_config;
pub use settings::Settings;
use tokio::signal;
#[cfg(target_family = "unix")]
async fn shutdown_signal() {
let mut term = signal::unix::signal(signal::unix::SignalKind::terminate())
.expect("failed to register signal handler");
let mut interrupt = signal::unix::signal(signal::unix::SignalKind::interrupt())
.expect("failed to register signal handler");
tokio::select! {
_ = term.recv() => {},
_ = interrupt.recv() => {},
};
eprintln!("Shutting down gracefully...");
}
#[cfg(target_family = "windows")]
async fn shutdown_signal() {
signal::windows::ctrl_c()
.expect("failed to register signal handler")
.recv()
.await;
eprintln!("Shutting down gracefully...");
}
pub async fn launch<Db: Database>(
settings: Settings<Db::Settings>,
host: &str,
port: u16,
) -> Result<()> {
launch_with_listener::<Db>(
settings,
TcpListener::bind((host, port)).context("could not connect to socket")?,
shutdown_signal(),
)
.await
}
pub async fn launch_with_listener<Db: Database>(
settings: Settings<Db::Settings>,
listener: TcpListener,
shutdown: impl Future<Output = ()>,
) -> Result<()> {
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::from_tcp(listener)
.context("could not launch server")?
2022-04-12 16:06:19 -06:00
.serve(r.into_make_service())
.with_graceful_shutdown(shutdown)
2022-04-12 16:06:19 -06:00
.await?;
Ok(())
}