Basically, this is what I'd like to do (of course it doesn't work like that, but still):
App::new()
// ... .app_data() calls
.wrap_fn(|req: ServiceRequest, srv /* : &AppRouting */ | async {
let path = req.path().to_string();
let asset: Option<HttpResponse> = http::admin::frontend_assets(path.clone()); // That's my code, the one that checks whether the resource exists, and if it does, we get the associated HTTP response with proper Content-Type
if asset.is_some() {
println!("response: asset exists");
let r: ServiceResponse = req.into_response(asset.unwrap());
return Ok(r);
}
println!("response: asset does not exist");
let r: ServiceResponse = srv.call(req).await.unwrap();
Ok(r)
})
// API routes
With the Rust-complex-typing-system newbie I am, compiling this code triggers a lifetime error that I have ever seen and I don't understand
error: lifetime may not live long enough
--> src\serve\mod.rs:95:70
|
95 | .wrap_fn(|req: ServiceRequest, srv /* : &AppRouting */ | async {
| ____________________________________________---_____________________-_^
| | | |
| | | return type of closure `[async block@src\serve\mod.rs:95:70: 108:14]` contains a lifetime `'2`
| | has type `&'1 actix_web::app_service::AppRouting`
96 | | let path = req.path().to_string();
97 | | let asset: Option<HttpResponse> = http::admin::frontend_assets(path.clone());
98 | |
... |
107 | | Ok(r)
108 | | })
| |_____________^ returning this value requires that `'1` must outlive `'2`