1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 13:29:24 +00:00
actix-web/src/route.rs

74 lines
2 KiB
Rust
Raw Normal View History

2017-12-05 00:09:22 +00:00
use futures::Future;
use error::Error;
use pred::Predicate;
2017-12-14 17:43:42 +00:00
use handler::{Reply, Handler, Responder, RouteHandler, AsyncHandler, WrapHandler};
2017-12-05 00:09:22 +00:00
use httpcodes::HTTPNotFound;
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
/// 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
}
pub(crate) fn handle(&self, req: HttpRequest<S>) -> Reply {
self.handler.handle(req)
}
2017-12-05 00:32:31 +00:00
/// Add match predicate to route.
pub fn p(&mut self, p: Box<Predicate<S>>) -> &mut Self {
self.preds.push(p);
2017-12-05 00:09:22 +00: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 17:43:42 +00:00
R: Responder + 'static,
2017-12-05 00:09:22 +00:00
{
self.handler = Box::new(WrapHandler::new(handler));
}
/// Set async handler function.
pub fn a<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));
}
}