2019-03-03 09:57:48 +01:00
|
|
|
use std::cell::RefCell;
|
2019-03-05 19:08:08 +01:00
|
|
|
use std::marker::PhantomData;
|
2018-01-10 05:00:18 +01:00
|
|
|
use std::rc::Rc;
|
2017-12-05 01:09:22 +01:00
|
|
|
|
2019-03-03 09:57:48 +01:00
|
|
|
use actix_http::{http::Method, Error, Extensions, Response};
|
2019-03-02 07:51:32 +01:00
|
|
|
use actix_service::{NewService, Service};
|
|
|
|
use futures::{Async, Future, IntoFuture, Poll};
|
|
|
|
|
2019-03-03 22:53:31 +01:00
|
|
|
use crate::extract::{ConfigStorage, ExtractorConfig, FromRequest};
|
2019-03-03 21:09:38 +01:00
|
|
|
use crate::guard::{self, Guard};
|
2019-03-07 20:09:42 +01:00
|
|
|
use crate::handler::{AsyncFactory, AsyncHandler, Extract, Factory, Handler};
|
2019-03-02 07:51:32 +01:00
|
|
|
use crate::responder::Responder;
|
2019-03-03 04:19:56 +01:00
|
|
|
use crate::service::{ServiceFromRequest, ServiceRequest, ServiceResponse};
|
|
|
|
use crate::HttpResponse;
|
2019-03-02 07:51:32 +01:00
|
|
|
|
|
|
|
type BoxedRouteService<Req, Res> = Box<
|
|
|
|
Service<
|
2019-03-05 19:08:08 +01:00
|
|
|
Req,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = Res,
|
|
|
|
Error = (),
|
|
|
|
Future = Box<Future<Item = Res, Error = ()>>,
|
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
|
|
|
type BoxedRouteNewService<Req, Res> = Box<
|
|
|
|
NewService<
|
2019-03-05 19:08:08 +01:00
|
|
|
Req,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = Res,
|
|
|
|
Error = (),
|
|
|
|
InitError = (),
|
|
|
|
Service = BoxedRouteService<Req, Res>,
|
|
|
|
Future = Box<Future<Item = BoxedRouteService<Req, Res>, Error = ()>>,
|
|
|
|
>,
|
|
|
|
>;
|
2017-12-05 01:09:22 +01:00
|
|
|
|
|
|
|
/// Resource route definition
|
|
|
|
///
|
|
|
|
/// Route uses builder-like pattern for configuration.
|
|
|
|
/// If handler is not explicitly set, default *404 Not Found* handler is used.
|
2019-03-02 07:51:32 +01:00
|
|
|
pub struct Route<P> {
|
|
|
|
service: BoxedRouteNewService<ServiceRequest<P>, ServiceResponse>,
|
2019-03-03 21:09:38 +01:00
|
|
|
guards: Rc<Vec<Box<Guard>>>,
|
2019-03-03 09:57:48 +01:00
|
|
|
config: ConfigStorage,
|
|
|
|
config_ref: Rc<RefCell<Option<Rc<Extensions>>>>,
|
2017-12-05 01:09:22 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<P: 'static> Route<P> {
|
2019-03-03 09:57:48 +01:00
|
|
|
/// Create new route which matches any request.
|
2019-03-03 04:19:56 +01:00
|
|
|
pub fn new() -> Route<P> {
|
2019-03-03 09:57:48 +01:00
|
|
|
let config_ref = Rc::new(RefCell::new(None));
|
2019-03-03 04:19:56 +01:00
|
|
|
Route {
|
2019-03-03 09:57:48 +01:00
|
|
|
service: Box::new(RouteNewService::new(
|
2019-03-07 20:09:42 +01:00
|
|
|
Extract::new(config_ref.clone()).and_then(
|
|
|
|
Handler::new(HttpResponse::NotFound).map_err(|_| panic!()),
|
|
|
|
),
|
2019-03-03 09:57:48 +01:00
|
|
|
)),
|
2019-03-03 21:09:38 +01:00
|
|
|
guards: Rc::new(Vec::new()),
|
2019-03-03 09:57:48 +01:00
|
|
|
config: ConfigStorage::default(),
|
|
|
|
config_ref,
|
2019-03-03 04:19:56 +01:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-03 09:57:48 +01:00
|
|
|
pub(crate) fn finish(self) -> Self {
|
|
|
|
*self.config_ref.borrow_mut() = self.config.storage.clone();
|
|
|
|
self
|
|
|
|
}
|
2019-03-07 00:47:15 +01:00
|
|
|
|
|
|
|
pub(crate) fn take_guards(&mut self) -> Vec<Box<Guard>> {
|
|
|
|
std::mem::replace(Rc::get_mut(&mut self.guards).unwrap(), Vec::new())
|
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 19:08:08 +01:00
|
|
|
impl<P> NewService<ServiceRequest<P>> for Route<P> {
|
2019-03-02 07:51:32 +01:00
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type InitError = ();
|
|
|
|
type Service = RouteService<P>;
|
|
|
|
type Future = CreateRouteService<P>;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
CreateRouteService {
|
|
|
|
fut: self.service.new_service(&()),
|
2019-03-03 21:09:38 +01:00
|
|
|
guards: self.guards.clone(),
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RouteFuture<P> = Box<
|
|
|
|
Future<Item = BoxedRouteService<ServiceRequest<P>, ServiceResponse>, Error = ()>,
|
|
|
|
>;
|
|
|
|
|
|
|
|
pub struct CreateRouteService<P> {
|
|
|
|
fut: RouteFuture<P>,
|
2019-03-03 21:09:38 +01:00
|
|
|
guards: Rc<Vec<Box<Guard>>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> Future for CreateRouteService<P> {
|
|
|
|
type Item = RouteService<P>;
|
|
|
|
type Error = ();
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match self.fut.poll()? {
|
|
|
|
Async::Ready(service) => Ok(Async::Ready(RouteService {
|
|
|
|
service,
|
2019-03-03 21:09:38 +01:00
|
|
|
guards: self.guards.clone(),
|
2019-03-02 07:51:32 +01:00
|
|
|
})),
|
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
2017-12-05 01:09:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
pub struct RouteService<P> {
|
|
|
|
service: BoxedRouteService<ServiceRequest<P>, ServiceResponse>,
|
2019-03-03 21:09:38 +01:00
|
|
|
guards: Rc<Vec<Box<Guard>>>,
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> RouteService<P> {
|
|
|
|
pub fn check(&self, req: &mut ServiceRequest<P>) -> bool {
|
2019-03-03 21:09:38 +01:00
|
|
|
for f in self.guards.iter() {
|
2019-03-03 04:19:56 +01:00
|
|
|
if !f.check(req.head()) {
|
2018-04-14 01:02:01 +02:00
|
|
|
return false;
|
2017-12-05 01:09:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2017-12-05 01:09:22 +01:00
|
|
|
|
2019-03-05 19:08:08 +01:00
|
|
|
impl<P> Service<ServiceRequest<P>> for RouteService<P> {
|
2019-03-02 07:51:32 +01:00
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.service.poll_ready()
|
2017-12-05 01:09:22 +01:00
|
|
|
}
|
|
|
|
|
2019-03-05 19:08:08 +01:00
|
|
|
fn call(&mut self, req: ServiceRequest<P>) -> Self::Future {
|
2019-03-02 07:51:32 +01:00
|
|
|
self.service.call(req)
|
2018-01-10 05:00:18 +01:00
|
|
|
}
|
2019-03-02 07:51:32 +01:00
|
|
|
}
|
2018-01-10 05:00:18 +01:00
|
|
|
|
2019-03-03 04:19:56 +01:00
|
|
|
impl<P: 'static> Route<P> {
|
2019-03-03 21:09:38 +01:00
|
|
|
/// Add method guard to the route.
|
2017-12-20 07:36:06 +01:00
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
2017-12-20 07:36:06 +01:00
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// App::new().service(web::resource("/path").route(
|
|
|
|
/// web::get()
|
|
|
|
/// .method(http::Method::CONNECT)
|
2019-03-03 21:09:38 +01:00
|
|
|
/// .guard(guard::Header("content-type", "text/plain"))
|
|
|
|
/// .to(|req: HttpRequest| HttpResponse::Ok()))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// );
|
2017-12-20 07:36:06 +01:00
|
|
|
/// # }
|
|
|
|
/// ```
|
2019-03-02 07:51:32 +01:00
|
|
|
pub fn method(mut self, method: Method) -> Self {
|
2019-03-03 21:09:38 +01:00
|
|
|
Rc::get_mut(&mut self.guards)
|
2019-03-03 04:19:56 +01:00
|
|
|
.unwrap()
|
2019-03-03 21:09:38 +01:00
|
|
|
.push(Box::new(guard::Method(method)));
|
2017-12-05 01:09:22 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2019-03-03 21:09:38 +01:00
|
|
|
/// Add guard to the route.
|
2019-03-02 07:51:32 +01:00
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
2019-03-02 07:51:32 +01:00
|
|
|
/// # use actix_web::*;
|
|
|
|
/// # fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// App::new().service(web::resource("/path").route(
|
|
|
|
/// web::route()
|
2019-03-03 21:09:38 +01:00
|
|
|
/// .guard(guard::Get())
|
|
|
|
/// .guard(guard::Header("content-type", "text/plain"))
|
|
|
|
/// .to(|req: HttpRequest| HttpResponse::Ok()))
|
2019-03-07 00:47:15 +01:00
|
|
|
/// );
|
2019-03-02 07:51:32 +01:00
|
|
|
/// # }
|
|
|
|
/// ```
|
2019-03-03 21:09:38 +01:00
|
|
|
pub fn guard<F: Guard + 'static>(mut self, f: F) -> Self {
|
|
|
|
Rc::get_mut(&mut self.guards).unwrap().push(Box::new(f));
|
2019-03-02 07:51:32 +01:00
|
|
|
self
|
2017-12-05 01:09:22 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
// pub fn map<T, U, F: IntoNewService<T>>(
|
|
|
|
// self,
|
|
|
|
// md: F,
|
|
|
|
// ) -> RouteServiceBuilder<T, S, (), U>
|
|
|
|
// where
|
|
|
|
// T: NewService<
|
|
|
|
// Request = HandlerRequest<S>,
|
|
|
|
// Response = HandlerRequest<S, U>,
|
|
|
|
// InitError = (),
|
|
|
|
// >,
|
|
|
|
// {
|
|
|
|
// RouteServiceBuilder {
|
|
|
|
// service: md.into_new_service(),
|
2019-03-03 21:09:38 +01:00
|
|
|
// guards: self.guards,
|
2019-03-02 07:51:32 +01:00
|
|
|
// _t: PhantomData,
|
|
|
|
// }
|
|
|
|
// }
|
2018-03-27 08:10:31 +02:00
|
|
|
|
2019-03-03 21:09:38 +01:00
|
|
|
/// Set handler function, use request extractors for parameters.
|
2018-03-27 08:10:31 +02:00
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
2018-03-27 08:10:31 +02:00
|
|
|
/// #[macro_use] extern crate serde_derive;
|
2019-03-07 23:01:52 +01:00
|
|
|
/// use actix_web::{web, http, App};
|
2018-03-27 08:10:31 +02:00
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Info {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// extract path info using serde
|
2019-03-07 23:01:52 +01:00
|
|
|
/// fn index(info: web::Path<Info>) -> String {
|
2019-03-03 21:09:38 +01:00
|
|
|
/// format!("Welcome {}!", info.username)
|
2018-03-27 08:10:31 +02:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/{username}/index.html") // <- define path parameters
|
|
|
|
/// .route(web::get().to(index)) // <- register handler
|
2019-03-03 21:09:38 +01:00
|
|
|
/// );
|
2018-03-27 08:10:31 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-05-10 01:27:31 +02:00
|
|
|
///
|
2018-10-02 06:16:56 +02:00
|
|
|
/// It is possible to use multiple extractors for one handler function.
|
2018-05-10 01:27:31 +02:00
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
2018-05-10 01:27:31 +02:00
|
|
|
/// # use std::collections::HashMap;
|
2019-03-03 21:09:38 +01:00
|
|
|
/// # use serde_derive::Deserialize;
|
2019-03-07 20:43:46 +01:00
|
|
|
/// use actix_web::{web, App};
|
2018-05-10 01:27:31 +02:00
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Info {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// extract path info using serde
|
2019-03-07 20:43:46 +01:00
|
|
|
/// fn index(path: web::Path<Info>, query: web::Query<HashMap<String, String>>, body: web::Json<Info>) -> String {
|
2019-03-03 21:09:38 +01:00
|
|
|
/// format!("Welcome {}!", path.username)
|
2018-05-10 01:27:31 +02:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/{username}/index.html") // <- define path parameters
|
|
|
|
/// .route(web::get().to(index))
|
2019-03-03 21:09:38 +01:00
|
|
|
/// );
|
2018-05-10 01:27:31 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-03 04:19:56 +01:00
|
|
|
pub fn to<F, T, R>(mut self, handler: F) -> Route<P>
|
2018-06-21 07:47:01 +02:00
|
|
|
where
|
2019-03-02 07:51:32 +01:00
|
|
|
F: Factory<T, R> + 'static,
|
|
|
|
T: FromRequest<P> + 'static,
|
2018-06-21 07:47:01 +02:00
|
|
|
R: Responder + 'static,
|
|
|
|
{
|
2019-03-03 09:57:48 +01:00
|
|
|
T::Config::store_default(&mut self.config);
|
2019-03-03 04:19:56 +01:00
|
|
|
self.service = Box::new(RouteNewService::new(
|
2019-03-03 09:57:48 +01:00
|
|
|
Extract::new(self.config_ref.clone())
|
2019-03-07 20:09:42 +01:00
|
|
|
.and_then(Handler::new(handler).map_err(|_| panic!())),
|
2019-03-03 04:19:56 +01:00
|
|
|
));
|
|
|
|
self
|
2018-03-27 08:10:31 +02:00
|
|
|
}
|
2018-03-28 23:24:32 +02:00
|
|
|
|
2019-03-03 21:09:38 +01:00
|
|
|
/// Set async handler function, use request extractors for parameters.
|
|
|
|
/// This method has to be used if your handler function returns `impl Future<>`
|
2018-05-10 01:27:31 +02:00
|
|
|
///
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ```rust
|
|
|
|
/// # use futures::future::ok;
|
2018-05-10 01:27:31 +02:00
|
|
|
/// #[macro_use] extern crate serde_derive;
|
2019-03-07 23:01:52 +01:00
|
|
|
/// use actix_web::{web, App, Error};
|
2018-05-10 01:27:31 +02:00
|
|
|
/// use futures::Future;
|
|
|
|
///
|
|
|
|
/// #[derive(Deserialize)]
|
|
|
|
/// struct Info {
|
|
|
|
/// username: String,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// /// extract path info using serde
|
2019-03-07 23:01:52 +01:00
|
|
|
/// fn index(info: web::Path<Info>) -> impl Future<Item = &'static str, Error = Error> {
|
2019-03-03 21:09:38 +01:00
|
|
|
/// ok("Hello World!")
|
2018-05-10 01:27:31 +02:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/{username}/index.html") // <- define path parameters
|
|
|
|
/// .route(web::get().to_async(index)) // <- register async handler
|
2019-03-03 21:09:38 +01:00
|
|
|
/// );
|
2018-05-10 01:27:31 +02:00
|
|
|
/// }
|
|
|
|
/// ```
|
2019-03-02 07:51:32 +01:00
|
|
|
#[allow(clippy::wrong_self_convention)]
|
2019-03-03 04:19:56 +01:00
|
|
|
pub fn to_async<F, T, R>(mut self, handler: F) -> Self
|
2018-04-14 01:02:01 +02:00
|
|
|
where
|
2019-03-02 07:51:32 +01:00
|
|
|
F: AsyncFactory<T, R>,
|
|
|
|
T: FromRequest<P> + 'static,
|
|
|
|
R: IntoFuture + 'static,
|
|
|
|
R::Item: Into<Response>,
|
|
|
|
R::Error: Into<Error>,
|
2018-01-10 05:00:18 +01:00
|
|
|
{
|
2019-03-03 04:19:56 +01:00
|
|
|
self.service = Box::new(RouteNewService::new(
|
2019-03-03 09:57:48 +01:00
|
|
|
Extract::new(self.config_ref.clone())
|
2019-03-07 20:09:42 +01:00
|
|
|
.and_then(AsyncHandler::new(handler).map_err(|_| panic!())),
|
2019-03-03 04:19:56 +01:00
|
|
|
));
|
|
|
|
self
|
2018-01-10 05:00:18 +01:00
|
|
|
}
|
2019-03-03 09:57:48 +01:00
|
|
|
|
|
|
|
/// This method allows to add extractor configuration
|
|
|
|
/// for specific route.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2019-03-07 23:01:52 +01:00
|
|
|
/// use actix_web::{web, App};
|
2019-03-03 09:57:48 +01:00
|
|
|
///
|
|
|
|
/// /// extract text data from request
|
|
|
|
/// fn index(body: String) -> String {
|
|
|
|
/// format!("Body {}!", body)
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2019-03-07 00:47:15 +01:00
|
|
|
/// let app = App::new().service(
|
|
|
|
/// web::resource("/index.html").route(
|
2019-03-03 09:57:48 +01:00
|
|
|
/// web::get()
|
|
|
|
/// // limit size of the payload
|
2019-03-07 23:01:52 +01:00
|
|
|
/// .config(web::PayloadConfig::new(4096))
|
2019-03-03 09:57:48 +01:00
|
|
|
/// // register handler
|
|
|
|
/// .to(index)
|
2019-03-07 00:47:15 +01:00
|
|
|
/// ));
|
2019-03-03 09:57:48 +01:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub fn config<C: ExtractorConfig>(mut self, config: C) -> Self {
|
|
|
|
self.config.store(config);
|
|
|
|
self
|
|
|
|
}
|
2018-01-10 05:00:18 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
struct RouteNewService<P, T>
|
|
|
|
where
|
2019-03-05 19:08:08 +01:00
|
|
|
T: NewService<ServiceRequest<P>, Error = (Error, ServiceFromRequest<P>)>,
|
2019-03-02 07:51:32 +01:00
|
|
|
{
|
|
|
|
service: T,
|
2019-03-05 19:08:08 +01:00
|
|
|
_t: PhantomData<P>,
|
2018-01-10 05:00:18 +01:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
impl<P: 'static, T> RouteNewService<P, T>
|
|
|
|
where
|
|
|
|
T: NewService<
|
2019-03-05 19:08:08 +01:00
|
|
|
ServiceRequest<P>,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-03 04:19:56 +01:00
|
|
|
Error = (Error, ServiceFromRequest<P>),
|
2019-03-02 07:51:32 +01:00
|
|
|
>,
|
|
|
|
T::Future: 'static,
|
|
|
|
T::Service: 'static,
|
2019-03-05 19:08:08 +01:00
|
|
|
<T::Service as Service<ServiceRequest<P>>>::Future: 'static,
|
2019-03-02 07:51:32 +01:00
|
|
|
{
|
|
|
|
pub fn new(service: T) -> Self {
|
2019-03-05 19:08:08 +01:00
|
|
|
RouteNewService {
|
|
|
|
service,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2018-01-10 05:00:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 19:08:08 +01:00
|
|
|
impl<P: 'static, T> NewService<ServiceRequest<P>> for RouteNewService<P, T>
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-03-05 19:08:08 +01:00
|
|
|
ServiceRequest<P>,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-03 04:19:56 +01:00
|
|
|
Error = (Error, ServiceFromRequest<P>),
|
2019-03-02 07:51:32 +01:00
|
|
|
>,
|
|
|
|
T::Future: 'static,
|
|
|
|
T::Service: 'static,
|
2019-03-05 19:08:08 +01:00
|
|
|
<T::Service as Service<ServiceRequest<P>>>::Future: 'static,
|
2019-03-02 07:51:32 +01:00
|
|
|
{
|
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type InitError = ();
|
2019-03-05 19:08:08 +01:00
|
|
|
type Service = BoxedRouteService<ServiceRequest<P>, Self::Response>;
|
2019-03-02 07:51:32 +01:00
|
|
|
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
Box::new(
|
|
|
|
self.service
|
|
|
|
.new_service(&())
|
|
|
|
.map_err(|_| ())
|
|
|
|
.and_then(|service| {
|
|
|
|
let service: BoxedRouteService<_, _> =
|
2019-03-05 19:08:08 +01:00
|
|
|
Box::new(RouteServiceWrapper {
|
|
|
|
service,
|
|
|
|
_t: PhantomData,
|
|
|
|
});
|
2019-03-02 07:51:32 +01:00
|
|
|
Ok(service)
|
|
|
|
}),
|
|
|
|
)
|
2018-01-10 05:00:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 19:08:08 +01:00
|
|
|
struct RouteServiceWrapper<P, T: Service<ServiceRequest<P>>> {
|
2019-03-02 07:51:32 +01:00
|
|
|
service: T,
|
2019-03-05 19:08:08 +01:00
|
|
|
_t: PhantomData<P>,
|
2018-04-30 05:50:38 +02:00
|
|
|
}
|
|
|
|
|
2019-03-05 19:08:08 +01:00
|
|
|
impl<P, T> Service<ServiceRequest<P>> for RouteServiceWrapper<P, T>
|
2019-03-02 07:51:32 +01:00
|
|
|
where
|
|
|
|
T::Future: 'static,
|
|
|
|
T: Service<
|
2019-03-05 19:08:08 +01:00
|
|
|
ServiceRequest<P>,
|
2019-03-02 07:51:32 +01:00
|
|
|
Response = ServiceResponse,
|
2019-03-03 04:19:56 +01:00
|
|
|
Error = (Error, ServiceFromRequest<P>),
|
2019-03-02 07:51:32 +01:00
|
|
|
>,
|
|
|
|
{
|
|
|
|
type Response = ServiceResponse;
|
|
|
|
type Error = ();
|
|
|
|
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.service.poll_ready().map_err(|_| ())
|
2018-04-30 05:50:38 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 07:51:32 +01:00
|
|
|
fn call(&mut self, req: ServiceRequest<P>) -> Self::Future {
|
|
|
|
Box::new(self.service.call(req).then(|res| match res {
|
|
|
|
Ok(res) => Ok(res),
|
|
|
|
Err((err, req)) => Ok(req.error_response(err)),
|
|
|
|
}))
|
2017-12-05 01:09:22 +01:00
|
|
|
}
|
|
|
|
}
|