Skip to content
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

[tonic-web] Fix CORS trait issue when using tonic_web::enable #1286

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions examples/src/grpc-web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
use tonic_web::GrpcWebLayer;

pub mod hello_world {
tonic::include_proto!("helloworld");
Expand Down Expand Up @@ -39,8 +38,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

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

Expand Down
1 change: 1 addition & 0 deletions tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ percent-encoding = "2.1"
pin-project = "1.0.11"
tower-layer = "0.3"
tower-service = "0.3"
tower-http = { version = "0.3", features = ["cors"] }

# prost
prost = {version = "0.11", default-features = false, features = ["std"], optional = true}
Expand Down
7 changes: 7 additions & 0 deletions tonic/src/service/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,13 @@ where
}
}

impl<S: crate::server::NamedService> crate::server::NamedService for tower_http::cors::Cors<S>
where
S: crate::server::NamedService,
{
const NAME: &'static str = S::NAME;
}

// required to use `InterceptedService` with `Router`
impl<S, F> crate::server::NamedService for InterceptedService<S, F>
where
Expand Down