这就是我想要实现的目标:
我的端点格式为
/entity1/<id>/entity2/<id>/stuff
,即:
let route = warp::path("entity1")
.and(warp::path::param::<u64>())
.and(warp::path("entity2"))
.and(warp::path::param::<u64>())
.and(warp::path("stuff"))
.and(warp::path::end())
.and(with_auth(db.clone())) // Authenticates user using Authorization header
.and_then(handler_function);
我有几个类似的端点,它们从数据库加载实体2,如果与实体1不匹配则拒绝请求,然后填充加载的实体。我的想法是创建一个过滤器,从数据库加载实体并将其传递给处理函数(并进行一些验证,最终拒绝请求),使用类似
.and(with_entity2(db.clone()))
和 的内容
pub fn with_entity2(db: &Db) -> impl Filter<Extract = (Entity2,), Error = warp::Rejection> + Clone {
// Get path params here?
}
我想我可以使用
FullPath
,但需要自己解析它。
是否可以获取路径参数?或者有更好的选择吗?我不再需要提取的参数,但我不想在用户经过身份验证并且整个路径匹配之前进行数据库查询。
warp todos的示例向您展示了一种做您想做的事情的方法:
fn with_db(db: Db) -> impl Filter<Extract = (Db,), Error = std::convert::Infallible> + Clone {
warp::any().map(move || db.clone())
}
像你写的那样使用,但没有克隆
.and(with_db(db))
。