Skip to content

Commit

Permalink
chore(router): Remove wrapper type for axum router (#1746)
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto authored Jun 23, 2024
1 parent 5d7bfc2 commit 6a213e9
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 32 deletions.
2 changes: 1 addition & 1 deletion tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ prost = ["dep:prost"]
tls = ["dep:rustls-pemfile", "dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"]
tls-roots = ["tls", "channel", "dep:rustls-native-certs"]
tls-webpki-roots = ["tls", "channel", "dep:webpki-roots"]
router = ["dep:axum"]
router = ["dep:axum", "dep:tower", "tower?/util"]
server = [
"router",
"dep:async-stream",
Expand Down
33 changes: 2 additions & 31 deletions tonic/src/service/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{
pin::Pin,
task::{ready, Context, Poll},
};
use tower_service::Service;
use tower::{Service, ServiceExt};

/// A [`Service`] router.
#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -77,7 +77,7 @@ impl Routes {
{
self.router = self.router.route_service(
&format!("/{}/*rest", S::NAME),
AxumBodyService { service: svc },
svc.map_request(|req: Request<axum::body::Body>| req.map(boxed)),
);
self
}
Expand Down Expand Up @@ -143,32 +143,3 @@ impl Future for RoutesFuture {
}
}
}

#[derive(Clone)]
struct AxumBodyService<S> {
service: S,
}

type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;

impl<S> Service<Request<axum::body::Body>> for AxumBodyService<S>
where
S: Service<Request<BoxBody>, Response = Response<BoxBody>, Error = Infallible>
+ Clone
+ Send
+ 'static,
S::Future: Send + 'static,
{
type Response = Response<axum::body::Body>;
type Error = Infallible;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}

fn call(&mut self, req: Request<axum::body::Body>) -> Self::Future {
let fut = self.service.call(req.map(boxed));
Box::pin(async move { fut.await.map(|res| res.map(axum::body::Body::new)) })
}
}

0 comments on commit 6a213e9

Please sign in to comment.