40 lines
1.1 KiB
Rust
40 lines
1.1 KiB
Rust
use crate::routes::*;
|
|
use axum::{
|
|
Router,
|
|
extract::MatchedPath,
|
|
http::Request,
|
|
routing::{get, post},
|
|
};
|
|
use sqlx::PgPool;
|
|
use tokio::net::TcpListener;
|
|
use tower_http::trace::TraceLayer;
|
|
use uuid::Uuid;
|
|
|
|
pub async fn run(listener: TcpListener, connection_pool: PgPool) {
|
|
axum::serve(listener, app(connection_pool)).await.unwrap();
|
|
}
|
|
|
|
pub fn app(connection_pool: PgPool) -> Router {
|
|
Router::new()
|
|
.route("/health_check", get(health_check))
|
|
.route("/subscriptions", post(subscribe))
|
|
.layer(
|
|
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
|
let matched_path = request
|
|
.extensions()
|
|
.get::<MatchedPath>()
|
|
.map(MatchedPath::as_str);
|
|
let request_id = Uuid::new_v4().to_string();
|
|
|
|
tracing::info_span!(
|
|
"http_request",
|
|
method = ?request.method(),
|
|
matched_path,
|
|
request_id,
|
|
some_other_field = tracing::field::Empty,
|
|
)
|
|
}),
|
|
)
|
|
.with_state(connection_pool)
|
|
}
|