mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 09:59:21 +02:00
rename async to a
This commit is contained in:
@ -2,7 +2,8 @@ use std::rc::Rc;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use handler::{Reply, RouteHandler};
|
||||
use resource::{Route, Resource};
|
||||
use route::Route;
|
||||
use resource::Resource;
|
||||
use recognizer::{RouteRecognizer, check_pattern};
|
||||
use httprequest::HttpRequest;
|
||||
use channel::HttpHandler;
|
||||
|
@ -55,6 +55,7 @@ mod encoding;
|
||||
mod httprequest;
|
||||
mod httpresponse;
|
||||
mod payload;
|
||||
mod route;
|
||||
mod resource;
|
||||
mod recognizer;
|
||||
mod handler;
|
||||
@ -84,7 +85,8 @@ pub use httprequest::{HttpRequest, UrlEncoded};
|
||||
pub use httpresponse::HttpResponse;
|
||||
pub use payload::{Payload, PayloadItem};
|
||||
pub use handler::{Reply, Json, FromRequest};
|
||||
pub use resource::{Route, Resource};
|
||||
pub use route::Route;
|
||||
pub use resource::Resource;
|
||||
pub use recognizer::Params;
|
||||
pub use server::HttpServer;
|
||||
pub use context::HttpContext;
|
||||
|
@ -1,96 +1,13 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use http::Method;
|
||||
use futures::Future;
|
||||
|
||||
use error::Error;
|
||||
use pred::{self, Predicate};
|
||||
use handler::{Reply, Handler, FromRequest, RouteHandler, AsyncHandler, WrapHandler};
|
||||
use route::Route;
|
||||
use handler::{Reply, Handler, RouteHandler, WrapHandler};
|
||||
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)
|
||||
}
|
||||
|
||||
/// 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 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,
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
/// Http resource
|
||||
///
|
||||
/// `Resource` is an entry in route table which corresponds to requested URL.
|
||||
/// *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)
|
||||
|
88
src/route.rs
Normal file
88
src/route.rs
Normal file
@ -0,0 +1,88 @@
|
||||
use http::Method;
|
||||
use futures::Future;
|
||||
|
||||
use error::Error;
|
||||
use pred::{self, Predicate};
|
||||
use handler::{Reply, Handler, FromRequest, RouteHandler, AsyncHandler, WrapHandler};
|
||||
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)
|
||||
}
|
||||
|
||||
/// 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 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,
|
||||
R: FromRequest + 'static,
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user