Struct lexa_framework::extract::MatchedPath
source · pub struct MatchedPath(/* private fields */);Expand description
Access the path in the router that matches the request.
use axum::{
Router,
extract::MatchedPath,
routing::get,
};
let app = Router::new().route(
"/users/:id",
get(|path: MatchedPath| async move {
let path = path.as_str();
// `path` will be "/users/:id"
})
);Accessing MatchedPath via extensions
MatchedPath can also be accessed from middleware via request extensions.
This is useful for example with Trace to
create a span that contains the matched path:
use axum::{
Router,
extract::MatchedPath,
http::Request,
routing::get,
};
use tower_http::trace::TraceLayer;
let app = Router::new()
.route("/users/:id", get(|| async { /* ... */ }))
.layer(
TraceLayer::new_for_http().make_span_with(|req: &Request<_>| {
let path = if let Some(path) = req.extensions().get::<MatchedPath>() {
path.as_str()
} else {
req.uri().path()
};
tracing::info_span!("http-request", %path)
}),
);Matched path in nested routers
Because of how nesting works MatchedPath isn’t accessible in middleware on nested routes:
use axum::{
Router,
RequestExt,
routing::get,
extract::{MatchedPath, rejection::MatchedPathRejection},
middleware::map_request,
http::Request,
body::Body,
};
async fn access_matched_path(mut request: Request<Body>) -> Request<Body> {
// if `/foo/bar` is called this will be `Err(_)` since that matches
// a nested route
let matched_path: Result<MatchedPath, MatchedPathRejection> =
request.extract_parts::<MatchedPath>().await;
request
}
// `MatchedPath` is always accessible on handlers added via `Router::route`
async fn handler(matched_path: MatchedPath) {}
let app = Router::new()
.nest(
"/foo",
Router::new().route("/bar", get(handler)),
)
.layer(map_request(access_matched_path));Implementations§
Trait Implementations§
source§impl Clone for MatchedPath
impl Clone for MatchedPath
source§fn clone(&self) -> MatchedPath
fn clone(&self) -> MatchedPath
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moresource§impl Debug for MatchedPath
impl Debug for MatchedPath
source§impl<S> FromRequestParts<S> for MatchedPathwhere
S: Send + Sync,
impl<S> FromRequestParts<S> for MatchedPathwhere S: Send + Sync,
§type Rejection = MatchedPathRejection
type Rejection = MatchedPathRejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.
source§fn from_request_parts<'life0, 'life1, 'async_trait>(
parts: &'life0 mut Parts,
_state: &'life1 S
) -> Pin<Box<dyn Future<Output = Result<MatchedPath, <MatchedPath as FromRequestParts<S>>::Rejection>> + Send + 'async_trait, Global>>where
'life0: 'async_trait,
'life1: 'async_trait,
MatchedPath: 'async_trait,
fn from_request_parts<'life0, 'life1, 'async_trait>( parts: &'life0 mut Parts, _state: &'life1 S ) -> Pin<Box<dyn Future<Output = Result<MatchedPath, <MatchedPath as FromRequestParts<S>>::Rejection>> + Send + 'async_trait, Global>>where 'life0: 'async_trait, 'life1: 'async_trait, MatchedPath: 'async_trait,
Perform the extraction.
Auto Trait Implementations§
impl RefUnwindSafe for MatchedPath
impl Send for MatchedPath
impl Sync for MatchedPath
impl Unpin for MatchedPath
impl UnwindSafe for MatchedPath
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<S, B, T> FromRequest<S, B, ViaParts> for Twhere
B: Send + 'static,
S: Send + Sync,
T: FromRequestParts<S>,
impl<S, B, T> FromRequest<S, B, ViaParts> for Twhere B: Send + 'static, S: Send + Sync, T: FromRequestParts<S>,
§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.