michalvankodev-site/axum_server/src/router.rs

30 lines
1.1 KiB
Rust
Raw Normal View History

2024-01-14 22:56:14 +01:00
use crate::pages::{index::render_index, post::render_post, post_list::render_post_list};
2024-01-09 20:54:36 +01:00
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()
2024-01-11 20:43:47 +01:00
.route("/", get(render_index))
2024-01-14 22:56:14 +01:00
.route("/blog", get(render_post_list))
2024-01-16 21:56:29 +01:00
.route("/blog/tags/:tag", get(render_post_list))
2024-01-11 22:16:38 +01:00
.route("/blog/:post_id", get(render_post))
2024-01-09 20:54:36 +01:00
.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,
)
}),
)
}