#How to use wrap_fn without calling `srv.call(req)`?

1 messages · Page 1 of 1 (latest)

edgy bough
#

I've made my own quick-and-dirty auth middleware, and obviously when users aren't authenticated I don't want the service to continue executing, and instead a 401 response should go out immediately. However, I can't figure out how to make it work with the compiler.

Here's two constructions I've tried (I've also tried a few other permutations of the below to no avail):

This one, despite the abomination on the second line, still doesn't quite match the return type from call

.wrap_fn(|serv_req, service| {
    if !check_auth(serv_req.request()){ return Box::pin(std::future::ready(Err(error::ErrorUnauthorized("Unauthorized")))); }
    service.call(serv_req)
})

In this one, the types seem fine, but the async block has a longer lifetime than the function, so it doesn't like the variables being used inside of it.

.wrap_fn(|serv_req, service| async {
    if check_auth(serv_req.request()) {
        service.call(serv_req).await
    }
    else {
        Err(error::ErrorUnauthorized("Unauthorized"))
    }
})
amber lily
#

would strongly recommend using from_fn instead of wrap_fn