I've create a gRPC web server like this:
use tonic_web::GrpcWebLayer;
use tonic::{
transport::Server,
};
let grpc_web_handle = tokio::spawn(
Server::builder()
.accept_http1(true)
.layer(GrpcWebLayer::new())
.add_service(
tonic_web::enable(
AuthServer::new(
AuthImpl::default()
)
)
)
.serve("127.0.0.1:9001".parse()?)
)
//...
However, my firefox complains about CORS:
OPTIONS http://127.0.0.1:9001/auth.Auth/Login
:
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing)
Response:
HTTP/1.1 400 Bad Request
content-length: 0
date: Wed, 13 Dec 2023 22:25:53 GMT
I also tried to wrap the AuthServer
into cors(AuthServer::new(...))
fn cors<S>(service: S) -> CorsGrpcWeb<S> where
S: Service<http::Request<hyper::Body>, Response=http::Response<BoxBody>>,
S: Clone + Send + 'static,
S::Future: Send + 'static,
S::Error: Into<BoxError> + Send
{
CorsLayer::permissive().layer(tonic_web::enable(service)).into_inner()
}
Still not working.
tonic = {version = "0.10.2", features = []}
tonic-web = {version = "0.10.2",features = []}
tokio = { version = "1.34.0", features = ["full"] }
To enable CORS you can use below definition
As they said in this PR
Also found this example in their issues
Also check this comment too.