first refactor

This commit is contained in:
2024-01-09 20:54:36 +01:00
parent a474164964
commit f071a702af
3 changed files with 106 additions and 112 deletions

27
axum_server/src/router.rs Normal file
View File

@ -0,0 +1,27 @@
use crate::post_parser::parse_post;
use axum::{extract::MatchedPath, http::Request, routing::get, Router};
use tower_http::trace::TraceLayer;
use tracing::info_span;
pub fn get_router() -> Router {
Router::new()
.route("/", get(|| async { "Hello, World!" }))
.route("/blog/:post_id", get(parse_post))
.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);
info_span!(
"http_request",
method = ?request.method(),
matched_path,
some_other_field = tracing::field::Empty,
)
}),
)
}