diff --git a/examples/src/grpc-web/server.rs b/examples/src/grpc-web/server.rs index 7204b07a2..0e8d51f32 100644 --- a/examples/src/grpc-web/server.rs +++ b/examples/src/grpc-web/server.rs @@ -3,6 +3,7 @@ use tonic::{transport::Server, Request, Response, Status}; use hello_world::greeter_server::{Greeter, GreeterServer}; use hello_world::{HelloReply, HelloRequest}; use tonic_web::GrpcWebLayer; +use tower_http::cors::CorsLayer; pub mod hello_world { tonic::include_proto!("helloworld"); @@ -38,7 +39,13 @@ async fn main() -> Result<(), Box> { println!("GreeterServer listening on {}", addr); Server::builder() + // GrpcWeb is over http1 so we must enable it. .accept_http1(true) + // Use the Cors layer from `tower-http`. + .layer(CorsLayer::new()) + // Apply the tonic-web layer to convert + // http1 requests into something that + // the core tonic code can understand. .layer(GrpcWebLayer::new()) .add_service(greeter) .serve(addr)