I'm trying to write a middleware in Actix Web that will call an async function and, depending on the response, let the request flow as usual or return early.
Here's a simplified version of my code:
impl<S, B> Service for MyLittleMiddleware<S> ... {
...
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
fn call(&mut self, req: ServiceRequest) -> Self::Future {
Box::pin(async move {
// the async logic here
}
}
}
The issue is that I can't call self.service.call(req) from within the async block to allow the request flow as usual, because the reference to self in the function does not have a static lifetime.
I have tried to reduce the noise as much as possible, but no matter what I try, this limitation just sticks and does not let me go ahead. For example, if I move the call to self.service.call(req) outside the async block, it will move req with it and render the rest of the code unusable. It might be worth mentioning that the async function that I'm calling is in the application's data, hence the need to access the request from within the async block.
It would be great if we could return a response without having to mutate the request .
Since MyLittleError implements ResponseError. Now the compiler is compaining about the fact that I'm returning a value referencing data owned by the current function:
| / match a_result {
| | Ok(value) => {
| |
| | services.my_little_service.do_something(value);
| | -------- `services` is borrowed here
| |
| | Err(_) => Box::pin(ok(req.into_response(HttpResponse::NotFound().finish().into_body())))
| | }
| |_____^ returns a value referencing data owned by the current function