2019-03-09 18:49:11 +01:00
|
|
|
use std::cell::RefCell;
|
2019-11-20 18:33:22 +01:00
|
|
|
use std::future::Future;
|
2019-03-09 18:49:11 +01:00
|
|
|
use std::marker::PhantomData;
|
2019-11-20 18:33:22 +01:00
|
|
|
use std::pin::Pin;
|
2019-03-09 18:49:11 +01:00
|
|
|
use std::rc::Rc;
|
2019-11-20 18:33:22 +01:00
|
|
|
use std::task::{Context, Poll};
|
2019-03-09 18:49:11 +01:00
|
|
|
|
2019-05-05 04:43:49 +02:00
|
|
|
use actix_http::{Extensions, Request, Response};
|
2019-03-09 18:49:11 +01:00
|
|
|
use actix_router::{Path, ResourceDef, ResourceInfo, Router, Url};
|
2019-11-26 06:25:50 +01:00
|
|
|
use actix_service::boxed::{self, BoxService, BoxServiceFactory};
|
2019-12-08 14:25:24 +01:00
|
|
|
use actix_service::{fn_service, Service, ServiceFactory};
|
2019-11-26 06:25:50 +01:00
|
|
|
use futures::future::{ok, FutureExt, LocalBoxFuture};
|
2019-03-09 18:49:11 +01:00
|
|
|
|
2019-04-15 16:32:49 +02:00
|
|
|
use crate::config::{AppConfig, AppService};
|
2019-05-05 04:43:49 +02:00
|
|
|
use crate::data::DataFactory;
|
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;
|
2019-11-20 18:33:22 +01:00
|
|
|
use crate::service::{AppServiceFactory, ServiceRequest, ServiceResponse};
|
2019-03-09 18:49:11 +01:00
|
|
|
|
2019-07-17 11:48:37 +02:00
|
|
|
type Guards = Vec<Box<dyn Guard>>;
|
2019-11-26 06:25:50 +01:00
|
|
|
type HttpService = BoxService<ServiceRequest, ServiceResponse, Error>;
|
|
|
|
type HttpNewService = BoxServiceFactory<(), ServiceRequest, ServiceResponse, Error, ()>;
|
|
|
|
type BoxResponse = LocalBoxFuture<'static, Result<ServiceResponse, Error>>;
|
2019-07-17 11:48:37 +02:00
|
|
|
type FnDataFactory =
|
2019-11-20 18:33:22 +01:00
|
|
|
Box<dyn Fn() -> LocalBoxFuture<'static, Result<Box<dyn DataFactory>, ()>>>;
|
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
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory<
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
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-12-20 12:13:09 +01:00
|
|
|
pub(crate) extensions: RefCell<Option<Extensions>>,
|
2019-07-17 11:48:37 +02:00
|
|
|
pub(crate) data: Rc<Vec<Box<dyn DataFactory>>>,
|
2019-06-28 06:43:52 +02:00
|
|
|
pub(crate) data_factories: Rc<Vec<FnDataFactory>>,
|
2019-11-20 18:33:22 +01:00
|
|
|
pub(crate) services: Rc<RefCell<Vec<Box<dyn AppServiceFactory>>>>,
|
2019-04-13 23:50:54 +02:00
|
|
|
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-11-20 18:33:22 +01:00
|
|
|
impl<T, B> ServiceFactory for AppInit<T, B>
|
2019-03-09 18:49:11 +01:00
|
|
|
where
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory<
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
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-12-20 11:04:51 +01:00
|
|
|
type Config = AppConfig;
|
2019-03-09 18:49:11 +01:00
|
|
|
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-12-20 11:04:51 +01:00
|
|
|
fn new_service(&self, config: AppConfig) -> Self::Future {
|
2019-03-09 18:49:11 +01:00
|
|
|
// update resource default service
|
|
|
|
let default = self.default.clone().unwrap_or_else(|| {
|
2019-12-08 14:25:24 +01:00
|
|
|
Rc::new(boxed::factory(fn_service(|req: ServiceRequest| {
|
2019-11-20 18:33:22 +01:00
|
|
|
ok(req.into_response(Response::NotFound().finish()))
|
2019-03-09 18:49:11 +01:00
|
|
|
})))
|
|
|
|
});
|
|
|
|
|
2019-05-05 04:43:49 +02:00
|
|
|
// App config
|
2019-12-20 11:04:51 +01:00
|
|
|
let mut config = AppService::new(config, default.clone(), self.data.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(""));
|
|
|
|
|
2019-05-05 04:43:49 +02:00
|
|
|
let (config, services) = config.into_services();
|
|
|
|
|
2019-03-09 18:49:11 +01:00
|
|
|
// complete pipeline creation
|
|
|
|
*self.factory_ref.borrow_mut() = Some(AppRoutingFactory {
|
|
|
|
default,
|
|
|
|
services: Rc::new(
|
2019-05-05 04:43:49 +02:00
|
|
|
services
|
2019-03-09 18:49:11 +01:00
|
|
|
.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,
|
2019-12-02 16:37:13 +01:00
|
|
|
endpoint_fut: self.endpoint.new_service(()),
|
2019-06-28 06:43:52 +02:00
|
|
|
data: self.data.clone(),
|
|
|
|
data_factories: Vec::new(),
|
|
|
|
data_factories_fut: self.data_factories.iter().map(|f| f()).collect(),
|
2019-12-20 12:13:09 +01:00
|
|
|
extensions: Some(
|
|
|
|
self.extensions
|
|
|
|
.borrow_mut()
|
|
|
|
.take()
|
|
|
|
.unwrap_or_else(Extensions::new),
|
|
|
|
),
|
2019-05-05 04:43:49 +02:00
|
|
|
config,
|
2019-03-09 18:49:11 +01:00
|
|
|
rmap,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
#[pin_project::pin_project]
|
2019-04-13 23:50:54 +02:00
|
|
|
pub struct AppInitResult<T, B>
|
2019-03-09 18:49:11 +01:00
|
|
|
where
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory,
|
2019-03-09 18:49:11 +01:00
|
|
|
{
|
|
|
|
endpoint: Option<T::Service>,
|
2019-11-20 18:33:22 +01:00
|
|
|
#[pin]
|
2019-03-09 18:49:11 +01:00
|
|
|
endpoint_fut: T::Future,
|
|
|
|
rmap: Rc<ResourceMap>,
|
2019-03-09 23:06:24 +01:00
|
|
|
config: AppConfig,
|
2019-07-17 11:48:37 +02:00
|
|
|
data: Rc<Vec<Box<dyn DataFactory>>>,
|
|
|
|
data_factories: Vec<Box<dyn DataFactory>>,
|
2019-11-20 18:33:22 +01:00
|
|
|
data_factories_fut: Vec<LocalBoxFuture<'static, Result<Box<dyn DataFactory>, ()>>>,
|
2019-12-20 12:13:09 +01:00
|
|
|
extensions: Option<Extensions>,
|
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
|
2019-11-20 18:33:22 +01:00
|
|
|
T: ServiceFactory<
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
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-11-20 18:33:22 +01:00
|
|
|
type Output = Result<AppInitService<T::Service, B>, ()>;
|
|
|
|
|
2019-12-07 19:46:51 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-20 18:33:22 +01:00
|
|
|
let this = self.project();
|
2019-03-09 18:49:11 +01:00
|
|
|
|
2019-06-28 06:43:52 +02:00
|
|
|
// async data factories
|
|
|
|
let mut idx = 0;
|
2019-11-20 18:33:22 +01:00
|
|
|
while idx < this.data_factories_fut.len() {
|
|
|
|
match Pin::new(&mut this.data_factories_fut[idx]).poll(cx)? {
|
|
|
|
Poll::Ready(f) => {
|
|
|
|
this.data_factories.push(f);
|
|
|
|
let _ = this.data_factories_fut.remove(idx);
|
2019-06-28 06:43:52 +02:00
|
|
|
}
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Pending => idx += 1,
|
2019-06-28 06:43:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
if this.endpoint.is_none() {
|
|
|
|
if let Poll::Ready(srv) = this.endpoint_fut.poll(cx)? {
|
|
|
|
*this.endpoint = Some(srv);
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
if this.endpoint.is_some() && this.data_factories_fut.is_empty() {
|
2019-06-28 06:43:52 +02:00
|
|
|
// create app data container
|
2019-12-20 12:13:09 +01:00
|
|
|
let mut data = this.extensions.take().unwrap();
|
2019-11-20 18:33:22 +01:00
|
|
|
for f in this.data.iter() {
|
2019-06-28 06:43:52 +02:00
|
|
|
f.create(&mut data);
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
for f in this.data_factories.iter() {
|
2019-06-28 06:43:52 +02:00
|
|
|
f.create(&mut data);
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Ready(Ok(AppInitService {
|
|
|
|
service: this.endpoint.take().unwrap(),
|
|
|
|
rmap: this.rmap.clone(),
|
|
|
|
config: this.config.clone(),
|
2019-06-28 06:43:52 +02:00
|
|
|
data: Rc::new(data),
|
2019-04-13 23:50:54 +02:00
|
|
|
pool: HttpRequestPool::create(),
|
|
|
|
}))
|
2019-03-09 18:49:11 +01:00
|
|
|
} else {
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Pending
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Service to convert `Request` to a `ServiceRequest<S>`
|
2019-05-22 20:18:33 +02:00
|
|
|
pub struct 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
|
|
|
{
|
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-05-05 04:43:49 +02:00
|
|
|
data: Rc<Extensions>,
|
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
|
|
|
|
2019-12-07 19:46:51 +01:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-11-20 18:33:22 +01:00
|
|
|
self.service.poll_ready(cx)
|
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;
|
2019-05-22 20:49:27 +02:00
|
|
|
inner.payload = payload;
|
2019-05-05 04:43:49 +02:00
|
|
|
inner.app_data = self.data.clone();
|
2019-04-08 08:06:21 +02:00
|
|
|
req
|
|
|
|
} else {
|
|
|
|
HttpRequest::new(
|
|
|
|
Path::new(Url::new(head.uri.clone())),
|
|
|
|
head,
|
2019-05-22 20:49:27 +02:00
|
|
|
payload,
|
2019-04-08 08:06:21 +02:00
|
|
|
self.rmap.clone(),
|
|
|
|
self.config.clone(),
|
2019-05-05 04:43:49 +02:00
|
|
|
self.data.clone(),
|
2019-04-08 08:06:21 +02:00
|
|
|
self.pool,
|
|
|
|
)
|
|
|
|
};
|
2019-05-22 20:49:27 +02:00
|
|
|
self.service.call(ServiceRequest::new(req))
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 20:18:33 +02:00
|
|
|
impl<T, B> Drop for AppInitService<T, B>
|
|
|
|
where
|
|
|
|
T: Service<Request = ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
|
|
|
|
{
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.pool.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-20 18:33:22 +01:00
|
|
|
impl ServiceFactory for AppRoutingFactory {
|
2019-05-12 17:34:51 +02:00
|
|
|
type Config = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
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
|
|
|
|
2019-12-02 16:37:13 +01:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
2019-03-09 18:49:11 +01:00
|
|
|
AppRoutingFactoryResponse {
|
|
|
|
fut: self
|
|
|
|
.services
|
|
|
|
.iter()
|
|
|
|
.map(|(path, service, guards)| {
|
|
|
|
CreateAppRoutingItem::Future(
|
|
|
|
Some(path.clone()),
|
|
|
|
guards.borrow_mut().take(),
|
2019-12-02 16:37:13 +01:00
|
|
|
service.new_service(()).boxed_local(),
|
2019-03-09 18:49:11 +01:00
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
default: None,
|
2019-12-02 16:37:13 +01:00
|
|
|
default_fut: Some(self.default.new_service(())),
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:33:22 +01:00
|
|
|
type HttpServiceFut = LocalBoxFuture<'static, Result<HttpService, ()>>;
|
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>,
|
2019-11-20 18:33:22 +01:00
|
|
|
default_fut: Option<LocalBoxFuture<'static, Result<HttpService, ()>>>,
|
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 {
|
2019-11-20 18:33:22 +01:00
|
|
|
type Output = Result<AppRouting, ()>;
|
2019-03-09 18:49:11 +01:00
|
|
|
|
2019-12-07 19:46:51 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-03-09 18:49:11 +01:00
|
|
|
let mut done = true;
|
|
|
|
|
|
|
|
if let Some(ref mut fut) = self.default_fut {
|
2019-11-20 18:33:22 +01:00
|
|
|
match Pin::new(fut).poll(cx)? {
|
|
|
|
Poll::Ready(default) => self.default = Some(default),
|
|
|
|
Poll::Pending => done = false,
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// poll http services
|
|
|
|
for item in &mut self.fut {
|
|
|
|
let res = match item {
|
|
|
|
CreateAppRoutingItem::Future(
|
|
|
|
ref mut path,
|
|
|
|
ref mut guards,
|
|
|
|
ref mut fut,
|
2019-11-20 18:33:22 +01:00
|
|
|
) => match Pin::new(fut).poll(cx) {
|
|
|
|
Poll::Ready(Ok(service)) => {
|
2019-03-09 18:49:11 +01:00
|
|
|
Some((path.take().unwrap(), guards.take(), service))
|
|
|
|
}
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Ready(Err(_)) => return Poll::Ready(Err(())),
|
|
|
|
Poll::Pending => {
|
2019-03-09 18:49:11 +01:00
|
|
|
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
|
|
|
|
});
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Ready(Ok(AppRouting {
|
2019-03-09 18:49:11 +01:00
|
|
|
ready: None,
|
|
|
|
router: router.finish(),
|
|
|
|
default: self.default.take(),
|
|
|
|
}))
|
|
|
|
} else {
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Pending
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-26 06:25:50 +01:00
|
|
|
type Future = BoxResponse;
|
2019-03-09 18:49:11 +01:00
|
|
|
|
2019-12-07 19:46:51 +01:00
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-03-09 18:49:11 +01:00
|
|
|
if self.ready.is_none() {
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Ready(Ok(()))
|
2019-03-09 18:49:11 +01:00
|
|
|
} else {
|
2019-11-20 18:33:22 +01:00
|
|
|
Poll::Pending
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-20 18:33:22 +01:00
|
|
|
ok(ServiceResponse::new(req, Response::NotFound().finish())).boxed_local()
|
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-11-20 18:33:22 +01:00
|
|
|
impl ServiceFactory for AppEntry {
|
2019-05-12 17:34:51 +02:00
|
|
|
type Config = ();
|
2019-04-13 23:50:54 +02:00
|
|
|
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
|
|
|
|
2019-12-02 16:37:13 +01:00
|
|
|
fn new_service(&self, _: ()) -> Self::Future {
|
|
|
|
self.factory.borrow_mut().as_mut().unwrap().new_service(())
|
2019-03-09 18:49:11 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-22 20:18:33 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-11-26 06:25:50 +01:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::Arc;
|
2019-05-22 20:18:33 +02:00
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
use crate::test::{init_service, TestRequest};
|
|
|
|
use crate::{web, App, HttpResponse};
|
|
|
|
use actix_service::Service;
|
2019-05-22 20:18:33 +02:00
|
|
|
|
|
|
|
struct DropData(Arc<AtomicBool>);
|
|
|
|
|
|
|
|
impl Drop for DropData {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.0.store(true, Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 06:25:50 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_drop_data() {
|
2019-05-22 20:18:33 +02:00
|
|
|
let data = Arc::new(AtomicBool::new(false));
|
2019-11-26 06:25:50 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut app = init_service(
|
2019-05-22 20:18:33 +02:00
|
|
|
App::new()
|
|
|
|
.data(DropData(data.clone()))
|
|
|
|
.service(web::resource("/test").to(|| HttpResponse::Ok())),
|
2019-11-20 18:33:22 +01:00
|
|
|
)
|
|
|
|
.await;
|
2019-11-26 06:25:50 +01:00
|
|
|
let req = TestRequest::with_uri("/test").to_request();
|
2019-11-20 18:33:22 +01:00
|
|
|
let _ = app.call(req).await.unwrap();
|
2019-11-26 06:25:50 +01:00
|
|
|
}
|
2019-05-22 20:18:33 +02:00
|
|
|
assert!(data.load(Ordering::Relaxed));
|
|
|
|
}
|
|
|
|
}
|