Struct axum::extract::OriginalUri
source · [−]pub struct OriginalUri(pub Uri);
original-uri
only.Expand description
Extractor that gets the original request URI regardless of nesting.
This is necessary since Uri
, when used as an extractor, will
have the prefix stripped if used in a nested service.
Example
use axum::{
routing::get,
Router,
extract::OriginalUri,
http::Uri
};
let api_routes = Router::new()
.route(
"/users",
get(|uri: Uri, OriginalUri(original_uri): OriginalUri| async {
// `uri` is `/users`
// `original_uri` is `/api/users`
}),
);
let app = Router::new().nest("/api", api_routes);
Extracting via request extensions
OriginalUri
can also be accessed from middleware via request extensions.
This is useful for example with Trace
to
create a span that contains the full path, if your service might be nested:
use axum::{
Router,
extract::OriginalUri,
http::Request,
routing::get,
};
use tower_http::trace::TraceLayer;
let api_routes = 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::<OriginalUri>() {
// This will include `/api`
path.0.path().to_owned()
} else {
// The `OriginalUri` extension will always be present if using
// `Router` unless another extractor or middleware has removed it
req.uri().path().to_owned()
};
tracing::info_span!("http-request", %path)
}),
);
let app = Router::new().nest("/api", api_routes);
Tuple Fields
0: Uri
Trait Implementations
sourceimpl Clone for OriginalUri
impl Clone for OriginalUri
sourcefn clone(&self) -> OriginalUri
fn clone(&self) -> OriginalUri
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for OriginalUri
impl Debug for OriginalUri
sourceimpl<S> FromRequestParts<S> for OriginalUri where
S: Send + Sync,
impl<S> FromRequestParts<S> for OriginalUri where
S: Send + Sync,
type Rejection = Infallible
type Rejection = Infallible
If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response. Read more
Auto Trait Implementations
impl RefUnwindSafe for OriginalUri
impl Send for OriginalUri
impl Sync for OriginalUri
impl Unpin for OriginalUri
impl UnwindSafe for OriginalUri
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<S, B, T> FromRequest<S, B, ViaParts> for T where
B: 'static + Send,
S: Send + Sync,
T: FromRequestParts<S>,
impl<S, B, T> FromRequest<S, B, ViaParts> for T where
B: 'static + Send,
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. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> PolicyExt for T where
T: ?Sized,
impl<T> PolicyExt for T where
T: ?Sized,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more