1

I'm trying to create a little Grpc-Web Rust server using axum and tonic.

I'm new both to Rust and Grpc.

I managed to generate all the rust code from proto files.

THE REPOSITORY IS HERE: https://github.com/frederikhors/issue-tonic-web

The issue I'm having is when I try to create the grpc server inside another function.

What should I use here? https://github.com/frederikhors/issue-tonic-web/blob/main/src/routes/grpc.rs#L51

pub fn new_grpc_server() -> Router {
    // I need to expose these services as grpc-web (using tonic_web I think)

    // let server = tonic_web::enable(GreeterServer::new(GrpcServiceImpl::default()));

    let server = tonic::transport::Server::builder()
        .add_service(HelloGreeterServer::new(HelloServiceImpl::default()))
        .add_service(WorldGreeterServer::new(WorldServiceImpl::default()));

    server
}

Issues

  1. I don't know what to use in axum route, given the present error:

    error[E0308]: mismatched types
      --> src\main.rs:16:9
        |
    13  |     api = api.route(
        |               ----- arguments to this method are incorrect
    ...
    16  |         axum::routing::any_service(grpc_server.into_service()),
        |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `MethodRouter<_, _>`, found `MethodRouter<_, Body, Box<...>>`
        |
        = note: expected struct `MethodRouter<_, _, Infallible>`
                  found struct `MethodRouter<_, axum::body::Body, Box<(dyn std::error::Error + Send + Sync + 'static)>>`
        |
    122 |     pub fn route(mut self, path: &str, method_router: MethodRouter<S, B>) -> Self {
        |            ^^^^^
    

    for the code:

    async fn main() {
        let mut api = axum::Router::new().route("/api", get(|| async { "Hello, World!" }));
    
        let grpc_server = grpc::new_grpc_server();
    
        api = api.route(
            "/*rpc",
            // I don't know what to use here
            axum::routing::any_service(grpc_server.into_service()),
        );
    
        let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
    
        axum::Server::bind(&addr)
            .serve(api.into_make_service())
            .await
            .unwrap();
    }
    
  2. Is axum::routing::any_service necessary?

  3. I don't know how to use tonic_web to expose the grpc-web

I can also PR adding an example in axum repo for this as soon I understand how to do.

Jmb
  • 18,893
  • 2
  • 28
  • 55
Fred Hors
  • 3,258
  • 3
  • 25
  • 71

0 Answers0