35 lines
943 B
Rust
35 lines
943 B
Rust
use crate::{config, context, database};
|
|
|
|
pub mod error;
|
|
pub mod middlware;
|
|
pub mod routes;
|
|
|
|
pub async fn run<D: database::Database + Clone + Send + Sync + 'static>(
|
|
context: context::Context<D>,
|
|
) -> anyhow::Result<()> {
|
|
let config = config::config();
|
|
|
|
let addr: std::net::SocketAddr = ([0, 0, 0, 0], config.port).into();
|
|
tracing::info!("Listening on {}", addr);
|
|
let listener = tokio::net::TcpListener::bind(addr).await?;
|
|
|
|
axum::serve(listener, router(context))
|
|
.with_graceful_shutdown(shutdown_signal())
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn router<D: database::Database + Clone + Send + Sync + 'static>(
|
|
context: context::Context<D>,
|
|
) -> axum::Router {
|
|
axum::Router::new()
|
|
.route("/login", axum::routing::post(routes::login))
|
|
.with_state(context)
|
|
.layer(axum::middleware::from_fn(middlware::response_map))
|
|
}
|
|
|
|
async fn shutdown_signal() {
|
|
_ = tokio::signal::ctrl_c().await;
|
|
}
|