Make the tonic-web layer axum compatible
Motivation
So tonic-web can be used with axum-multiplexing. For example: (GenericAuthenticationServiceServer and GenericAuthenticationService are from my own project, don't get confused)
let mut routes = Routes::builder();
routes.add_service(GenericAuthenticationServiceServer::new(
crate::services::auth::GenericAuthenticationService {
backend: self.backend.clone(),
},
));
let router: axum::Router = routes
.routes()
.into_axum_router()
.layer(GrpcWebLayer::new());
// This router can now be used in combination with normal http routes.
Solution
With this pr you can just .layer(GrpcWebLayer::new()) on a normal axum router (as long as the axum feature is enabled)
Fixes https://github.com/hyperium/tonic/issues/1964
I try your PR, and tonic doesn't build
I try your PR, and tonic doesn't build
It does for me. Can you please share your compile error?
Finally tonic build if I only update tonic-web crate to your fork. But get
the trait bound `AuthMiddleware<axum::routing::route::Route>: tower::Service<http::Request<AxumBody>>` is not satisfied
--> server/src/rpc.rs:661:16
|
661 | .layer(layer)
| ----- ^^^^^ unsatisfied trait bound
| |
| required by a bound introduced by this call
|
= help: the trait `tower::Service<http::Request<AxumBody>>` is not implemented for `AuthMiddleware<Route>`
= help: the trait `tower::Service<http::Request<http_body_util::combinators::UnsyncBoxBody<axum::body::Bytes, Status>>>` is implemented for `AuthMiddleware<S>`
note: the method call chain might not have had the expected associated types
--> server/src/rpc.rs:651:46
|
651 | ... = tower::ServiceBuilder::new().layer(AuthMiddlewareLayer::default()).into_inner...
| ---------------------------- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ------------ `Layer::Service` remains `AuthMiddleware<_>` here
| | |
| | `Layer::Service` is `AuthMiddleware<_>` here
| this expression has type `ServiceBuilder<Identity>`
note: required by a bound in `AxumRouter::<S>::layer`
--> /data/data/com.termux/files/home/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.7.9/src/routing/mod.rs:284:21
|
281 | ...fn layer<L>(self, layer: L) -> ...
| ----- required by a bound in this associated function
...
284 | ...L::Service: Service<Request> + ...
| ^^^^^^^^^^^^^^^^ required by this bound in `AxumRouter::<S>::layer`
Can you share the code or provide a sample, in which this fork doesn't work?
https://github.com/Ludea/speedupdate-rs/blob/master/server/src/rpc.rs#L657 log: https://github.com/Ludea/speedupdate-rs/actions/runs/13857019225/job/38776006308#step:4:2125
https://github.com/Ludea/speedupdate-rs/blob/master/server/src/rpc.rs#L657 log: https://github.com/Ludea/speedupdate-rs/actions/runs/13857019225/job/38776006308#step:4:2125
Line 662: There is no variable named "layer". Do you mean "cors_layer"? Also you should enable the "axum" feature in tonic-web.
This crate only modifies parts of the "tonic-web" crate. Try to have working axum-multiplexing (with the normal tonic crate) in the first place without grpc-web and after that try to use it.
There is no axum feature: https://github.com/MarkusTieger/tonic/blob/master/tonic-web/Cargo.toml. Even there are no features (same on grpc-web crate)
btw, I update with your comment
the trait `tower::Service<http::Request<AxumBody>>` is not implemented for `AuthMiddleware<axum::routing::route::Route>
logs: https://github.com/Ludea/speedupdate-rs/actions/runs/13859610820/job/38784557650#step:4:698
There is no axum feature: https://github.com/MarkusTieger/tonic/blob/master/tonic-web/Cargo.toml. Even there are no features (same on grpc-web crate)
btw, I update with your comment
the trait `tower::Service<http::Request<AxumBody>>` is not implemented for `AuthMiddleware<axum::routing::route::Route>logs: https://github.com/Ludea/speedupdate-rs/actions/runs/13859610820/job/38784557650#step:4:698
The dependency "axum" is marked as optional in tonic-web, which automatically defines a feature. Just add "axum" to features in tonic-web.
Already done : https://github.com/Ludea/speedupdate-rs/blob/master/server/Cargo.toml#L22, but doesn't fix issue
nvm it work as expected !
@MarkusTieger can you fix the conflict and lets get the tests passing then we can merge this.
Seems fixed by https://github.com/hyperium/tonic/commit/fee9c130c86a365b039f3e70a72a94ea28a9aaa9