mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-27 17:52:56 +01:00
revert generic request parameter for service; support ServerConfig as new factory config
This commit is contained in:
parent
aadcdaa3d6
commit
fde55ffa14
@ -72,7 +72,7 @@ actix-utils = { git = "https://github.com/actix/actix-net.git" }
|
||||
actix-http = { git = "https://github.com/actix/actix-http.git" }
|
||||
actix-router = { git = "https://github.com/actix/actix-net.git" }
|
||||
actix-server = { git = "https://github.com/actix/actix-net.git" }
|
||||
#actix-router = { path="../actix-net/router" }
|
||||
actix-server-config = { git = "https://github.com/actix/actix-net.git" }
|
||||
|
||||
bytes = "0.4"
|
||||
derive_more = "0.14"
|
||||
|
419
src/app.rs
419
src/app.rs
@ -3,37 +3,30 @@ use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
|
||||
use actix_http::body::{Body, MessageBody};
|
||||
use actix_http::{Extensions, PayloadStream, Request, Response};
|
||||
use actix_router::{Path, ResourceDef, ResourceInfo, Router, Url};
|
||||
use actix_service::boxed::{self, BoxedNewService, BoxedService};
|
||||
use actix_http::{Extensions, PayloadStream};
|
||||
use actix_server_config::ServerConfig;
|
||||
use actix_service::boxed::{self, BoxedNewService};
|
||||
use actix_service::{
|
||||
fn_service, AndThenNewService, ApplyTransform, IntoNewService, IntoTransform,
|
||||
NewService, Service, Transform,
|
||||
ApplyTransform, IntoNewService, IntoTransform, NewService, Transform,
|
||||
};
|
||||
use futures::future::{ok, Either, FutureResult};
|
||||
use futures::{Async, Future, IntoFuture, Poll};
|
||||
use futures::IntoFuture;
|
||||
|
||||
use crate::config::AppConfig;
|
||||
use crate::guard::Guard;
|
||||
use crate::app_service::{AppChain, AppEntry, AppInit, AppRouting, AppRoutingFactory};
|
||||
use crate::resource::Resource;
|
||||
use crate::rmap::ResourceMap;
|
||||
use crate::route::Route;
|
||||
use crate::service::{
|
||||
HttpServiceFactory, ServiceFactory, ServiceFactoryWrapper, ServiceRequest,
|
||||
ServiceResponse,
|
||||
};
|
||||
use crate::state::{State, StateFactory, StateFactoryResult};
|
||||
use crate::state::{State, StateFactory};
|
||||
|
||||
type Guards = Vec<Box<Guard>>;
|
||||
type HttpService<P> = BoxedService<ServiceRequest<P>, ServiceResponse, ()>;
|
||||
type HttpNewService<P> = BoxedNewService<(), ServiceRequest<P>, ServiceResponse, (), ()>;
|
||||
type BoxedResponse = Box<Future<Item = ServiceResponse, Error = ()>>;
|
||||
|
||||
/// Application builder - structure that follows the builder pattern
|
||||
/// for building application instances.
|
||||
pub struct App<P, T>
|
||||
where
|
||||
T: NewService<ServiceRequest, Response = ServiceRequest<P>>,
|
||||
T: NewService<Request = ServiceRequest, Response = ServiceRequest<P>>,
|
||||
{
|
||||
chain: T,
|
||||
extensions: Extensions,
|
||||
@ -58,7 +51,7 @@ impl<P, T> App<P, T>
|
||||
where
|
||||
P: 'static,
|
||||
T: NewService<
|
||||
ServiceRequest,
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceRequest<P>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -121,7 +114,7 @@ where
|
||||
P,
|
||||
B,
|
||||
impl NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -130,12 +123,12 @@ where
|
||||
where
|
||||
M: Transform<
|
||||
AppRouting<P>,
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
F: IntoTransform<M, AppRouting<P>, ServiceRequest<P>>,
|
||||
F: IntoTransform<M, AppRouting<P>>,
|
||||
{
|
||||
let fref = Rc::new(RefCell::new(None));
|
||||
let endpoint = ApplyTransform::new(mw, AppEntry::new(fref.clone()));
|
||||
@ -159,7 +152,7 @@ where
|
||||
) -> App<
|
||||
P1,
|
||||
impl NewService<
|
||||
ServiceRequest,
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceRequest<P1>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -167,12 +160,12 @@ where
|
||||
>
|
||||
where
|
||||
C: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceRequest<P1>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
F: IntoNewService<C, ServiceRequest<P>>,
|
||||
F: IntoNewService<C>,
|
||||
{
|
||||
let chain = self.chain.and_then(chain.into_new_service());
|
||||
App {
|
||||
@ -264,7 +257,7 @@ where
|
||||
P: 'static,
|
||||
B: MessageBody,
|
||||
T: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -324,7 +317,7 @@ where
|
||||
P,
|
||||
B1,
|
||||
impl NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse<B1>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -333,13 +326,13 @@ where
|
||||
where
|
||||
M: Transform<
|
||||
T::Service,
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse<B1>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
B1: MessageBody,
|
||||
F: IntoTransform<M, T::Service, ServiceRequest<P>>,
|
||||
F: IntoTransform<M, T::Service>,
|
||||
{
|
||||
let endpoint = ApplyTransform::new(mw, self.endpoint);
|
||||
AppRouter {
|
||||
@ -362,7 +355,7 @@ where
|
||||
where
|
||||
F: FnOnce(Resource<P>) -> Resource<P, U>,
|
||||
U: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -413,391 +406,39 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, T, P: 'static, B: MessageBody>
|
||||
IntoNewService<AndThenNewService<AppInit<C, P>, T>, Request>
|
||||
impl<C, T, P: 'static, B: MessageBody> IntoNewService<AppInit<C, T, P, B>, ServerConfig>
|
||||
for AppRouter<C, P, B, T>
|
||||
where
|
||||
T: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
C: NewService<
|
||||
ServiceRequest,
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceRequest<P>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
{
|
||||
fn into_new_service(self) -> AndThenNewService<AppInit<C, P>, T> {
|
||||
// update resource default service
|
||||
let default = self.default.unwrap_or_else(|| {
|
||||
Rc::new(boxed::new_service(fn_service(|req: ServiceRequest<P>| {
|
||||
Ok(req.into_response(Response::NotFound().finish()))
|
||||
})))
|
||||
});
|
||||
|
||||
let mut config = AppConfig::new(
|
||||
"127.0.0.1:8080".parse().unwrap(),
|
||||
"localhost:8080".to_owned(),
|
||||
false,
|
||||
default.clone(),
|
||||
);
|
||||
|
||||
// register services
|
||||
self.services
|
||||
.into_iter()
|
||||
.for_each(|mut srv| srv.register(&mut config));
|
||||
|
||||
let mut rmap = ResourceMap::new(ResourceDef::new(""));
|
||||
|
||||
// complete pipeline creation
|
||||
*self.factory_ref.borrow_mut() = Some(AppRoutingFactory {
|
||||
default,
|
||||
services: Rc::new(
|
||||
config
|
||||
.into_services()
|
||||
.into_iter()
|
||||
.map(|(mut rdef, srv, guards, nested)| {
|
||||
rmap.add(&mut rdef, nested);
|
||||
(rdef, srv, RefCell::new(guards))
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
});
|
||||
|
||||
// complete ResourceMap tree creation
|
||||
let rmap = Rc::new(rmap);
|
||||
rmap.finish(rmap.clone());
|
||||
|
||||
fn into_new_service(self) -> AppInit<C, T, P, B> {
|
||||
AppInit {
|
||||
rmap,
|
||||
chain: self.chain,
|
||||
state: self.state,
|
||||
endpoint: self.endpoint,
|
||||
services: RefCell::new(self.services),
|
||||
default: self.default,
|
||||
factory_ref: self.factory_ref,
|
||||
extensions: Rc::new(RefCell::new(Rc::new(self.extensions))),
|
||||
}
|
||||
.and_then(self.endpoint)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AppRoutingFactory<P> {
|
||||
services: Rc<Vec<(ResourceDef, HttpNewService<P>, RefCell<Option<Guards>>)>>,
|
||||
default: Rc<HttpNewService<P>>,
|
||||
}
|
||||
|
||||
impl<P: 'static> NewService<ServiceRequest<P>> for AppRoutingFactory<P> {
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
type Service = AppRouting<P>;
|
||||
type Future = AppRoutingFactoryResponse<P>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
AppRoutingFactoryResponse {
|
||||
fut: self
|
||||
.services
|
||||
.iter()
|
||||
.map(|(path, service, guards)| {
|
||||
CreateAppRoutingItem::Future(
|
||||
Some(path.clone()),
|
||||
guards.borrow_mut().take(),
|
||||
service.new_service(&()),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
default: None,
|
||||
default_fut: Some(self.default.new_service(&())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type HttpServiceFut<P> = Box<Future<Item = HttpService<P>, Error = ()>>;
|
||||
|
||||
/// Create app service
|
||||
#[doc(hidden)]
|
||||
pub struct AppRoutingFactoryResponse<P> {
|
||||
fut: Vec<CreateAppRoutingItem<P>>,
|
||||
default: Option<HttpService<P>>,
|
||||
default_fut: Option<Box<Future<Item = HttpService<P>, Error = ()>>>,
|
||||
}
|
||||
|
||||
enum CreateAppRoutingItem<P> {
|
||||
Future(Option<ResourceDef>, Option<Guards>, HttpServiceFut<P>),
|
||||
Service(ResourceDef, Option<Guards>, HttpService<P>),
|
||||
}
|
||||
|
||||
impl<P> Future for AppRoutingFactoryResponse<P> {
|
||||
type Item = AppRouting<P>;
|
||||
type Error = ();
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
let mut done = true;
|
||||
|
||||
if let Some(ref mut fut) = self.default_fut {
|
||||
match fut.poll()? {
|
||||
Async::Ready(default) => self.default = Some(default),
|
||||
Async::NotReady => done = false,
|
||||
}
|
||||
}
|
||||
|
||||
// poll http services
|
||||
for item in &mut self.fut {
|
||||
let res = match item {
|
||||
CreateAppRoutingItem::Future(
|
||||
ref mut path,
|
||||
ref mut guards,
|
||||
ref mut fut,
|
||||
) => match fut.poll()? {
|
||||
Async::Ready(service) => {
|
||||
Some((path.take().unwrap(), guards.take(), service))
|
||||
}
|
||||
Async::NotReady => {
|
||||
done = false;
|
||||
None
|
||||
}
|
||||
},
|
||||
CreateAppRoutingItem::Service(_, _, _) => continue,
|
||||
};
|
||||
|
||||
if let Some((path, guards, service)) = res {
|
||||
*item = CreateAppRoutingItem::Service(path, guards, service);
|
||||
}
|
||||
}
|
||||
|
||||
if done {
|
||||
let router = self
|
||||
.fut
|
||||
.drain(..)
|
||||
.fold(Router::build(), |mut router, item| {
|
||||
match item {
|
||||
CreateAppRoutingItem::Service(path, guards, service) => {
|
||||
router.rdef(path, service).2 = guards;
|
||||
}
|
||||
CreateAppRoutingItem::Future(_, _, _) => unreachable!(),
|
||||
}
|
||||
router
|
||||
});
|
||||
Ok(Async::Ready(AppRouting {
|
||||
ready: None,
|
||||
router: router.finish(),
|
||||
default: self.default.take(),
|
||||
}))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AppRouting<P> {
|
||||
router: Router<HttpService<P>, Guards>,
|
||||
ready: Option<(ServiceRequest<P>, ResourceInfo)>,
|
||||
default: Option<HttpService<P>>,
|
||||
}
|
||||
|
||||
impl<P> Service<ServiceRequest<P>> for AppRouting<P> {
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type Future = Either<BoxedResponse, FutureResult<Self::Response, Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
if self.ready.is_none() {
|
||||
Ok(Async::Ready(()))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
|
||||
fn call(&mut self, mut req: ServiceRequest<P>) -> Self::Future {
|
||||
let res = self.router.recognize_mut_checked(&mut req, |req, guards| {
|
||||
if let Some(ref guards) = guards {
|
||||
for f in guards {
|
||||
if !f.check(req.head()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
});
|
||||
|
||||
if let Some((srv, _info)) = res {
|
||||
Either::A(srv.call(req))
|
||||
} else if let Some(ref mut default) = self.default {
|
||||
Either::A(default.call(req))
|
||||
} else {
|
||||
let req = req.into_request();
|
||||
Either::B(ok(ServiceResponse::new(req, Response::NotFound().finish())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
/// Wrapper service for routing
|
||||
pub struct AppEntry<P> {
|
||||
factory: Rc<RefCell<Option<AppRoutingFactory<P>>>>,
|
||||
}
|
||||
|
||||
impl<P> AppEntry<P> {
|
||||
fn new(factory: Rc<RefCell<Option<AppRoutingFactory<P>>>>) -> Self {
|
||||
AppEntry { factory }
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: 'static> NewService<ServiceRequest<P>> for AppEntry<P> {
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
type Service = AppRouting<P>;
|
||||
type Future = AppRoutingFactoryResponse<P>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
self.factory.borrow_mut().as_mut().unwrap().new_service(&())
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct AppChain;
|
||||
|
||||
impl NewService<ServiceRequest> for AppChain {
|
||||
type Response = ServiceRequest;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
type Service = AppChain;
|
||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
ok(AppChain)
|
||||
}
|
||||
}
|
||||
|
||||
impl Service<ServiceRequest> for AppChain {
|
||||
type Response = ServiceRequest;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
||||
ok(req)
|
||||
}
|
||||
}
|
||||
|
||||
/// Service factory to convert `Request` to a `ServiceRequest<S>`.
|
||||
/// It also executes state factories.
|
||||
pub struct AppInit<C, P>
|
||||
where
|
||||
C: NewService<ServiceRequest, Response = ServiceRequest<P>>,
|
||||
{
|
||||
chain: C,
|
||||
rmap: Rc<ResourceMap>,
|
||||
state: Vec<Box<StateFactory>>,
|
||||
extensions: Rc<RefCell<Rc<Extensions>>>,
|
||||
}
|
||||
|
||||
impl<C, P: 'static> NewService<Request> for AppInit<C, P>
|
||||
where
|
||||
C: NewService<ServiceRequest, Response = ServiceRequest<P>, InitError = ()>,
|
||||
{
|
||||
type Response = ServiceRequest<P>;
|
||||
type Error = C::Error;
|
||||
type InitError = C::InitError;
|
||||
type Service = AppInitService<C::Service, P>;
|
||||
type Future = AppInitResult<C, P>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
AppInitResult {
|
||||
chain: self.chain.new_service(&()),
|
||||
state: self.state.iter().map(|s| s.construct()).collect(),
|
||||
extensions: self.extensions.clone(),
|
||||
rmap: self.rmap.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct AppInitResult<C, P>
|
||||
where
|
||||
C: NewService<ServiceRequest, Response = ServiceRequest<P>, InitError = ()>,
|
||||
{
|
||||
chain: C::Future,
|
||||
rmap: Rc<ResourceMap>,
|
||||
state: Vec<Box<StateFactoryResult>>,
|
||||
extensions: Rc<RefCell<Rc<Extensions>>>,
|
||||
}
|
||||
|
||||
impl<C, P> Future for AppInitResult<C, P>
|
||||
where
|
||||
C: NewService<ServiceRequest, Response = ServiceRequest<P>, InitError = ()>,
|
||||
{
|
||||
type Item = AppInitService<C::Service, P>;
|
||||
type Error = C::InitError;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if let Some(extensions) = Rc::get_mut(&mut *self.extensions.borrow_mut()) {
|
||||
let mut idx = 0;
|
||||
while idx < self.state.len() {
|
||||
if let Async::Ready(_) = self.state[idx].poll_result(extensions)? {
|
||||
self.state.remove(idx);
|
||||
} else {
|
||||
idx += 1;
|
||||
}
|
||||
}
|
||||
if !self.state.is_empty() {
|
||||
return Ok(Async::NotReady);
|
||||
}
|
||||
} else {
|
||||
log::warn!("Multiple copies of app extensions exists");
|
||||
}
|
||||
|
||||
let chain = futures::try_ready!(self.chain.poll());
|
||||
|
||||
Ok(Async::Ready(AppInitService {
|
||||
chain,
|
||||
rmap: self.rmap.clone(),
|
||||
extensions: self.extensions.borrow().clone(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
/// Service to convert `Request` to a `ServiceRequest<S>`
|
||||
pub struct AppInitService<C, P>
|
||||
where
|
||||
C: Service<ServiceRequest, Response = ServiceRequest<P>>,
|
||||
{
|
||||
chain: C,
|
||||
rmap: Rc<ResourceMap>,
|
||||
extensions: Rc<Extensions>,
|
||||
}
|
||||
|
||||
impl<C, P> Service<Request> for AppInitService<C, P>
|
||||
where
|
||||
C: Service<ServiceRequest, Response = ServiceRequest<P>>,
|
||||
{
|
||||
type Response = ServiceRequest<P>;
|
||||
type Error = C::Error;
|
||||
type Future = C::Future;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.chain.poll_ready()
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Request) -> Self::Future {
|
||||
let req = ServiceRequest::new(
|
||||
Path::new(Url::new(req.uri().clone())),
|
||||
req,
|
||||
self.rmap.clone(),
|
||||
self.extensions.clone(),
|
||||
);
|
||||
self.chain.call(req)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use actix_service::Service;
|
||||
|
||||
use super::*;
|
||||
use crate::http::{Method, StatusCode};
|
||||
use crate::test::{block_on, init_service, TestRequest};
|
||||
|
439
src/app_service.rs
Normal file
439
src/app_service.rs
Normal file
@ -0,0 +1,439 @@
|
||||
use std::cell::RefCell;
|
||||
use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
|
||||
use actix_http::{Extensions, Request, Response};
|
||||
use actix_router::{Path, ResourceDef, ResourceInfo, Router, Url};
|
||||
use actix_server_config::ServerConfig;
|
||||
use actix_service::boxed::{self, BoxedNewService, BoxedService};
|
||||
use actix_service::{fn_service, AndThen, NewService, Service, ServiceExt};
|
||||
use futures::future::{ok, Either, FutureResult};
|
||||
use futures::{Async, Future, Poll};
|
||||
|
||||
use crate::config::AppConfig;
|
||||
use crate::guard::Guard;
|
||||
use crate::rmap::ResourceMap;
|
||||
use crate::service::{ServiceFactory, ServiceRequest, ServiceResponse};
|
||||
use crate::state::{StateFactory, StateFactoryResult};
|
||||
|
||||
type Guards = Vec<Box<Guard>>;
|
||||
type HttpService<P> = BoxedService<ServiceRequest<P>, ServiceResponse, ()>;
|
||||
type HttpNewService<P> = BoxedNewService<(), ServiceRequest<P>, ServiceResponse, (), ()>;
|
||||
type BoxedResponse = Box<Future<Item = ServiceResponse, Error = ()>>;
|
||||
|
||||
/// Service factory to convert `Request` to a `ServiceRequest<S>`.
|
||||
/// It also executes state factories.
|
||||
pub struct AppInit<C, T, P, B>
|
||||
where
|
||||
C: NewService<Request = ServiceRequest, Response = ServiceRequest<P>>,
|
||||
T: NewService<
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
{
|
||||
pub(crate) chain: C,
|
||||
pub(crate) endpoint: T,
|
||||
pub(crate) state: Vec<Box<StateFactory>>,
|
||||
pub(crate) extensions: Rc<RefCell<Rc<Extensions>>>,
|
||||
pub(crate) services: RefCell<Vec<Box<ServiceFactory<P>>>>,
|
||||
pub(crate) default: Option<Rc<HttpNewService<P>>>,
|
||||
pub(crate) factory_ref: Rc<RefCell<Option<AppRoutingFactory<P>>>>,
|
||||
}
|
||||
|
||||
impl<C, T, P: 'static, B> NewService<ServerConfig> for AppInit<C, T, P, B>
|
||||
where
|
||||
C: NewService<
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceRequest<P>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
T: NewService<
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
{
|
||||
type Request = Request;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = C::Error;
|
||||
type InitError = C::InitError;
|
||||
type Service = AndThen<AppInitService<C::Service, P>, T::Service>;
|
||||
type Future = AppInitResult<C, T, P, B>;
|
||||
|
||||
fn new_service(&self, _: &ServerConfig) -> Self::Future {
|
||||
// update resource default service
|
||||
let default = self.default.clone().unwrap_or_else(|| {
|
||||
Rc::new(boxed::new_service(fn_service(|req: ServiceRequest<P>| {
|
||||
Ok(req.into_response(Response::NotFound().finish()))
|
||||
})))
|
||||
});
|
||||
|
||||
let mut config = AppConfig::new(
|
||||
"127.0.0.1:8080".parse().unwrap(),
|
||||
"localhost:8080".to_owned(),
|
||||
false,
|
||||
default.clone(),
|
||||
);
|
||||
|
||||
// register services
|
||||
std::mem::replace(&mut *self.services.borrow_mut(), Vec::new())
|
||||
.into_iter()
|
||||
.for_each(|mut srv| srv.register(&mut config));
|
||||
|
||||
let mut rmap = ResourceMap::new(ResourceDef::new(""));
|
||||
|
||||
// complete pipeline creation
|
||||
*self.factory_ref.borrow_mut() = Some(AppRoutingFactory {
|
||||
default,
|
||||
services: Rc::new(
|
||||
config
|
||||
.into_services()
|
||||
.into_iter()
|
||||
.map(|(mut rdef, srv, guards, nested)| {
|
||||
rmap.add(&mut rdef, nested);
|
||||
(rdef, srv, RefCell::new(guards))
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
});
|
||||
|
||||
// complete ResourceMap tree creation
|
||||
let rmap = Rc::new(rmap);
|
||||
rmap.finish(rmap.clone());
|
||||
|
||||
AppInitResult {
|
||||
chain: None,
|
||||
chain_fut: self.chain.new_service(&()),
|
||||
endpoint: None,
|
||||
endpoint_fut: self.endpoint.new_service(&()),
|
||||
state: self.state.iter().map(|s| s.construct()).collect(),
|
||||
extensions: self.extensions.clone(),
|
||||
rmap,
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AppInitResult<C, T, P, B>
|
||||
where
|
||||
C: NewService,
|
||||
T: NewService,
|
||||
{
|
||||
chain: Option<C::Service>,
|
||||
endpoint: Option<T::Service>,
|
||||
chain_fut: C::Future,
|
||||
endpoint_fut: T::Future,
|
||||
rmap: Rc<ResourceMap>,
|
||||
state: Vec<Box<StateFactoryResult>>,
|
||||
extensions: Rc<RefCell<Rc<Extensions>>>,
|
||||
_t: PhantomData<(P, B)>,
|
||||
}
|
||||
|
||||
impl<C, T, P, B> Future for AppInitResult<C, T, P, B>
|
||||
where
|
||||
C: NewService<
|
||||
Request = ServiceRequest,
|
||||
Response = ServiceRequest<P>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
T: NewService<
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
{
|
||||
type Item = AndThen<AppInitService<C::Service, P>, T::Service>;
|
||||
type Error = C::InitError;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if let Some(extensions) = Rc::get_mut(&mut *self.extensions.borrow_mut()) {
|
||||
let mut idx = 0;
|
||||
while idx < self.state.len() {
|
||||
if let Async::Ready(_) = self.state[idx].poll_result(extensions)? {
|
||||
self.state.remove(idx);
|
||||
} else {
|
||||
idx += 1;
|
||||
}
|
||||
}
|
||||
if !self.state.is_empty() {
|
||||
return Ok(Async::NotReady);
|
||||
}
|
||||
} else {
|
||||
log::warn!("Multiple copies of app extensions exists");
|
||||
}
|
||||
|
||||
if self.chain.is_none() {
|
||||
if let Async::Ready(srv) = self.chain_fut.poll()? {
|
||||
self.chain = Some(srv);
|
||||
}
|
||||
}
|
||||
|
||||
if self.endpoint.is_none() {
|
||||
if let Async::Ready(srv) = self.endpoint_fut.poll()? {
|
||||
self.endpoint = Some(srv);
|
||||
}
|
||||
}
|
||||
|
||||
if self.chain.is_some() && self.endpoint.is_some() {
|
||||
Ok(Async::Ready(
|
||||
AppInitService {
|
||||
chain: self.chain.take().unwrap(),
|
||||
rmap: self.rmap.clone(),
|
||||
extensions: self.extensions.borrow().clone(),
|
||||
}
|
||||
.and_then(self.endpoint.take().unwrap()),
|
||||
))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Service to convert `Request` to a `ServiceRequest<S>`
|
||||
pub struct AppInitService<C, P>
|
||||
where
|
||||
C: Service<Request = ServiceRequest, Response = ServiceRequest<P>, Error = ()>,
|
||||
{
|
||||
chain: C,
|
||||
rmap: Rc<ResourceMap>,
|
||||
extensions: Rc<Extensions>,
|
||||
}
|
||||
|
||||
impl<C, P> Service for AppInitService<C, P>
|
||||
where
|
||||
C: Service<Request = ServiceRequest, Response = ServiceRequest<P>, Error = ()>,
|
||||
{
|
||||
type Request = Request;
|
||||
type Response = ServiceRequest<P>;
|
||||
type Error = C::Error;
|
||||
type Future = C::Future;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.chain.poll_ready()
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Request) -> Self::Future {
|
||||
let req = ServiceRequest::new(
|
||||
Path::new(Url::new(req.uri().clone())),
|
||||
req,
|
||||
self.rmap.clone(),
|
||||
self.extensions.clone(),
|
||||
);
|
||||
self.chain.call(req)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AppRoutingFactory<P> {
|
||||
services: Rc<Vec<(ResourceDef, HttpNewService<P>, RefCell<Option<Guards>>)>>,
|
||||
default: Rc<HttpNewService<P>>,
|
||||
}
|
||||
|
||||
impl<P: 'static> NewService for AppRoutingFactory<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
type Service = AppRouting<P>;
|
||||
type Future = AppRoutingFactoryResponse<P>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
AppRoutingFactoryResponse {
|
||||
fut: self
|
||||
.services
|
||||
.iter()
|
||||
.map(|(path, service, guards)| {
|
||||
CreateAppRoutingItem::Future(
|
||||
Some(path.clone()),
|
||||
guards.borrow_mut().take(),
|
||||
service.new_service(&()),
|
||||
)
|
||||
})
|
||||
.collect(),
|
||||
default: None,
|
||||
default_fut: Some(self.default.new_service(&())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type HttpServiceFut<P> = Box<Future<Item = HttpService<P>, Error = ()>>;
|
||||
|
||||
/// Create app service
|
||||
#[doc(hidden)]
|
||||
pub struct AppRoutingFactoryResponse<P> {
|
||||
fut: Vec<CreateAppRoutingItem<P>>,
|
||||
default: Option<HttpService<P>>,
|
||||
default_fut: Option<Box<Future<Item = HttpService<P>, Error = ()>>>,
|
||||
}
|
||||
|
||||
enum CreateAppRoutingItem<P> {
|
||||
Future(Option<ResourceDef>, Option<Guards>, HttpServiceFut<P>),
|
||||
Service(ResourceDef, Option<Guards>, HttpService<P>),
|
||||
}
|
||||
|
||||
impl<P> Future for AppRoutingFactoryResponse<P> {
|
||||
type Item = AppRouting<P>;
|
||||
type Error = ();
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
let mut done = true;
|
||||
|
||||
if let Some(ref mut fut) = self.default_fut {
|
||||
match fut.poll()? {
|
||||
Async::Ready(default) => self.default = Some(default),
|
||||
Async::NotReady => done = false,
|
||||
}
|
||||
}
|
||||
|
||||
// poll http services
|
||||
for item in &mut self.fut {
|
||||
let res = match item {
|
||||
CreateAppRoutingItem::Future(
|
||||
ref mut path,
|
||||
ref mut guards,
|
||||
ref mut fut,
|
||||
) => match fut.poll()? {
|
||||
Async::Ready(service) => {
|
||||
Some((path.take().unwrap(), guards.take(), service))
|
||||
}
|
||||
Async::NotReady => {
|
||||
done = false;
|
||||
None
|
||||
}
|
||||
},
|
||||
CreateAppRoutingItem::Service(_, _, _) => continue,
|
||||
};
|
||||
|
||||
if let Some((path, guards, service)) = res {
|
||||
*item = CreateAppRoutingItem::Service(path, guards, service);
|
||||
}
|
||||
}
|
||||
|
||||
if done {
|
||||
let router = self
|
||||
.fut
|
||||
.drain(..)
|
||||
.fold(Router::build(), |mut router, item| {
|
||||
match item {
|
||||
CreateAppRoutingItem::Service(path, guards, service) => {
|
||||
router.rdef(path, service).2 = guards;
|
||||
}
|
||||
CreateAppRoutingItem::Future(_, _, _) => unreachable!(),
|
||||
}
|
||||
router
|
||||
});
|
||||
Ok(Async::Ready(AppRouting {
|
||||
ready: None,
|
||||
router: router.finish(),
|
||||
default: self.default.take(),
|
||||
}))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AppRouting<P> {
|
||||
router: Router<HttpService<P>, Guards>,
|
||||
ready: Option<(ServiceRequest<P>, ResourceInfo)>,
|
||||
default: Option<HttpService<P>>,
|
||||
}
|
||||
|
||||
impl<P> Service for AppRouting<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type Future = Either<BoxedResponse, FutureResult<Self::Response, Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
if self.ready.is_none() {
|
||||
Ok(Async::Ready(()))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
|
||||
fn call(&mut self, mut req: ServiceRequest<P>) -> Self::Future {
|
||||
let res = self.router.recognize_mut_checked(&mut req, |req, guards| {
|
||||
if let Some(ref guards) = guards {
|
||||
for f in guards {
|
||||
if !f.check(req.head()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
true
|
||||
});
|
||||
|
||||
if let Some((srv, _info)) = res {
|
||||
Either::A(srv.call(req))
|
||||
} else if let Some(ref mut default) = self.default {
|
||||
Either::A(default.call(req))
|
||||
} else {
|
||||
let req = req.into_request();
|
||||
Either::B(ok(ServiceResponse::new(req, Response::NotFound().finish())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrapper service for routing
|
||||
pub struct AppEntry<P> {
|
||||
factory: Rc<RefCell<Option<AppRoutingFactory<P>>>>,
|
||||
}
|
||||
|
||||
impl<P> AppEntry<P> {
|
||||
pub fn new(factory: Rc<RefCell<Option<AppRoutingFactory<P>>>>) -> Self {
|
||||
AppEntry { factory }
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: 'static> NewService for AppEntry<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
type Service = AppRouting<P>;
|
||||
type Future = AppRoutingFactoryResponse<P>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
self.factory.borrow_mut().as_mut().unwrap().new_service(&())
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct AppChain;
|
||||
|
||||
impl NewService for AppChain {
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceRequest;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
type Service = AppChain;
|
||||
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||
|
||||
fn new_service(&self, _: &()) -> Self::Future {
|
||||
ok(AppChain)
|
||||
}
|
||||
}
|
||||
|
||||
impl Service for AppChain {
|
||||
type Request = ServiceRequest;
|
||||
type Response = ServiceRequest;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
#[inline]
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn call(&mut self, req: ServiceRequest) -> Self::Future {
|
||||
ok(req)
|
||||
}
|
||||
}
|
@ -98,9 +98,9 @@ impl<P: 'static> AppConfig<P> {
|
||||
service: F,
|
||||
nested: Option<Rc<ResourceMap>>,
|
||||
) where
|
||||
F: IntoNewService<S, ServiceRequest<P>>,
|
||||
F: IntoNewService<S>,
|
||||
S: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
|
@ -52,11 +52,12 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<F, T, R> NewService<(T, HttpRequest)> for Handler<F, T, R>
|
||||
impl<F, T, R> NewService for Handler<F, T, R>
|
||||
where
|
||||
F: Factory<T, R>,
|
||||
R: Responder + 'static,
|
||||
{
|
||||
type Request = (T, HttpRequest);
|
||||
type Response = ServiceResponse;
|
||||
type Error = Void;
|
||||
type InitError = ();
|
||||
@ -81,11 +82,12 @@ where
|
||||
_t: PhantomData<(T, R)>,
|
||||
}
|
||||
|
||||
impl<F, T, R> Service<(T, HttpRequest)> for HandlerService<F, T, R>
|
||||
impl<F, T, R> Service for HandlerService<F, T, R>
|
||||
where
|
||||
F: Factory<T, R>,
|
||||
R: Responder + 'static,
|
||||
{
|
||||
type Request = (T, HttpRequest);
|
||||
type Response = ServiceResponse;
|
||||
type Error = Void;
|
||||
type Future = HandlerServiceResponse<<R::Future as IntoFuture>::Future>;
|
||||
@ -182,13 +184,14 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<F, T, R> NewService<(T, HttpRequest)> for AsyncHandler<F, T, R>
|
||||
impl<F, T, R> NewService for AsyncHandler<F, T, R>
|
||||
where
|
||||
F: AsyncFactory<T, R>,
|
||||
R: IntoFuture,
|
||||
R::Item: Into<Response>,
|
||||
R::Error: Into<Error>,
|
||||
{
|
||||
type Request = (T, HttpRequest);
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
@ -215,13 +218,14 @@ where
|
||||
_t: PhantomData<(T, R)>,
|
||||
}
|
||||
|
||||
impl<F, T, R> Service<(T, HttpRequest)> for AsyncHandlerService<F, T, R>
|
||||
impl<F, T, R> Service for AsyncHandlerService<F, T, R>
|
||||
where
|
||||
F: AsyncFactory<T, R>,
|
||||
R: IntoFuture,
|
||||
R::Item: Into<Response>,
|
||||
R::Error: Into<Error>,
|
||||
{
|
||||
type Request = (T, HttpRequest);
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type Future = AsyncHandlerServiceResponse<R::Future>;
|
||||
@ -286,7 +290,8 @@ impl<P, T: FromRequest<P>> Extract<P, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<P, T: FromRequest<P>> NewService<ServiceRequest<P>> for Extract<P, T> {
|
||||
impl<P, T: FromRequest<P>> NewService for Extract<P, T> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = (T, HttpRequest);
|
||||
type Error = (Error, ServiceFromRequest<P>);
|
||||
type InitError = ();
|
||||
@ -306,7 +311,8 @@ pub struct ExtractService<P, T: FromRequest<P>> {
|
||||
_t: PhantomData<(P, T)>,
|
||||
}
|
||||
|
||||
impl<P, T: FromRequest<P>> Service<ServiceRequest<P>> for ExtractService<P, T> {
|
||||
impl<P, T: FromRequest<P>> Service for ExtractService<P, T> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = (T, HttpRequest);
|
||||
type Error = (Error, ServiceFromRequest<P>);
|
||||
type Future = ExtractResponse<P, T>;
|
||||
|
@ -1,6 +1,7 @@
|
||||
#![allow(clippy::type_complexity)]
|
||||
|
||||
mod app;
|
||||
mod app_service;
|
||||
mod extract;
|
||||
mod handler;
|
||||
// mod info;
|
||||
|
@ -1,4 +1,5 @@
|
||||
use std::io::Write;
|
||||
use std::marker::PhantomData;
|
||||
use std::str::FromStr;
|
||||
use std::{cmp, fmt, io};
|
||||
|
||||
@ -36,13 +37,14 @@ impl Default for Compress {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, P, B> Transform<S, ServiceRequest<P>> for Compress
|
||||
impl<S, P, B> Transform<S> for Compress
|
||||
where
|
||||
P: 'static,
|
||||
B: MessageBody,
|
||||
S: Service<ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse<Encoder<B>>;
|
||||
type Error = S::Error;
|
||||
type InitError = ();
|
||||
@ -62,13 +64,14 @@ pub struct CompressMiddleware<S> {
|
||||
encoding: ContentEncoding,
|
||||
}
|
||||
|
||||
impl<S, P, B> Service<ServiceRequest<P>> for CompressMiddleware<S>
|
||||
impl<S, P, B> Service for CompressMiddleware<S>
|
||||
where
|
||||
P: 'static,
|
||||
B: MessageBody,
|
||||
S: Service<ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse<Encoder<B>>;
|
||||
type Error = S::Error;
|
||||
type Future = CompressResponse<S, P, B>;
|
||||
@ -92,6 +95,7 @@ where
|
||||
CompressResponse {
|
||||
encoding,
|
||||
fut: self.service.call(req),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -101,18 +105,19 @@ pub struct CompressResponse<S, P, B>
|
||||
where
|
||||
P: 'static,
|
||||
B: MessageBody,
|
||||
S: Service<ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S: Service,
|
||||
S::Future: 'static,
|
||||
{
|
||||
fut: S::Future,
|
||||
encoding: ContentEncoding,
|
||||
_t: PhantomData<(P, B)>,
|
||||
}
|
||||
|
||||
impl<S, P, B> Future for CompressResponse<S, P, B>
|
||||
where
|
||||
P: 'static,
|
||||
B: MessageBody,
|
||||
S: Service<ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Item = ServiceResponse<Encoder<B>>;
|
||||
|
@ -85,11 +85,12 @@ impl DefaultHeaders {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, P, B> Transform<S, ServiceRequest<P>> for DefaultHeaders
|
||||
impl<S, P, B> Transform<S> for DefaultHeaders
|
||||
where
|
||||
S: Service<ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = S::Error;
|
||||
type InitError = ();
|
||||
@ -109,11 +110,12 @@ pub struct DefaultHeadersMiddleware<S> {
|
||||
inner: Rc<Inner>,
|
||||
}
|
||||
|
||||
impl<S, P, B> Service<ServiceRequest<P>> for DefaultHeadersMiddleware<S>
|
||||
impl<S, P, B> Service for DefaultHeadersMiddleware<S>
|
||||
where
|
||||
S: Service<ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse<B>;
|
||||
type Error = S::Error;
|
||||
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
|
||||
|
@ -2,6 +2,7 @@
|
||||
use std::collections::HashSet;
|
||||
use std::env;
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
|
||||
use actix_service::{Service, Transform};
|
||||
@ -110,11 +111,12 @@ impl Default for Logger {
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, P, B> Transform<S, ServiceRequest<P>> for Logger
|
||||
impl<S, P, B> Transform<S> for Logger
|
||||
where
|
||||
S: Service<ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
B: MessageBody,
|
||||
{
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse<StreamLog<B>>;
|
||||
type Error = S::Error;
|
||||
type InitError = ();
|
||||
@ -135,11 +137,12 @@ pub struct LoggerMiddleware<S> {
|
||||
service: S,
|
||||
}
|
||||
|
||||
impl<S, P, B> Service<ServiceRequest<P>> for LoggerMiddleware<S>
|
||||
impl<S, P, B> Service for LoggerMiddleware<S>
|
||||
where
|
||||
S: Service<ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
B: MessageBody,
|
||||
{
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse<StreamLog<B>>;
|
||||
type Error = S::Error;
|
||||
type Future = LoggerResponse<S, P, B>;
|
||||
@ -154,6 +157,7 @@ where
|
||||
fut: self.service.call(req),
|
||||
format: None,
|
||||
time: time::now(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
} else {
|
||||
let now = time::now();
|
||||
@ -166,6 +170,7 @@ where
|
||||
fut: self.service.call(req),
|
||||
format: Some(format),
|
||||
time: now,
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -175,17 +180,18 @@ where
|
||||
pub struct LoggerResponse<S, P, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
S: Service<ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S: Service,
|
||||
{
|
||||
fut: S::Future,
|
||||
time: time::Tm,
|
||||
format: Option<Format>,
|
||||
_t: PhantomData<(P, B)>,
|
||||
}
|
||||
|
||||
impl<S, P, B> Future for LoggerResponse<S, P, B>
|
||||
where
|
||||
B: MessageBody,
|
||||
S: Service<ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
S: Service<Request = ServiceRequest<P>, Response = ServiceResponse<B>>,
|
||||
{
|
||||
type Item = ServiceResponse<StreamLog<B>>;
|
||||
type Error = S::Error;
|
||||
|
@ -66,7 +66,7 @@ impl<P, T> Resource<P, T>
|
||||
where
|
||||
P: 'static,
|
||||
T: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -220,7 +220,7 @@ where
|
||||
) -> Resource<
|
||||
P,
|
||||
impl NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -229,12 +229,12 @@ where
|
||||
where
|
||||
M: Transform<
|
||||
T::Service,
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
F: IntoTransform<M, T::Service, ServiceRequest<P>>,
|
||||
F: IntoTransform<M, T::Service>,
|
||||
{
|
||||
let endpoint = ApplyTransform::new(mw, self.endpoint);
|
||||
Resource {
|
||||
@ -251,9 +251,12 @@ where
|
||||
pub fn default_resource<F, R, U>(mut self, f: F) -> Self
|
||||
where
|
||||
F: FnOnce(Resource<P>) -> R,
|
||||
R: IntoNewService<U, ServiceRequest<P>>,
|
||||
U: NewService<ServiceRequest<P>, Response = ServiceResponse, Error = ()>
|
||||
+ 'static,
|
||||
R: IntoNewService<U>,
|
||||
U: NewService<
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
> + 'static,
|
||||
{
|
||||
// create and configure default resource
|
||||
self.default = Rc::new(RefCell::new(Some(Rc::new(boxed::new_service(
|
||||
@ -268,7 +271,7 @@ impl<P, T> HttpServiceFactory<P> for Resource<P, T>
|
||||
where
|
||||
P: 'static,
|
||||
T: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -292,10 +295,10 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<P, T> IntoNewService<T, ServiceRequest<P>> for Resource<P, T>
|
||||
impl<P, T> IntoNewService<T> for Resource<P, T>
|
||||
where
|
||||
T: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -316,7 +319,8 @@ pub struct ResourceFactory<P> {
|
||||
default: Rc<RefCell<Option<Rc<HttpNewService<P>>>>>,
|
||||
}
|
||||
|
||||
impl<P: 'static> NewService<ServiceRequest<P>> for ResourceFactory<P> {
|
||||
impl<P: 'static> NewService for ResourceFactory<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
@ -406,7 +410,8 @@ pub struct ResourceService<P> {
|
||||
default: Option<HttpService<P>>,
|
||||
}
|
||||
|
||||
impl<P> Service<ServiceRequest<P>> for ResourceService<P> {
|
||||
impl<P> Service for ResourceService<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type Future = Either<
|
||||
@ -450,7 +455,8 @@ impl<P> ResourceEndpoint<P> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: 'static> NewService<ServiceRequest<P>> for ResourceEndpoint<P> {
|
||||
impl<P: 'static> NewService for ResourceEndpoint<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
|
30
src/route.rs
30
src/route.rs
@ -15,7 +15,7 @@ use crate::HttpResponse;
|
||||
|
||||
type BoxedRouteService<Req, Res> = Box<
|
||||
Service<
|
||||
Req,
|
||||
Request = Req,
|
||||
Response = Res,
|
||||
Error = (),
|
||||
Future = Box<Future<Item = Res, Error = ()>>,
|
||||
@ -24,7 +24,7 @@ type BoxedRouteService<Req, Res> = Box<
|
||||
|
||||
type BoxedRouteNewService<Req, Res> = Box<
|
||||
NewService<
|
||||
Req,
|
||||
Request = Req,
|
||||
Response = Res,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -70,7 +70,8 @@ impl<P: 'static> Route<P> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> NewService<ServiceRequest<P>> for Route<P> {
|
||||
impl<P> NewService for Route<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
@ -125,7 +126,8 @@ impl<P> RouteService<P> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> Service<ServiceRequest<P>> for RouteService<P> {
|
||||
impl<P> Service for RouteService<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
|
||||
@ -330,7 +332,7 @@ impl<P: 'static> Route<P> {
|
||||
|
||||
struct RouteNewService<P, T>
|
||||
where
|
||||
T: NewService<ServiceRequest<P>, Error = (Error, ServiceFromRequest<P>)>,
|
||||
T: NewService<Request = ServiceRequest<P>, Error = (Error, ServiceFromRequest<P>)>,
|
||||
{
|
||||
service: T,
|
||||
_t: PhantomData<P>,
|
||||
@ -339,13 +341,13 @@ where
|
||||
impl<P: 'static, T> RouteNewService<P, T>
|
||||
where
|
||||
T: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (Error, ServiceFromRequest<P>),
|
||||
>,
|
||||
T::Future: 'static,
|
||||
T::Service: 'static,
|
||||
<T::Service as Service<ServiceRequest<P>>>::Future: 'static,
|
||||
<T::Service as Service>::Future: 'static,
|
||||
{
|
||||
pub fn new(service: T) -> Self {
|
||||
RouteNewService {
|
||||
@ -355,17 +357,18 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: 'static, T> NewService<ServiceRequest<P>> for RouteNewService<P, T>
|
||||
impl<P: 'static, T> NewService for RouteNewService<P, T>
|
||||
where
|
||||
T: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (Error, ServiceFromRequest<P>),
|
||||
>,
|
||||
T::Future: 'static,
|
||||
T::Service: 'static,
|
||||
<T::Service as Service<ServiceRequest<P>>>::Future: 'static,
|
||||
<T::Service as Service>::Future: 'static,
|
||||
{
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
@ -389,20 +392,21 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
struct RouteServiceWrapper<P, T: Service<ServiceRequest<P>>> {
|
||||
struct RouteServiceWrapper<P, T: Service> {
|
||||
service: T,
|
||||
_t: PhantomData<P>,
|
||||
}
|
||||
|
||||
impl<P, T> Service<ServiceRequest<P>> for RouteServiceWrapper<P, T>
|
||||
impl<P, T> Service for RouteServiceWrapper<P, T>
|
||||
where
|
||||
T::Future: 'static,
|
||||
T: Service<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (Error, ServiceFromRequest<P>),
|
||||
>,
|
||||
{
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type Future = Box<Future<Item = Self::Response, Error = Self::Error>>;
|
||||
|
21
src/scope.rs
21
src/scope.rs
@ -81,7 +81,7 @@ impl<P, T> Scope<P, T>
|
||||
where
|
||||
P: 'static,
|
||||
T: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -174,7 +174,7 @@ where
|
||||
where
|
||||
F: FnOnce(Resource<P>) -> Resource<P, U>,
|
||||
U: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -199,7 +199,7 @@ where
|
||||
) -> Scope<
|
||||
P,
|
||||
impl NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -208,12 +208,12 @@ where
|
||||
where
|
||||
M: Transform<
|
||||
T::Service,
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
>,
|
||||
F: IntoTransform<M, T::Service, ServiceRequest<P>>,
|
||||
F: IntoTransform<M, T::Service>,
|
||||
{
|
||||
let endpoint = ApplyTransform::new(mw, self.endpoint);
|
||||
Scope {
|
||||
@ -231,7 +231,7 @@ impl<P, T> HttpServiceFactory<P> for Scope<P, T>
|
||||
where
|
||||
P: 'static,
|
||||
T: NewService<
|
||||
ServiceRequest<P>,
|
||||
Request = ServiceRequest<P>,
|
||||
Response = ServiceResponse,
|
||||
Error = (),
|
||||
InitError = (),
|
||||
@ -287,7 +287,8 @@ pub struct ScopeFactory<P> {
|
||||
default: Rc<RefCell<Option<Rc<HttpNewService<P>>>>>,
|
||||
}
|
||||
|
||||
impl<P: 'static> NewService<ServiceRequest<P>> for ScopeFactory<P> {
|
||||
impl<P: 'static> NewService for ScopeFactory<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
@ -402,7 +403,8 @@ pub struct ScopeService<P> {
|
||||
_ready: Option<(ServiceRequest<P>, ResourceInfo)>,
|
||||
}
|
||||
|
||||
impl<P> Service<ServiceRequest<P>> for ScopeService<P> {
|
||||
impl<P> Service for ScopeService<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type Future = Either<BoxedResponse, FutureResult<Self::Response, Self::Error>>;
|
||||
@ -445,7 +447,8 @@ impl<P> ScopeEndpoint<P> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: 'static> NewService<ServiceRequest<P>> for ScopeEndpoint<P> {
|
||||
impl<P: 'static> NewService for ScopeEndpoint<P> {
|
||||
type Request = ServiceRequest<P>;
|
||||
type Response = ServiceResponse;
|
||||
type Error = ();
|
||||
type InitError = ();
|
||||
|
@ -7,6 +7,7 @@ use actix_http::{
|
||||
};
|
||||
use actix_rt::System;
|
||||
use actix_server::{Server, ServerBuilder};
|
||||
use actix_server_config::ServerConfig;
|
||||
use actix_service::{IntoNewService, NewService};
|
||||
use parking_lot::Mutex;
|
||||
|
||||
@ -53,8 +54,8 @@ struct Config {
|
||||
pub struct HttpServer<F, I, S, B>
|
||||
where
|
||||
F: Fn() -> I + Send + Clone + 'static,
|
||||
I: IntoNewService<S, Request>,
|
||||
S: NewService<Request>,
|
||||
I: IntoNewService<S, ServerConfig>,
|
||||
S: NewService<ServerConfig, Request = Request>,
|
||||
S::Error: fmt::Debug,
|
||||
S::Response: Into<Response<B>>,
|
||||
S::Service: 'static,
|
||||
@ -72,8 +73,8 @@ where
|
||||
impl<F, I, S, B> HttpServer<F, I, S, B>
|
||||
where
|
||||
F: Fn() -> I + Send + Clone + 'static,
|
||||
I: IntoNewService<S, Request>,
|
||||
S: NewService<Request>,
|
||||
I: IntoNewService<S, ServerConfig>,
|
||||
S: NewService<ServerConfig, Request = Request>,
|
||||
S::Error: fmt::Debug + 'static,
|
||||
S::Response: Into<Response<B>>,
|
||||
S::Service: 'static,
|
||||
@ -432,8 +433,8 @@ where
|
||||
impl<F, I, S, B> HttpServer<F, I, S, B>
|
||||
where
|
||||
F: Fn() -> I + Send + Clone + 'static,
|
||||
I: IntoNewService<S, Request>,
|
||||
S: NewService<Request>,
|
||||
I: IntoNewService<S, ServerConfig>,
|
||||
S: NewService<ServerConfig, Request = Request>,
|
||||
S::Error: fmt::Debug,
|
||||
S::Response: Into<Response<B>>,
|
||||
S::Service: 'static,
|
||||
|
17
src/test.rs
17
src/test.rs
@ -8,6 +8,7 @@ use actix_http::test::TestRequest as HttpTestRequest;
|
||||
use actix_http::{Extensions, PayloadStream, Request};
|
||||
use actix_router::{Path, ResourceDef, Url};
|
||||
use actix_rt::Runtime;
|
||||
use actix_server_config::ServerConfig;
|
||||
use actix_service::{IntoNewService, NewService, Service};
|
||||
use bytes::Bytes;
|
||||
use futures::Future;
|
||||
@ -62,13 +63,19 @@ where
|
||||
/// ```
|
||||
pub fn init_service<R, S, B, E>(
|
||||
app: R,
|
||||
) -> impl Service<Request, Response = ServiceResponse<B>, Error = E>
|
||||
) -> impl Service<Request = Request, Response = ServiceResponse<B>, Error = E>
|
||||
where
|
||||
R: IntoNewService<S, Request, ()>,
|
||||
S: NewService<Request, Response = ServiceResponse<B>, Error = E>,
|
||||
R: IntoNewService<S, ServerConfig>,
|
||||
S: NewService<
|
||||
ServerConfig,
|
||||
Request = Request,
|
||||
Response = ServiceResponse<B>,
|
||||
Error = E,
|
||||
>,
|
||||
S::InitError: std::fmt::Debug,
|
||||
{
|
||||
block_on(app.into_new_service().new_service(&())).unwrap()
|
||||
let cfg = ServerConfig::new("127.0.0.1:8080".parse().unwrap());
|
||||
block_on(app.into_new_service().new_service(&cfg)).unwrap()
|
||||
}
|
||||
|
||||
/// Calls service and waits for response future completion.
|
||||
@ -93,7 +100,7 @@ where
|
||||
/// ```
|
||||
pub fn call_success<S, R, B, E>(app: &mut S, req: R) -> S::Response
|
||||
where
|
||||
S: Service<R, Response = ServiceResponse<B>, Error = E>,
|
||||
S: Service<Request = R, Response = ServiceResponse<B>, Error = E>,
|
||||
E: std::fmt::Debug,
|
||||
{
|
||||
block_on(app.call(req)).unwrap()
|
||||
|
Loading…
Reference in New Issue
Block a user