chore(examples): Add Cors to grpc web server (#1320)

This commit is contained in:
Lucio Franco
2023-03-20 09:31:15 -04:00
committed by GitHub
parent d471212ee8
commit ddec9e34f2
+7
View File
@@ -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<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)