-
Hi, I want to understand why this does not work the way it has been described in the page: My middleware pub async fn middleware(
State(state): State<AppState>,
session: Session,
mut request: Request,
next: Next,
) -> Response {
/* do something */
next.run(request).await
} my usage: .layer(axum::middleware::from_fn_with_state(
shared_state.clone(),
middleware
)) AppState: struct AppState {
snippets: Model
} #[derive(Clone)]
pub struct Model {
pool: Pool<MySql>,
} The error I get:
|
Beta Was this translation helpful? Give feedback.
Answered by
jplatte
May 18, 2025
Replies: 1 comment 9 replies
-
Have you tried https://docs.rs/axum/latest/axum/attr.debug_middleware.html? |
Beta Was this translation helpful? Give feedback.
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Aha! I think with that hint (that removing the state parameter fixes the issue) I've found your problem: The error message above refers to the type that isn't a valid middleware as
axum::middleware::FromFn<fn(State<AppState>, tower_sessions::Session, axum::http::Request<Body>, Next) -> impl Future<Output = Response<Body>> {middleware::middleware}, Arc<AppState>, Route, _>
. TheArc<AppState>
in there is a pretty clear sign that the second parameter you're passing tofrom_fn_with_state
is actually anArc<AppState>
, notAppState
.You'll have to either change your function signature to use
State<Arc<AppState>>
(ifAppState
itself is not cheap to clone, I would recommend this), or pass a clone…