Skip to main content

Module guard

Module guard 

Source
Expand description

Route match guards.

Guards are one of the ways how ntex web router chooses a handler service. In essence it is just a function that accepts a reference to a RequestHead instance and returns a boolean. It is possible to add guards to scopes, resources and routes. ntex provide several guards by default, like various http methods, header, etc. To become a guard, type must implement Guard trait. Simple functions can be used as guards as well, see fn_guard.

Multiple guards on the same route, resource, or scope must all match.

Guards can not modify the request object. But it is possible to store extra attributes on a request by using the Extensions container. Extensions containers are available via the RequestHead::extensions() method.

use ntex::web::{self, guard, App, HttpResponse};

fn main() {
    App::default().service(web::resource("/index.html").route(
        web::route()
             .guard(guard::Post())
             .guard(guard::fn_guard(|head| head.headers.contains_key("x-api-key")))
             .to(async || { HttpResponse::Ok() }))
    );
}

Structs§

AllGuard
Matches all of supplied guards..
AnyGuard
Matches any of supplied guards match.

Traits§

Guard
Trait defines resource guards. Guards are used for route selection.

Functions§

All
Return guard that matches if all of the supplied guards.
Any
Return guard that matches if any of supplied guards.
Connect
Predicate to match CONNECT http method
Delete
Predicate to match DELETE http method
Get
Guard to match GET http method
Head
Predicate to match HEAD http method
Header
Return predicate that matches if request contains specified header and value.
Host
Return predicate that matches if request contains specified Host name.
Method
Predicate to match specified http method
Not
Return guard that matches if supplied guard does not match.
Options
Predicate to match OPTIONS http method
Patch
Predicate to match PATCH http method
Post
Predicate to match POST http method
Put
Predicate to match PUT http method
Query
Predicate to match QUERY http method
Trace
Predicate to match TRACE http method
fn_guard
Create guard object for supplied function.