The 'Extension' in the middleware is not working #3156
Answered
by
Turbo87
silent-rain
asked this question in
Q&A
-
Version Description // middleware
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for DemoService<S>
where
S: Service<Request<ReqBody>, Response = axum::http::Response<ResBody>> + Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Send + Sync + std::error::Error + Into<BoxError>,
ReqBody: Send + 'static,
ResBody: HttpBody<Data = Bytes> + Send + 'static + From<Body>,
ResBody::Error: Into<BoxError>,
{
type Response = S::Response;
type Error = S::Error;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, mut req: Request<ReqBody>) -> Self::Future {
let not_ready_inner = self.inner.clone();
let mut inner = std::mem::replace(&mut self.inner, not_ready_inner);
Box::pin(async move {
match req.extensions().get::<Extension<Arc<AppState>>>() { // is None
Some(_v) => (),
None => {
error!("get app state failed");
}
};
let _inject_provider = match req.extensions().get::<Extension<AInjectProvider>>() { // is None
Some(v) => &v.0,
None => {
error!("get app state failed");
}
};
let resp = inner.call(req).await?;
Ok(resp)
})
}
} // Router
let app = Router::new()
.nest("/api/v1", router::register())
.layer(Extension(app_config))
.layer(Extension(inject_provider))
.layer(Extension(state)); |
Beta Was this translation helpful? Give feedback.
Answered by
Turbo87
Jan 7, 2025
Replies: 1 comment 1 reply
-
I think you would need to |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
silent-rain
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you would need to
req.extensions().get::<Arc<AppState>>
, without theExtension<_>
wrapper