2017-10-07 06:48:14 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use http::Method;
|
2017-11-30 23:42:20 +01:00
|
|
|
use futures::Future;
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-11-28 21:28:51 +01:00
|
|
|
use error::Error;
|
2017-12-04 22:32:05 +01:00
|
|
|
use pred::{self, Predicate};
|
2017-12-03 01:37:21 +01:00
|
|
|
use route::{Reply, Handler, FromRequest, RouteHandler, AsyncHandler, WrapHandler};
|
2017-12-04 22:32:05 +01:00
|
|
|
use httpcodes::HTTPNotFound;
|
2017-10-15 07:52:38 +02:00
|
|
|
use httprequest::HttpRequest;
|
2017-11-30 23:42:20 +01:00
|
|
|
use httpresponse::HttpResponse;
|
2017-12-04 22:32:05 +01:00
|
|
|
|
|
|
|
/// Resource route definition. Route uses builder-like pattern for configuration.
|
|
|
|
pub struct Route<S> {
|
|
|
|
preds: Vec<Box<Predicate<S>>>,
|
|
|
|
handler: Box<RouteHandler<S>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> Default for Route<S> {
|
|
|
|
|
|
|
|
fn default() -> Route<S> {
|
|
|
|
Route {
|
|
|
|
preds: Vec::new(),
|
|
|
|
handler: Box::new(WrapHandler::new(|_| HTTPNotFound)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: 'static> Route<S> {
|
|
|
|
|
|
|
|
fn check(&self, req: &mut HttpRequest<S>) -> bool {
|
|
|
|
for pred in &self.preds {
|
|
|
|
if !pred.check(req) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle(&self, req: HttpRequest<S>) -> Reply {
|
|
|
|
self.handler.handle(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add match predicate to route.
|
|
|
|
pub fn p(&mut self, p: Box<Predicate<S>>) -> &mut Self {
|
|
|
|
self.preds.push(p);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add predicates to route.
|
|
|
|
pub fn predicates<P>(&mut self, preds: P) -> &mut Self
|
|
|
|
where P: IntoIterator<Item=Box<Predicate<S>>>
|
|
|
|
{
|
|
|
|
self.preds.extend(preds.into_iter());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Add method check to route. This method could be called multiple times.
|
|
|
|
pub fn method(&mut self, method: Method) -> &mut Self {
|
|
|
|
self.preds.push(pred::Method(method));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set handler function. Usually call to this method is last call
|
|
|
|
/// during route configuration, because it does not return reference to self.
|
|
|
|
pub fn handler<F, R>(&mut self, handler: F)
|
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
|
|
|
R: FromRequest + 'static,
|
|
|
|
{
|
|
|
|
self.handler = Box::new(WrapHandler::new(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set handler function.
|
|
|
|
pub fn async<F, R>(&mut self, handler: F)
|
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
|
|
|
R: Future<Item=HttpResponse, Error=Error> + 'static,
|
|
|
|
{
|
|
|
|
self.handler = Box::new(AsyncHandler::new(handler));
|
|
|
|
}
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-08 08:59:57 +02:00
|
|
|
/// Http resource
|
|
|
|
///
|
2017-10-08 23:56:51 +02:00
|
|
|
/// `Resource` is an entry in route table which corresponds to requested URL.
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
|
|
|
/// Resource in turn has at least one route.
|
2017-12-04 22:32:05 +01:00
|
|
|
/// 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.
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
2017-11-29 22:26:55 +01:00
|
|
|
/// ```rust
|
|
|
|
/// extern crate actix_web;
|
2017-12-04 22:32:05 +01:00
|
|
|
/// use actix_web::*;
|
2017-10-08 08:59:57 +02:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2017-12-04 22:32:05 +01:00
|
|
|
/// let app = Application::default("/")
|
|
|
|
/// .resource(
|
|
|
|
/// "/", |r| r.route().method(Method::GET).handler(|r| HttpResponse::Ok()))
|
2017-10-15 23:17:41 +02:00
|
|
|
/// .finish();
|
2017-10-08 08:59:57 +02:00
|
|
|
/// }
|
2017-10-08 23:56:51 +02:00
|
|
|
pub struct Resource<S=()> {
|
2017-10-17 04:21:24 +02:00
|
|
|
name: String,
|
2017-10-07 06:48:14 +02:00
|
|
|
state: PhantomData<S>,
|
2017-12-04 22:32:05 +01:00
|
|
|
routes: Vec<Route<S>>,
|
2017-10-07 06:48:14 +02:00
|
|
|
default: Box<RouteHandler<S>>,
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
impl<S> Default for Resource<S> {
|
2017-10-07 06:48:14 +02:00
|
|
|
fn default() -> Self {
|
2017-10-08 23:56:51 +02:00
|
|
|
Resource {
|
2017-10-17 04:21:24 +02:00
|
|
|
name: String::new(),
|
2017-10-07 06:48:14 +02:00
|
|
|
state: PhantomData,
|
2017-12-04 22:32:05 +01:00
|
|
|
routes: Vec::new(),
|
|
|
|
default: Box::new(HTTPNotFound)}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
impl<S> Resource<S> where S: 'static {
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-10-29 14:05:31 +01:00
|
|
|
pub(crate) fn default_not_found() -> Self {
|
|
|
|
Resource {
|
|
|
|
name: String::new(),
|
|
|
|
state: PhantomData,
|
2017-12-04 22:32:05 +01:00
|
|
|
routes: Vec::new(),
|
2017-10-29 14:05:31 +01:00
|
|
|
default: Box::new(HTTPNotFound)}
|
|
|
|
}
|
|
|
|
|
2017-10-17 04:21:24 +02:00
|
|
|
/// Set resource name
|
2017-12-03 01:37:21 +01:00
|
|
|
pub fn name<T: Into<String>>(&mut self, name: T) {
|
2017-11-27 02:30:35 +01:00
|
|
|
self.name = name.into();
|
2017-10-17 04:21:24 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
/// 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::default("/")
|
|
|
|
/// .resource(
|
|
|
|
/// "/", |r| r.route()
|
|
|
|
/// .p(pred::Any(vec![pred::Get(), pred::Put()]))
|
|
|
|
/// .p(pred::Header("Content-Type", "text/plain"))
|
|
|
|
/// .handler(|r| HttpResponse::Ok()))
|
|
|
|
/// .finish();
|
|
|
|
/// }
|
|
|
|
pub fn route(&mut self) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap()
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
/// Register a new route and add method check to route.
|
|
|
|
pub fn method(&mut self, method: Method) -> &mut Route<S> {
|
|
|
|
self.routes.push(Route::default());
|
|
|
|
self.routes.last_mut().unwrap().method(method)
|
2017-10-15 23:17:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-07 08:14:13 +02:00
|
|
|
/// Default handler is used if no matched route found.
|
2017-12-04 22:32:05 +01:00
|
|
|
/// By default `HTTPNotFound` is used.
|
|
|
|
pub fn default_handler<H>(&mut self, handler: H) where H: Handler<S> {
|
2017-11-29 22:26:55 +01:00
|
|
|
self.default = Box::new(WrapHandler::new(handler));
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-08 23:56:51 +02:00
|
|
|
impl<S: 'static> RouteHandler<S> for Resource<S> {
|
2017-10-07 06:48:14 +02:00
|
|
|
|
2017-12-04 22:32:05 +01:00
|
|
|
fn handle(&self, mut req: HttpRequest<S>) -> Reply {
|
|
|
|
for route in &self.routes {
|
|
|
|
if route.check(&mut req) {
|
|
|
|
return route.handle(req)
|
|
|
|
}
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
2017-12-04 22:32:05 +01:00
|
|
|
self.default.handle(req)
|
2017-10-07 06:48:14 +02:00
|
|
|
}
|
|
|
|
}
|