1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-25 18:09:22 +02:00

add appl builder async method; add async handler section

This commit is contained in:
Nikolay Kim
2017-12-01 21:58:19 -08:00
parent e6feec62a8
commit c3a0a4457a
4 changed files with 56 additions and 9 deletions

View File

@ -1,10 +1,13 @@
use std::rc::Rc;
use std::collections::HashMap;
use futures::Future;
use route::{RouteHandler, WrapHandler, Reply, Handler};
use error::Error;
use route::{RouteHandler, Reply, Handler, WrapHandler, AsyncHandler};
use resource::Resource;
use recognizer::{RouteRecognizer, check_pattern};
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
use channel::HttpHandler;
use pipeline::Pipeline;
use middlewares::Middleware;
@ -204,6 +207,18 @@ impl<S> ApplicationBuilder<S> where S: 'static {
self
}
/// This method register async handler for specified path prefix.
/// Any path that starts with this prefix matches handler.
pub fn async<P, F, R>(&mut self, path: P, handler: F) -> &mut Self
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Future<Item=HttpResponse, Error=Error> + 'static,
P: Into<String>,
{
self.parts.as_mut().expect("Use after finish")
.handlers.insert(path.into(), Box::new(AsyncHandler::new(handler)));
self
}
/// Construct application
pub fn middleware<T>(&mut self, mw: T) -> &mut Self
where T: Middleware + 'static

View File

@ -5,7 +5,7 @@ use http::Method;
use futures::Future;
use error::Error;
use route::{Reply, RouteHandler, WrapHandler, Handler, StreamHandler};
use route::{Reply, Handler, RouteHandler, AsyncHandler, WrapHandler};
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed};
@ -70,7 +70,7 @@ impl<S> Resource<S> where S: 'static {
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Future<Item=HttpResponse, Error=Error> + 'static,
{
self.routes.insert(method, Box::new(StreamHandler::new(handler)));
self.routes.insert(method, Box::new(AsyncHandler::new(handler)));
}
/// Default handler is used if no matched route found.

View File

@ -154,7 +154,7 @@ impl<S, H, R> RouteHandler<S> for WrapHandler<S, H, R>
/// Async route handler
pub(crate)
struct StreamHandler<S, R, F>
struct AsyncHandler<S, R, F>
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Future<Item=HttpResponse, Error=Error> + 'static,
S: 'static,
@ -163,17 +163,17 @@ struct StreamHandler<S, R, F>
s: PhantomData<S>,
}
impl<S, R, F> StreamHandler<S, R, F>
impl<S, R, F> AsyncHandler<S, R, F>
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Future<Item=HttpResponse, Error=Error> + 'static,
S: 'static,
{
pub fn new(f: F) -> Self {
StreamHandler{f: Box::new(f), s: PhantomData}
AsyncHandler{f: Box::new(f), s: PhantomData}
}
}
impl<S, R, F> RouteHandler<S> for StreamHandler<S, R, F>
impl<S, R, F> RouteHandler<S> for AsyncHandler<S, R, F>
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Future<Item=HttpResponse, Error=Error> + 'static,
S: 'static,