tonic icon indicating copy to clipboard operation
tonic copied to clipboard

Make the tonic-web layer axum compatible

Open 0xIO32 opened this issue 11 months ago • 10 comments

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

0xIO32 avatar Jan 22 '25 17:01 0xIO32

I try your PR, and tonic doesn't build

Ludea avatar Mar 13 '25 07:03 Ludea

I try your PR, and tonic doesn't build

It does for me. Can you please share your compile error?

0xIO32 avatar Mar 13 '25 16:03 0xIO32

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`

Ludea avatar Mar 14 '25 06:03 Ludea

Can you share the code or provide a sample, in which this fork doesn't work?

0xIO32 avatar Mar 14 '25 12:03 0xIO32

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

Ludea avatar Mar 14 '25 12:03 Ludea

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.

0xIO32 avatar Mar 14 '25 14:03 0xIO32

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

Ludea avatar Mar 14 '25 15:03 Ludea

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.

0xIO32 avatar Mar 14 '25 15:03 0xIO32

Already done : https://github.com/Ludea/speedupdate-rs/blob/master/server/Cargo.toml#L22, but doesn't fix issue

Ludea avatar Mar 14 '25 15:03 Ludea

nvm it work as expected !

Ludea avatar Mar 24 '25 11:03 Ludea

@MarkusTieger can you fix the conflict and lets get the tests passing then we can merge this.

LucioFranco avatar Jul 02 '25 13:07 LucioFranco

Seems fixed by https://github.com/hyperium/tonic/commit/fee9c130c86a365b039f3e70a72a94ea28a9aaa9

0xIO32 avatar Jul 02 '25 16:07 0xIO32