2019-03-09 18:49:11 +01:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
use actix_http::{Request, Response};
|
2019-03-09 18:49:11 +01:00
|
|
|
use actix_router::{Path, ResourceDef, ResourceInfo, Router, Url};
|
|
|
|
use actix_server_config::ServerConfig;
|
|
|
|
use actix_service::boxed::{self, BoxedNewService, BoxedService};
|
2019-04-13 23:50:54 +02:00
|
|
|
use actix_service::{fn_service, NewService, Service};
|
2019-03-09 18:49:11 +01:00
|
|
|
use futures::future::{ok, Either, FutureResult};
|
|
|
|
use futures::{Async, Future, Poll};
|
|
|
|
|
2019-04-15 16:32:49 +02:00
|
|
|
use crate::config::{AppConfig, AppService};
|
2019-03-17 04:17:27 +01:00
|
|
|
use crate::data::{DataFactory, DataFactoryResult};
|
2019-03-11 00:35:38 +01:00
|
|
|
use crate::error::Error;
|
2019-03-09 18:49:11 +01:00
|
|
|
use crate::guard::Guard;
|
2019-04-08 08:06:21 +02:00
|
|
|
use crate::request::{HttpRequest, HttpRequestPool};
|
2019-03-09 18:49:11 +01:00
|
|
|
use crate::rmap::ResourceMap;
|
|
|
|
use crate::service::{ServiceFactory, ServiceRequest, ServiceResponse};
|
|
|
|
|
|
|
|
type Guards = Vec<Box<Guard>>;
|
2019-04-13 23:50:54 +02:00
|
|
|
type HttpService = BoxedService<ServiceRequest, ServiceResponse, Error>;
|
|
|
|
type HttpNewService = BoxedNewService<(), ServiceRequest, ServiceResponse, Error, ()>;
|
2019-04-08 08:06:21 +02:00
|
|
|
type BoxedResponse = Either<
|
|
|
|
FutureResult<ServiceResponse, Error>,
|
|
|
|
Box<Future<Item = ServiceResponse, Error = Error>>,
|
|
|
|
>;
|
2019-03-09 18:49:11 +01:00
|
|
|
|
|
|
|
/// Service factory to convert `Request` to a `ServiceRequest<S>`.
|
2019-03-17 04:17:27 +01:00
|
|
|
/// It also executes data factories.
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct AppInit<T, B>
|
2019-03-09 18:49:11 +01:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-09 18:49:11 +01:00
|
|
|
Response = ServiceResponse<B>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-09 18:49:11 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
pub(crate) endpoint: T,
|
2019-03-17 04:17:27 +01:00
|
|
|
pub(crate) data: Vec<Box<DataFactory>>,
|
2019-03-09 23:06:24 +01:00
|
|
|
pub(crate) config: RefCell<AppConfig>,
|
2019-04-13 23:50:54 +02:00
|
|
|
pub(crate) services: RefCell<Vec<Box<ServiceFactory>>>,
|
|
|
|
pub(crate) default: Option<Rc<HttpNewService>>,
|
|
|
|
pub(crate) factory_ref: Rc<RefCell<Option<AppRoutingFactory>>>,
|
2019-03-09 23:06:24 +01:00
|
|
|
pub(crate) external: RefCell<Vec<ResourceDef>>,
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl<T, B> NewService<ServerConfig> for AppInit<T, B>
|
2019-03-09 18:49:11 +01:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-09 18:49:11 +01:00
|
|
|
Response = ServiceResponse<B>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-09 18:49:11 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
type Request = Request;
|
|
|
|
type Response = ServiceResponse<B>;
|
2019-04-13 23:50:54 +02:00
|
|
|
type Error = T::Error;
|
|
|
|
type InitError = T::InitError;
|
|
|
|
type Service = AppInitService<T::Service, B>;
|
|
|
|
type Future = AppInitResult<T, B>;
|
2019-03-09 18:49:11 +01:00
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
fn new_service(&self, cfg: &ServerConfig) -> Self::Future {
|
2019-03-09 18:49:11 +01:00
|
|
|
// update resource default service
|
|
|
|
let default = self.default.clone().unwrap_or_else(|| {
|
2019-04-13 23:50:54 +02:00
|
|
|
Rc::new(boxed::new_service(fn_service(|req: ServiceRequest| {
|
2019-03-09 18:49:11 +01:00
|
|
|
Ok(req.into_response(Response::NotFound().finish()))
|
|
|
|
})))
|
|
|
|
});
|
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
{
|
|
|
|
let mut c = self.config.borrow_mut();
|
|
|
|
let loc_cfg = Rc::get_mut(&mut c.0).unwrap();
|
|
|
|
loc_cfg.secure = cfg.secure();
|
|
|
|
loc_cfg.addr = cfg.local_addr();
|
|
|
|
}
|
|
|
|
|
2019-04-15 16:32:49 +02:00
|
|
|
let mut config = AppService::new(self.config.borrow().clone(), default.clone());
|
2019-03-09 18:49:11 +01:00
|
|
|
|
|
|
|
// 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(),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2019-03-09 23:06:24 +01:00
|
|
|
// external resources
|
|
|
|
for mut rdef in std::mem::replace(&mut *self.external.borrow_mut(), Vec::new()) {
|
|
|
|
rmap.add(&mut rdef, None);
|
|
|
|
}
|
|
|
|
|
2019-03-09 18:49:11 +01:00
|
|
|
// complete ResourceMap tree creation
|
|
|
|
let rmap = Rc::new(rmap);
|
|
|
|
rmap.finish(rmap.clone());
|
|
|
|
|
|
|
|
AppInitResult {
|
|
|
|
endpoint: None,
|
|
|
|
endpoint_fut: self.endpoint.new_service(&()),
|
2019-03-17 04:17:27 +01:00
|
|
|
data: self.data.iter().map(|s| s.construct()).collect(),
|
2019-03-09 23:06:24 +01:00
|
|
|
config: self.config.borrow().clone(),
|
2019-03-09 18:49:11 +01:00
|
|
|
rmap,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct AppInitResult<T, B>
|
2019-03-09 18:49:11 +01:00
|
|
|
where
|
|
|
|
T: NewService,
|
|
|
|
{
|
|
|
|
endpoint: Option<T::Service>,
|
|
|
|
endpoint_fut: T::Future,
|
|
|
|
rmap: Rc<ResourceMap>,
|
2019-03-17 04:17:27 +01:00
|
|
|
data: Vec<Box<DataFactoryResult>>,
|
2019-03-09 23:06:24 +01:00
|
|
|
config: AppConfig,
|
2019-04-13 23:50:54 +02:00
|
|
|
_t: PhantomData<B>,
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl<T, B> Future for AppInitResult<T, B>
|
2019-03-09 18:49:11 +01:00
|
|
|
where
|
|
|
|
T: NewService<
|
2019-04-13 23:50:54 +02:00
|
|
|
Request = ServiceRequest,
|
2019-03-09 18:49:11 +01:00
|
|
|
Response = ServiceResponse<B>,
|
2019-03-11 00:35:38 +01:00
|
|
|
Error = Error,
|
2019-03-09 18:49:11 +01:00
|
|
|
InitError = (),
|
|
|
|
>,
|
|
|
|
{
|
2019-04-13 23:50:54 +02:00
|
|
|
type Item = AppInitService<T::Service, B>;
|
|
|
|
type Error = T::InitError;
|
2019-03-09 18:49:11 +01:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2019-03-09 23:06:24 +01:00
|
|
|
let mut idx = 0;
|
|
|
|
let mut extensions = self.config.0.extensions.borrow_mut();
|
2019-03-17 04:17:27 +01:00
|
|
|
while idx < self.data.len() {
|
|
|
|
if let Async::Ready(_) = self.data[idx].poll_result(&mut extensions)? {
|
|
|
|
self.data.remove(idx);
|
2019-03-09 23:06:24 +01:00
|
|
|
} else {
|
|
|
|
idx += 1;
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.endpoint.is_none() {
|
|
|
|
if let Async::Ready(srv) = self.endpoint_fut.poll()? {
|
|
|
|
self.endpoint = Some(srv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
if self.endpoint.is_some() {
|
|
|
|
Ok(Async::Ready(AppInitService {
|
|
|
|
service: self.endpoint.take().unwrap(),
|
|
|
|
rmap: self.rmap.clone(),
|
|
|
|
config: self.config.clone(),
|
|
|
|
pool: HttpRequestPool::create(),
|
|
|
|
}))
|
2019-03-09 18:49:11 +01:00
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Service to convert `Request` to a `ServiceRequest<S>`
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct AppInitService<T: Service, B>
|
2019-03-09 18:49:11 +01:00
|
|
|
where
|
2019-04-13 23:50:54 +02:00
|
|
|
T: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
2019-03-09 18:49:11 +01:00
|
|
|
{
|
2019-04-13 23:50:54 +02:00
|
|
|
service: T,
|
2019-03-09 18:49:11 +01:00
|
|
|
rmap: Rc<ResourceMap>,
|
2019-03-09 23:06:24 +01:00
|
|
|
config: AppConfig,
|
2019-04-08 08:06:21 +02:00
|
|
|
pool: &'static HttpRequestPool,
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl<T, B> Service for AppInitService<T, B>
|
2019-03-09 18:49:11 +01:00
|
|
|
where
|
2019-04-13 23:50:54 +02:00
|
|
|
T: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
2019-03-09 18:49:11 +01:00
|
|
|
{
|
|
|
|
type Request = Request;
|
2019-04-13 23:50:54 +02:00
|
|
|
type Response = ServiceResponse<B>;
|
|
|
|
type Error = T::Error;
|
|
|
|
type Future = T::Future;
|
2019-03-09 18:49:11 +01:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2019-04-13 23:50:54 +02:00
|
|
|
self.service.poll_ready()
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Request) -> Self::Future {
|
2019-04-08 08:06:21 +02:00
|
|
|
let (head, payload) = req.into_parts();
|
|
|
|
|
|
|
|
let req = if let Some(mut req) = self.pool.get_request() {
|
|
|
|
let inner = Rc::get_mut(&mut req.0).unwrap();
|
|
|
|
inner.path.get_mut().update(&head.uri);
|
|
|
|
inner.path.reset();
|
|
|
|
inner.head = head;
|
|
|
|
req
|
|
|
|
} else {
|
|
|
|
HttpRequest::new(
|
|
|
|
Path::new(Url::new(head.uri.clone())),
|
|
|
|
head,
|
|
|
|
self.rmap.clone(),
|
|
|
|
self.config.clone(),
|
|
|
|
self.pool,
|
|
|
|
)
|
|
|
|
};
|
2019-04-13 23:50:54 +02:00
|
|
|
self.service.call(ServiceRequest::from_parts(req, payload))
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct AppRoutingFactory {
|
|
|
|
services: Rc<Vec<(ResourceDef, HttpNewService, RefCell<Option<Guards>>)>>,
|
|
|
|
default: Rc<HttpNewService>,
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl NewService for AppRoutingFactory {
|
|
|
|
type Request = ServiceRequest;
|
2019-03-09 18:49:11 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2019-03-09 18:49:11 +01:00
|
|
|
type InitError = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Service = AppRouting;
|
|
|
|
type Future = AppRoutingFactoryResponse;
|
2019-03-09 18:49:11 +01:00
|
|
|
|
|
|
|
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(&())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
type HttpServiceFut = Box<Future<Item = HttpService, Error = ()>>;
|
2019-03-09 18:49:11 +01:00
|
|
|
|
|
|
|
/// Create app service
|
|
|
|
#[doc(hidden)]
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct AppRoutingFactoryResponse {
|
|
|
|
fut: Vec<CreateAppRoutingItem>,
|
|
|
|
default: Option<HttpService>,
|
|
|
|
default_fut: Option<Box<Future<Item = HttpService, Error = ()>>>,
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
enum CreateAppRoutingItem {
|
|
|
|
Future(Option<ResourceDef>, Option<Guards>, HttpServiceFut),
|
|
|
|
Service(ResourceDef, Option<Guards>, HttpService),
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl Future for AppRoutingFactoryResponse {
|
|
|
|
type Item = AppRouting;
|
2019-03-09 18:49:11 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct AppRouting {
|
|
|
|
router: Router<HttpService, Guards>,
|
|
|
|
ready: Option<(ServiceRequest, ResourceInfo)>,
|
|
|
|
default: Option<HttpService>,
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl Service for AppRouting {
|
|
|
|
type Request = ServiceRequest;
|
2019-03-09 18:49:11 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2019-04-08 08:06:21 +02:00
|
|
|
type Future = BoxedResponse;
|
2019-03-09 18:49:11 +01:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
if self.ready.is_none() {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
|
2019-03-09 18:49:11 +01:00
|
|
|
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 {
|
2019-04-08 08:06:21 +02:00
|
|
|
srv.call(req)
|
2019-03-09 18:49:11 +01:00
|
|
|
} else if let Some(ref mut default) = self.default {
|
2019-04-08 08:06:21 +02:00
|
|
|
default.call(req)
|
2019-03-09 18:49:11 +01:00
|
|
|
} else {
|
2019-03-26 23:14:32 +01:00
|
|
|
let req = req.into_parts().0;
|
2019-04-08 08:06:21 +02:00
|
|
|
Either::A(ok(ServiceResponse::new(req, Response::NotFound().finish())))
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wrapper service for routing
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct AppEntry {
|
|
|
|
factory: Rc<RefCell<Option<AppRoutingFactory>>>,
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl AppEntry {
|
|
|
|
pub fn new(factory: Rc<RefCell<Option<AppRoutingFactory>>>) -> Self {
|
2019-03-09 18:49:11 +01:00
|
|
|
AppEntry { factory }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-13 23:50:54 +02:00
|
|
|
impl NewService for AppEntry {
|
|
|
|
type Request = ServiceRequest;
|
2019-03-09 18:49:11 +01:00
|
|
|
type Response = ServiceResponse;
|
2019-03-11 00:35:38 +01:00
|
|
|
type Error = Error;
|
2019-03-09 18:49:11 +01:00
|
|
|
type InitError = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
type Service = AppRouting;
|
|
|
|
type Future = AppRoutingFactoryResponse;
|
2019-03-09 18:49:11 +01:00
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
|
|
|
self.factory.borrow_mut().as_mut().unwrap().new_service(&())
|
|
|
|
}
|
|
|
|
}
|