2017-12-05 01:09:22 +01:00
|
|
|
use futures::Future;
|
|
|
|
|
|
|
|
use error::Error;
|
2017-12-11 23:16:29 +01:00
|
|
|
use pred::Predicate;
|
2017-12-14 18:43:42 +01:00
|
|
|
use handler::{Reply, Handler, Responder, RouteHandler, AsyncHandler, WrapHandler};
|
2017-12-05 01:09:22 +01:00
|
|
|
use httpcodes::HTTPNotFound;
|
|
|
|
use httprequest::HttpRequest;
|
|
|
|
|
|
|
|
/// Resource route definition
|
|
|
|
///
|
|
|
|
/// Route uses builder-like pattern for configuration.
|
|
|
|
/// If handler is not explicitly set, default *404 Not Found* handler is used.
|
|
|
|
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> {
|
|
|
|
|
|
|
|
pub(crate) fn check(&self, req: &mut HttpRequest<S>) -> bool {
|
|
|
|
for pred in &self.preds {
|
|
|
|
if !pred.check(req) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2017-12-26 18:00:45 +01:00
|
|
|
pub(crate) fn handle(&mut self, req: HttpRequest<S>) -> Reply {
|
2017-12-05 01:09:22 +01:00
|
|
|
self.handler.handle(req)
|
|
|
|
}
|
|
|
|
|
2017-12-05 01:32:31 +01:00
|
|
|
/// Add match predicate to route.
|
2017-12-20 07:36:06 +01:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # extern crate actix_web;
|
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # use actix_web::httpcodes::*;
|
|
|
|
/// # fn main() {
|
|
|
|
/// Application::new()
|
|
|
|
/// .resource("/path", |r|
|
|
|
|
/// r.route()
|
|
|
|
/// .p(pred::Get())
|
|
|
|
/// .p(pred::Header("content-type", "text/plain"))
|
|
|
|
/// .f(|req| HTTPOk)
|
|
|
|
/// )
|
|
|
|
/// # .finish();
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2017-12-20 22:23:50 +01:00
|
|
|
pub fn p<T: Predicate<S> + 'static>(&mut self, p: T) -> &mut Self {
|
|
|
|
self.preds.push(Box::new(p));
|
2017-12-05 01:09:22 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set handler object. Usually call to this method is last call
|
|
|
|
/// during route configuration, because it does not return reference to self.
|
|
|
|
pub fn h<H: Handler<S>>(&mut self, handler: H) {
|
|
|
|
self.handler = Box::new(WrapHandler::new(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set handler function. Usually call to this method is last call
|
|
|
|
/// during route configuration, because it does not return reference to self.
|
|
|
|
pub fn f<F, R>(&mut self, handler: F)
|
|
|
|
where F: Fn(HttpRequest<S>) -> R + 'static,
|
2017-12-14 18:43:42 +01:00
|
|
|
R: Responder + 'static,
|
2017-12-05 01:09:22 +01:00
|
|
|
{
|
|
|
|
self.handler = Box::new(WrapHandler::new(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set async handler function.
|
2017-12-20 21:51:39 +01:00
|
|
|
pub fn a<H, R, F, E>(&mut self, handler: H)
|
|
|
|
where H: Fn(HttpRequest<S>) -> F + 'static,
|
|
|
|
F: Future<Item=R, Error=E> + 'static,
|
|
|
|
R: Responder + 'static,
|
|
|
|
E: Into<Error> + 'static
|
2017-12-05 01:09:22 +01:00
|
|
|
{
|
|
|
|
self.handler = Box::new(AsyncHandler::new(handler));
|
|
|
|
}
|
|
|
|
}
|