How do I intercept request, extract relavent info and forward the original request with a custom object? #2101
-
I have a pub(crate) struct CustomSession {
inner: SessionAuth<...<...<...<FetchSession>>>,
}
impl Service<ApiRequest> for CustomSession {
type Response = UserSession;
type Error = FetchSessionError;
type Future = BoxFuture<Self::Response, Self::Error>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
...
}
fn call(&mut self, api_req: ApiRequest) -> Self::Future {
self.inner.call(api_req)
}
#[derive(Debug, Clone)]
pub(crate) struct ApiRequest {
pub(crate) scheme: Option<String>,
pub(crate) remote_addr: Option<String>,
pub(crate) user_agent: Option<String>,
pub(crate) api_key: Option<String>,
} Based on this req, each layer in inner: SessionAuth<...<...<...<FetchSession>>>, contacts a backend database and either retrieves or verifies some information and finally return a I want to be able to use this pub(crate) async fn user_fetch(
fetch_svc: State<Fetch>, // Service to fetch user info.
UserSession(session): UserSession, // <--- This here
Json(user_id): Json<UserId>, // Unique user Id.
) -> impl IntoResponse {
let user = fetch_svc
... // arc/mutex magic
...
.call(IdentifiableUser { user_id })
...
...
.await?;
Ok(user)
} Accesssing this path without Is this possible? And if so, I am not sure what to look for. I think I'll need to refactor Also not sure how I'd extract Query(api_key): Query<ApiKey> where |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
In your middleware you’ll wanna add additional data as request extensions and extract those in the handler. See https://docs.rs/axum/latest/axum/middleware/index.html#passing-state-from-middleware-to-handlers |
Beta Was this translation helpful? Give feedback.
In your middleware you’ll wanna add additional data as request extensions and extract those in the handler. See https://docs.rs/axum/latest/axum/middleware/index.html#passing-state-from-middleware-to-handlers