Skip to content

Make the tonic-web layer axum compatible #2157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

MarkusTieger
Copy link

@MarkusTieger MarkusTieger commented Jan 22, 2025

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 #1964

@Ludea
Copy link
Contributor

Ludea commented Mar 13, 2025

I try your PR, and tonic doesn't build

@MarkusTieger
Copy link
Author

MarkusTieger commented Mar 13, 2025

I try your PR, and tonic doesn't build

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

@Ludea
Copy link
Contributor

Ludea commented Mar 14, 2025

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`

@MarkusTieger
Copy link
Author

MarkusTieger commented Mar 14, 2025

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

@MarkusTieger
Copy link
Author

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.

@Ludea
Copy link
Contributor

Ludea commented Mar 14, 2025

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

@MarkusTieger
Copy link
Author

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.

@Ludea
Copy link
Contributor

Ludea commented Mar 14, 2025

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

@Ludea
Copy link
Contributor

Ludea commented Mar 24, 2025

nvm it work as expected !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Impossible to use tonic-web (0.12.3) while multiplexing with axum (0.7.7) without using the deprecated Router::into_router()?
2 participants