Я строю API Graphql с Rust и Warp.Я просмотрел документы, но до сих пор не понял, как связать фильтры, особенно для проверки заголовка запроса authorization
.
let context_extractor = warp::any()
// this code rejects all request which doesn't contain the authorization in header
// I'd like to make to check if authorization in header
.and(warp::header::<String>("authorization"))
.map(|token: String| -> Context {
let token_data = match verify_jwt(token) {
Ok(t) => t,
Err(_) => return Context { user_id: 0 },
};
Context {
user_id: token_data.claims.user_id,
}
});
let handle_request = move |context: Context,
request: juniper::http::GraphQLRequest|
-> Result<Vec<u8>, serde_json::Error> {
serde_json::to_vec(&request.execute(&schema, &context))
};
warp::post2()
.and(warp::path(path.into()))
.and(context_extractor)
.and(warp::body::json())
.map(handle_request)
.map(build_response)
.boxed()
Это моя часть кода.Работает нормально, но есть одна проблема.Я настроил один маршрут context_extractor
с .and(warp::header::<String>("authorization")
, затем он отклоняет все запросы, которые не содержат authorization
в заголовке.
Как мне сделать
если заголовок запроса имеет authorization
в заголовке, то вернуть Context
с правильными user_id
, если нет, вернуть Context
с user_id: 0
?