middleware::from_fn
fails if fn calls async fn with &Request
argument
#2571
-
Bug ReportVersionaxum v0.7.4 PlatformLinux work 6.7.2-1-default #1 SMP PREEMPT_DYNAMIC Fri Jan 26 11:01:28 UTC 2024 (a52bf76) x86_64 x86_64 x86_64 GNU/Linux Description
I tried this code: use axum::{
extract::Request,
http::StatusCode,
middleware::{self, Next},
response::Response,
Router,
};
async fn auth(request: Request, next: Next) -> Result<Response, StatusCode> {
get_token(&request).await;
todo!()
}
async fn get_token(request: &Request) {
todo!()
}
fn main() {
Router::<()>::new().route_layer(middleware::from_fn(auth));
} I got this compiler error:
The error can be avoided by either changing the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Passing So you either have to pass the request by value (ownership) or use |
Beta Was this translation helpful? Give feedback.
-
I am having a problem with this from_fn + this span after: .layer(
TraceLayer::new_for_http()
.make_span_with(|_request: Request<Body>| {
tracing::info_span!(
"user-data",
id = tracing::field::Empty,
int_id = tracing::field::Empty,
email = tracing::field::Empty,
banned = tracing::field::Empty
)
})
.on_request(|request: Request<Body>, span: Span| {
let maybe_user: Option<&User> = request.extensions().get();
if let Some(user) = maybe_user {
span.record("id", user.id.to_string().as_str());
span.record("int_id", user.int_id);
span.record("email", user.email.clone());
span.record("banned", user.banned);
}
}),
), The error disappears when removing the info
|
Beta Was this translation helpful? Give feedback.
Passing
&Request
creates a future that is!Send
(becauseRequest<axum::body::Body>
is!Sync
, whichRequest
is a typealias for).So you either have to pass the request by value (ownership) or use
request.into_parts()
and pass things separately.