use axum::body::boxed;
use axum::Router;
use hyper::{Body, Request, Response};
use sealed::sealed;
use std::convert::Infallible;
use tonic::body::BoxBody;
use tonic::transport::NamedService;
use tower::{Service, ServiceBuilder};
use tower_http::ServiceBuilderExt;
#[sealed]
pub trait RouterTonicExt {
fn from_tonic_service<S>(service: S) -> Self
where
Self: Sized,
S: Service<Request<Body>, Response = Response<BoxBody>, Error = Infallible>
+ NamedService
+ Clone
+ Send
+ 'static,
S::Future: Send + 'static;
}
#[sealed]
impl RouterTonicExt for Router {
fn from_tonic_service<S>(service: S) -> Self
where
Self: Sized,
S: Service<Request<Body>, Response = Response<BoxBody>, Error = Infallible>
+ NamedService
+ Clone
+ Send
+ 'static,
S::Future: Send + 'static,
{
let svc = ServiceBuilder::new()
.map_err(|err: Infallible| match err {})
.map_response_body(boxed)
.service(service);
Router::new().route_service(&format!("/{}/*rest", S::NAME), svc)
}
}