pub fn guard<G>(
guard_fn: G,
) -> impl Fn(Router<()>) -> Router<()> + Clone + Send + 'staticExpand description
Returns a Router -> Router transform that guards every request with a
predicate.
Returns 403 Forbidden if guard_fn(&request) returns false. The
predicate runs before any extractors, so it has access to headers, URI,
and method.
For authentication, prefer require_bearer — it handles the
Authorization: Bearer protocol correctly. guard is suited for
non-auth predicates (e.g., IP allowlists, feature flags, method
restrictions).
§Usage
Pass directly to .map() on the pipeline:
ⓘ
use rust_api::prelude::*;
RouterPipeline::new()
.mount::<MyController>(svc)
.map(guard(|req| is_allowed_ip(req)))
.build()?