I try to set up routes dynamically using Warp. I found another topic, which is almost what I try to do here but I still run into some issues.
Here is my code:
let (db, handler) = handlers.remove(0);
let init = access(handler.clone())
.or(callback(db.clone(), handler.clone()))
.or(authorize(db, handler))
.boxed();
let routes = handlers
.into_iter()
.map(|(db, handler)| {
access(handler.clone())
.or(callback(db.clone(), handler.clone()))
.or(authorize(db, handler))
.boxed()
})
.fold(init, |routes, route| routes.or(route).boxed());
warp::serve(routes.recover(handle_rejection))
.run(([127, 0, 0, 1], 8080))
.await;
Ok(())
The error I get is:
error[E0308]: mismatched types
--> src/main.rs:44:37
|
44 | .fold(init, |routes, route| routes.or(route).boxed());
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected opaque type, found tuple
|
::: src/oidc.rs:32:32
|
32 | ) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
| ----------------
| |
| one of the expected opaque types
| one of the found opaque types
...
47 | ) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
| ----------------
| |
| one of the expected opaque types
| one of the found opaque types
...
63 | ) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
| ----------------
| |
| one of the expected opaque types
| one of the found opaque types
|
= note: expected struct `BoxedFilter<(warp::generic::Either<(warp::generic::Either<impl Reply, impl Reply>,), impl Reply>,)>`
found struct `BoxedFilter<(warp::generic::Either<(warp::generic::Either<(warp::generic::Either<impl Reply, impl Reply>,), impl Reply>,), (warp::generic::Either<(warp::generic::Either<impl Reply, impl Reply>,), impl Reply>,)>,)>
I understand what it means, but don't know how to fix it because my filters all returns some impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone
futures and I can't recover early (before every .boxed()
) then returns the warp::Reply
because I want all the filters to be tested. I've thought about creating a flatmap Vec but I doubt it'll fix this issue. How can I achieve this?
Thank you for your help!