2024-05-12 14:38:03 +02:00
|
|
|
use std::net::SocketAddr;
|
2024-05-11 15:50:24 +02:00
|
|
|
|
|
|
|
use axum::{extract::MatchedPath, http::Request, routing::get, Router};
|
|
|
|
use tower_http::trace::TraceLayer;
|
|
|
|
|
2024-05-12 14:38:03 +02:00
|
|
|
use crate::state::SharedState;
|
2024-05-11 15:50:24 +02:00
|
|
|
|
|
|
|
async fn root() -> &'static str {
|
|
|
|
"Hello, hyperlog!"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn serve(state: &SharedState, host: &SocketAddr) -> anyhow::Result<()> {
|
|
|
|
let app = Router::new()
|
|
|
|
.route("/", get(root))
|
|
|
|
.with_state(state.clone())
|
|
|
|
.layer(
|
|
|
|
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
|
|
|
// Log the matched route's path (with placeholders not filled in).
|
|
|
|
// Use request.uri() or OriginalUri if you want the real path.
|
|
|
|
let matched_path = request
|
|
|
|
.extensions()
|
|
|
|
.get::<MatchedPath>()
|
|
|
|
.map(MatchedPath::as_str);
|
|
|
|
|
|
|
|
tracing::info_span!(
|
|
|
|
"http_request",
|
|
|
|
method = ?request.method(),
|
|
|
|
matched_path,
|
|
|
|
some_other_field = tracing::field::Empty,
|
|
|
|
)
|
|
|
|
}), // ...
|
|
|
|
);
|
|
|
|
|
|
|
|
tracing::info!("listening on {}", host);
|
|
|
|
let listener = tokio::net::TcpListener::bind(host).await.unwrap();
|
|
|
|
axum::serve(listener, app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
Ok(())
|
|
|
|
}
|