viz_core/handler/
service.rs

1use hyper::service::Service;
2
3use crate::{Body, BoxError, Bytes, Error, Handler, HttpBody, Request, Response, Result};
4
5/// Converts a hyper [`Service`] to a viz [`Handler`].
6#[derive(Debug, Clone)]
7pub struct ServiceHandler<S>(S);
8
9impl<S> ServiceHandler<S> {
10    /// Creates a new [`ServiceHandler`].
11    pub const fn new(s: S) -> Self {
12        Self(s)
13    }
14}
15
16#[crate::async_trait]
17impl<I, O, S> Handler<Request<I>> for ServiceHandler<S>
18where
19    I: HttpBody + Send + 'static,
20    O: HttpBody + Send + 'static,
21    O::Data: Into<Bytes>,
22    O::Error: Into<BoxError>,
23    S: Service<Request<I>, Response = Response<O>> + Send + Sync + 'static,
24    S::Future: Send,
25    S::Error: Into<BoxError>,
26{
27    type Output = Result<Response>;
28
29    async fn call(&self, req: Request<I>) -> Self::Output {
30        self.0
31            .call(req)
32            .await
33            .map_err(Error::boxed)
34            .map(|resp| resp.map(Body::wrap))
35    }
36}