1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-31 19:10:07 +01:00

99 lines
2.6 KiB
Rust
Raw Normal View History

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