Skip to content

Commit

Permalink
chore(examples): Add Cors to grpc web server (#1320)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco authored Mar 20, 2023
1 parent d471212 commit ddec9e3
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/src/grpc-web/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -38,7 +39,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
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)
Expand Down

0 comments on commit ddec9e3

Please sign in to comment.