diff --git a/src/guard.rs b/src/guard.rs index 1632b997..f9565d0f 100644 --- a/src/guard.rs +++ b/src/guard.rs @@ -1,4 +1,30 @@ //! Route match guards. +//! +//! Guards are one of the way how actix-web router chooses +//! handler service. In essence it just function that accepts +//! reference to a `RequestHead` instance and returns boolean. +//! It is possible to add guards to *scopes*, *resources* +//! and *routes*. Actix provide several guards by default, like various +//! http methods, header, etc. To become a guard, type must implement `Guard` +//! trait. Simple functions coulds guards as well. +//! +//! Guard can not modify request object. But it is possible to +//! to store extra attributes on a request by using `Extensions` container. +//! Extensions container available via `RequestHead::extensions()` method. +//! +//! ```rust +//! use actix_web::{web, http, dev, guard, App, HttpResponse}; +//! +//! fn main() { +//! App::new().service(web::resource("/index.html").route( +//! web::route() +//! .guard(guard::Post()) +//! .guard(|head: &dev::RequestHead| head.method == http::Method::GET) +//! .to(|| HttpResponse::MethodNotAllowed())) +//! ); +//! } +//! ``` + #![allow(non_snake_case)] use actix_http::http::{self, header, HttpTryFrom}; use actix_http::RequestHead; @@ -13,6 +39,18 @@ pub trait Guard { fn check(&self, request: &RequestHead) -> bool; } +#[doc(hidden)] +pub struct FnGuard bool + 'static>(F); + +impl Guard for F +where + F: Fn(&RequestHead) -> bool + 'static, +{ + fn check(&self, head: &RequestHead) -> bool { + (*self)(head) + } +} + /// Return guard that matches if any of supplied guards. /// /// ```rust