hyperium / tonic

A native gRPC client & server implementation with async/await support.
https://docs.rs/tonic
MIT License
10.02k stars 1.02k forks source link

tonic_web: NamedService bound on enable() required #1177

Closed softdevca closed 1 year ago

softdevca commented 1 year ago

Bug Report

It was proposed to remove the NamedService bound of tonic_web::enable() in issue #1115. This change was accepted but it breaks adding the enable'd service to a Tonic server.

Issue #1174 was briefly filed and closed with a work around.

Version

├── tonic v0.8.3
├── tonic-web v0.5.0
│   ├── tonic v0.8.3 (*)
└── tonic-build v0.8.4

Platform

MacOS 13.0.1 on M1 Pro, rust 1.65.0 Pop OS Linux on AMD, rust 1.65.0

Description

The standard example below will not compile because add_service requires the NamedService trait and tonic_web::enable no longer provides it.

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let addr = "[::1]:50051".parse().unwrap();
    let greeter = GreeterServer::new(MyGreeter::default());

    Server::builder()
       .accept_http1(true)
       .add_service(tonic_web::enable(greeter))
       .serve(addr)
       .await?;

   Ok(())
}
stepan-romankov commented 1 year ago

Same here.

LucioFranco commented 1 year ago

I believe https://github.com/hyperium/tonic/blob/master/examples/src/grpc-web/server.rs#L42 should work for you?

stepan-romankov commented 1 year ago

I believe https://github.com/hyperium/tonic/blob/master/examples/src/grpc-web/server.rs#L42 should work for you?

Thanks @LucioFranco, works like a charm! I assume documentation needs to be fixed...

softdevca commented 1 year ago

It worked for me as well, though I had to add the Cors layer manual now that I cannot use tonic_web::enable().

LucioFranco commented 1 year ago

Ah yup those docs need to be updated!

@softdevca yeah the cors stuff got removed iirc, you can check the PRs we made for tonic-web to see the background.

LucioFranco commented 1 year ago

Gonna close this since I think we got this all resolved.