2019-11-21 07:17:01 +01:00
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-04-11 00:06:27 +02:00
|
|
|
use actix_http::Error;
|
2019-11-21 07:17:01 +01:00
|
|
|
use actix_service::{Service, ServiceFactory};
|
|
|
|
use futures::future::{FutureExt, LocalBoxFuture};
|
2019-04-11 00:06:27 +02:00
|
|
|
|
|
|
|
pub(crate) type BoxedHttpService<Req> = Box<
|
2019-07-17 11:48:37 +02:00
|
|
|
dyn Service<
|
2019-04-11 00:06:27 +02:00
|
|
|
Request = Req,
|
|
|
|
Response = (),
|
|
|
|
Error = Error,
|
2019-11-21 07:17:01 +01:00
|
|
|
Future = LocalBoxFuture<'static, Result<(), Error>>,
|
2019-04-11 00:06:27 +02:00
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
|
|
|
pub(crate) type BoxedHttpNewService<Req> = Box<
|
2019-11-21 07:17:01 +01:00
|
|
|
dyn ServiceFactory<
|
2019-05-12 17:34:51 +02:00
|
|
|
Config = (),
|
2019-04-11 00:06:27 +02:00
|
|
|
Request = Req,
|
|
|
|
Response = (),
|
|
|
|
Error = Error,
|
|
|
|
InitError = (),
|
|
|
|
Service = BoxedHttpService<Req>,
|
2019-11-21 07:17:01 +01:00
|
|
|
Future = LocalBoxFuture<'static, Result<BoxedHttpService<Req>, ()>>,
|
2019-04-11 00:06:27 +02:00
|
|
|
>,
|
|
|
|
>;
|
|
|
|
|
2019-11-21 07:17:01 +01:00
|
|
|
pub(crate) struct HttpNewService<T: ServiceFactory>(T);
|
2019-04-11 00:06:27 +02:00
|
|
|
|
|
|
|
impl<T> HttpNewService<T>
|
|
|
|
where
|
2019-11-21 07:17:01 +01:00
|
|
|
T: ServiceFactory<Response = (), Error = Error>,
|
2019-04-11 00:06:27 +02:00
|
|
|
T::Response: 'static,
|
|
|
|
T::Future: 'static,
|
2019-11-21 07:17:01 +01:00
|
|
|
T::Service: Service<Future = LocalBoxFuture<'static, Result<(), Error>>> + 'static,
|
2019-04-11 00:06:27 +02:00
|
|
|
<T::Service as Service>::Future: 'static,
|
|
|
|
{
|
|
|
|
pub fn new(service: T) -> Self {
|
|
|
|
HttpNewService(service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 07:17:01 +01:00
|
|
|
impl<T> ServiceFactory for HttpNewService<T>
|
2019-04-11 00:06:27 +02:00
|
|
|
where
|
2019-11-21 07:17:01 +01:00
|
|
|
T: ServiceFactory<Config = (), Response = (), Error = Error>,
|
2019-04-11 00:06:27 +02:00
|
|
|
T::Request: 'static,
|
|
|
|
T::Future: 'static,
|
2019-11-21 07:17:01 +01:00
|
|
|
T::Service: Service<Future = LocalBoxFuture<'static, Result<(), Error>>> + 'static,
|
2019-04-11 00:06:27 +02:00
|
|
|
<T::Service as Service>::Future: 'static,
|
|
|
|
{
|
2019-05-12 17:34:51 +02:00
|
|
|
type Config = ();
|
2019-04-11 00:06:27 +02:00
|
|
|
type Request = T::Request;
|
|
|
|
type Response = ();
|
|
|
|
type Error = Error;
|
|
|
|
type InitError = ();
|
|
|
|
type Service = BoxedHttpService<T::Request>;
|
2019-11-21 07:17:01 +01:00
|
|
|
type Future = LocalBoxFuture<'static, Result<Self::Service, ()>>;
|
2019-04-11 00:06:27 +02:00
|
|
|
|
|
|
|
fn new_service(&self, _: &()) -> Self::Future {
|
2019-11-21 07:17:01 +01:00
|
|
|
let fut = self.0.new_service(&());
|
|
|
|
|
|
|
|
async move {
|
|
|
|
fut.await.map_err(|_| ()).map(|service| {
|
|
|
|
let service: BoxedHttpService<_> =
|
|
|
|
Box::new(HttpServiceWrapper { service });
|
|
|
|
service
|
|
|
|
})
|
|
|
|
}
|
|
|
|
.boxed_local()
|
2019-04-11 00:06:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct HttpServiceWrapper<T: Service> {
|
|
|
|
service: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Service for HttpServiceWrapper<T>
|
|
|
|
where
|
|
|
|
T: Service<
|
|
|
|
Response = (),
|
2019-11-21 07:17:01 +01:00
|
|
|
Future = LocalBoxFuture<'static, Result<(), Error>>,
|
2019-04-11 00:06:27 +02:00
|
|
|
Error = Error,
|
|
|
|
>,
|
|
|
|
T::Request: 'static,
|
|
|
|
{
|
|
|
|
type Request = T::Request;
|
|
|
|
type Response = ();
|
|
|
|
type Error = Error;
|
2019-11-21 07:17:01 +01:00
|
|
|
type Future = LocalBoxFuture<'static, Result<(), Error>>;
|
2019-04-11 00:06:27 +02:00
|
|
|
|
2019-11-21 07:17:01 +01:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.service.poll_ready(cx)
|
2019-04-11 00:06:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
self.service.call(req)
|
|
|
|
}
|
|
|
|
}
|