mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2025-08-06 16:02:17 +00:00
139 lines
3.6 KiB
Rust
139 lines
3.6 KiB
Rust
![]() |
use crate::error::Error;
|
||
|
use crate::payload::Payload;
|
||
|
use crate::request::FlowyRequest;
|
||
|
use crate::response::{FlowyResponse, Responder};
|
||
|
use crate::service::{Service, ServiceFactory, ServiceRequest, ServiceResponse};
|
||
|
use crate::util::ready::*;
|
||
|
use futures_core::ready;
|
||
|
use paste::paste;
|
||
|
use pin_project::pin_project;
|
||
|
use std::future::Future;
|
||
|
use std::marker::PhantomData;
|
||
|
use std::pin::Pin;
|
||
|
use std::task::{Context, Poll};
|
||
|
|
||
|
pub struct BoxServiceFactory<Cfg, Req, Res, Err, InitErr>(Inner<Cfg, Req, Res, Err, InitErr>);
|
||
|
impl<C, Req, Res, Err, InitErr> ServiceFactory<Req> for BoxServiceFactory<C, Req, Res, Err, InitErr>
|
||
|
where
|
||
|
Req: 'static,
|
||
|
Res: 'static,
|
||
|
Err: 'static,
|
||
|
InitErr: 'static,
|
||
|
{
|
||
|
type Response = Res;
|
||
|
type Error = Err;
|
||
|
type Service = BoxService<Req, Res, Err>;
|
||
|
type InitError = InitErr;
|
||
|
type Config = C;
|
||
|
|
||
|
type Future = BoxFuture<Result<Self::Service, InitErr>>;
|
||
|
|
||
|
fn new_service(&self, cfg: C) -> Self::Future {
|
||
|
self.0.new_service(cfg)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T>>>;
|
||
|
pub type BoxService<Req, Res, Err> =
|
||
|
Box<dyn Service<Req, Response = Res, Error = Err, Future = BoxFuture<Result<Res, Err>>>>;
|
||
|
|
||
|
pub fn service<S, Req>(service: S) -> BoxService<Req, S::Response, S::Error>
|
||
|
where
|
||
|
S: Service<Req> + 'static,
|
||
|
Req: 'static,
|
||
|
S::Future: 'static,
|
||
|
{
|
||
|
Box::new(ServiceWrapper::new(service))
|
||
|
}
|
||
|
|
||
|
impl<S, Req> Service<Req> for Box<S>
|
||
|
where
|
||
|
S: Service<Req> + ?Sized,
|
||
|
{
|
||
|
type Response = S::Response;
|
||
|
type Error = S::Error;
|
||
|
type Future = S::Future;
|
||
|
|
||
|
fn call(&self, request: Req) -> S::Future {
|
||
|
(**self).call(request)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
struct ServiceWrapper<S> {
|
||
|
inner: S,
|
||
|
}
|
||
|
|
||
|
impl<S> ServiceWrapper<S> {
|
||
|
fn new(inner: S) -> Self {
|
||
|
Self { inner }
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl<S, Req, Res, Err> Service<Req> for ServiceWrapper<S>
|
||
|
where
|
||
|
S: Service<Req, Response = Res, Error = Err>,
|
||
|
S::Future: 'static,
|
||
|
{
|
||
|
type Response = Res;
|
||
|
type Error = Err;
|
||
|
type Future = BoxFuture<Result<Res, Err>>;
|
||
|
|
||
|
fn call(&self, req: Req) -> Self::Future {
|
||
|
Box::pin(self.inner.call(req))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
struct FactoryWrapper<SF>(SF);
|
||
|
|
||
|
impl<SF, Req, Cfg, Res, Err, InitErr> ServiceFactory<Req> for FactoryWrapper<SF>
|
||
|
where
|
||
|
Req: 'static,
|
||
|
Res: 'static,
|
||
|
Err: 'static,
|
||
|
InitErr: 'static,
|
||
|
SF: ServiceFactory<Req, Config = Cfg, Response = Res, Error = Err, InitError = InitErr>,
|
||
|
SF::Future: 'static,
|
||
|
SF::Service: 'static,
|
||
|
<SF::Service as Service<Req>>::Future: 'static,
|
||
|
{
|
||
|
type Response = Res;
|
||
|
type Error = Err;
|
||
|
// type Service: Service<Req, Response = Self::Response, Error = Self::Error>;
|
||
|
type Service = BoxService<Req, Res, Err>;
|
||
|
type InitError = InitErr;
|
||
|
type Config = Cfg;
|
||
|
type Future = BoxFuture<Result<Self::Service, Self::InitError>>;
|
||
|
|
||
|
fn new_service(&self, cfg: Cfg) -> Self::Future {
|
||
|
let f = self.0.new_service(cfg);
|
||
|
Box::pin(async { f.await.map(|s| Box::new(ServiceWrapper::new(s)) as _) })
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn factory<SF, Req>(
|
||
|
factory: SF,
|
||
|
) -> BoxServiceFactory<SF::Config, Req, SF::Response, SF::Error, SF::InitError>
|
||
|
where
|
||
|
SF: ServiceFactory<Req> + 'static,
|
||
|
Req: 'static,
|
||
|
SF::Response: 'static,
|
||
|
SF::Service: 'static,
|
||
|
SF::Future: 'static,
|
||
|
SF::Error: 'static,
|
||
|
SF::InitError: 'static,
|
||
|
{
|
||
|
BoxServiceFactory(Box::new(FactoryWrapper(factory)))
|
||
|
}
|
||
|
|
||
|
type Inner<C, Req, Res, Err, InitErr> = Box<
|
||
|
dyn ServiceFactory<
|
||
|
Req,
|
||
|
Config = C,
|
||
|
Response = Res,
|
||
|
Error = Err,
|
||
|
InitError = InitErr,
|
||
|
Service = BoxService<Req, Res, Err>,
|
||
|
Future = BoxFuture<Result<BoxService<Req, Res, Err>, InitErr>>,
|
||
|
>,
|
||
|
>;
|