use std::marker::PhantomData; use http::Method; use route::Route; use handler::{Reply, Handler, FromRequest, RouteHandler, WrapHandler}; use httpcodes::HTTPNotFound; use httprequest::HttpRequest; /// *Resource* is an entry in route table which corresponds to requested URL. /// /// Resource in turn has at least one route. /// Route consists of an object that implements `Handler` trait (handler) /// and list of predicates (objects that implement `Predicate` trait). /// Route uses builder-like pattern for configuration. /// During request handling, resource object iterate through all routes /// and check all predicates for specific route, if request matches all predicates route /// route considired matched and route handler get called. /// /// ```rust /// # extern crate actix_web; /// use actix_web::*; /// /// fn main() { /// let app = Application::new("/") /// .resource( /// "/", |r| r.method(Method::GET).f(|r| HttpResponse::Ok())) /// .finish(); /// } pub struct Resource { name: String, state: PhantomData, routes: Vec>, default: Box>, } impl Default for Resource { fn default() -> Self { Resource { name: String::new(), state: PhantomData, routes: Vec::new(), default: Box::new(HTTPNotFound)} } } impl Resource where S: 'static { pub(crate) fn default_not_found() -> Self { Resource { name: String::new(), state: PhantomData, routes: Vec::new(), default: Box::new(HTTPNotFound)} } /// Set resource name pub fn name>(&mut self, name: T) { self.name = name.into(); } pub(crate) fn get_name(&self) -> Option { if self.name.is_empty() { None } else { Some(self.name.clone()) } } /// Register a new route and return mutable reference to *Route* object. /// *Route* is used for route configuration, i.e. adding predicates, setting up handler. /// /// ```rust /// # extern crate actix_web; /// use actix_web::*; /// /// fn main() { /// let app = Application::new("/") /// .resource( /// "/", |r| r.route() /// .p(pred::Any(vec![pred::Get(), pred::Put()])) /// .p(pred::Header("Content-Type", "text/plain")) /// .f(|r| HttpResponse::Ok())) /// .finish(); /// } /// ``` pub fn route(&mut self) -> &mut Route { self.routes.push(Route::default()); self.routes.last_mut().unwrap() } /// Register a new route and add method check to route. /// /// This is shortcut for: /// /// ```rust,ignore /// Resource::resource("/", |r| r.route().method(Method::GET).f(index) /// ``` pub fn method(&mut self, method: Method) -> &mut Route { self.routes.push(Route::default()); self.routes.last_mut().unwrap().method(method) } /// Register a new route and add handler object. /// /// This is shortcut for: /// /// ```rust,ignore /// Resource::resource("/", |r| r.route().h(handler) /// ``` pub fn h>(&mut self, handler: H) { self.routes.push(Route::default()); self.routes.last_mut().unwrap().h(handler) } /// Register a new route and add handler function. /// /// This is shortcut for: /// /// ```rust,ignore /// Resource::resource("/", |r| r.route().f(index) /// ``` pub fn f(&mut self, handler: F) where F: Fn(HttpRequest) -> R + 'static, R: FromRequest + 'static, { self.routes.push(Route::default()); self.routes.last_mut().unwrap().f(handler) } /// Default handler is used if no matched route found. /// By default `HTTPNotFound` is used. pub fn default_handler(&mut self, handler: H) where H: Handler { self.default = Box::new(WrapHandler::new(handler)); } } impl RouteHandler for Resource { fn handle(&self, mut req: HttpRequest) -> Reply { for route in &self.routes { if route.check(&mut req) { return route.handle(req) } } self.default.handle(req) } }