I have a route and an endpoint function defined. I've also injected some dependencies.
pub fn route1() -> BoxedFilter<(String, ParamType)> {
warp::get()
.and(warp::path::param())
.and(warp::filters::query::query())
.and(warp::path::end())
.boxed()
}
pub async fn handler1(
query: String,
param: ParamType,
dependency: DependencyType,
) -> Result<impl warp::Reply, warp::Rejection> {
}
let api = api::routes::route1()
.and(warp::any().map(move || dependency))
.and_then(api::hanlders::hander1);
This all seems to work fine.
However, I want to be able to have something that sits in front of several endpoints that checks for a valid key in the query parameter. Inside handler1
I can add:
if !param.key_valid {
return Ok(warp::reply::with_status(
warp::reply::json(&""),
StatusCode::BAD_REQUEST,
));
}
I do not want to add this to every handler individually.
It seems like I should be able to do it via filter
, but I can't figure it out. I've tried using .map()
but then returning multiple items shifts it to a tuple and I have to change my downstream function signature. Ideally I want to find a way to add verification or other filters that can reject the request without any downstream values knowing about them.
param
needed inside ofhandler1
once the validation has happened? – Valerievalerio