-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Conversation
I think the readme is outdated so I updated it here #1292 since this should be the new way to do it rather than the old way. |
Do we want to enable |
I believe we should be pointing people to use |
Wait until add extra to sea-query [PR](SeaQL/sea-query#611) Wait until fix cors to tonic_web [PR](hyperium/tonic#1286)
Hello! Is there anything I can do to speed up the process? |
I am trying to get this patch working on a cloned version but I am still getting error:
I am pretty sure that is applied... I am doing something wrong? It shows that And that service works fine without tonic_web::enable |
Check the readme https://github.com/hyperium/tonic/tree/master/tonic-web#enabling-tonic-services and the examples for how to configure it. |
So we shouldn't use tonic_web::enable at all? |
@LucioFranco mind adding an example with how configure tonic_web with CORS? My understanding is that this PR broke CORS support by not implementing the trait on Using the new builder syntax doesn't affect the underlying issue. This also fails with the trait issue, for example.
|
Thanks for the example @LucioFranco this works for me: #1320 and allows me to configure the CORS layer to my needs. This default should work well for anyone getting started. const DEFAULT_MAX_AGE: Duration = Duration::from_secs(24 * 60 * 60);
const DEFAULT_EXPOSED_HEADERS: [&str; 3] =
["grpc-status", "grpc-message", "grpc-status-details-bin"];
const DEFAULT_ALLOW_HEADERS: [&str; 4] =
["x-grpc-web", "content-type", "x-user-agent", "grpc-timeout"];
let cors_layer = CorsLayer::new()
.allow_origin(AllowOrigin::mirror_request())
.allow_credentials(true)
.max_age(DEFAULT_MAX_AGE)
.expose_headers(
DEFAULT_EXPOSED_HEADERS
.iter()
.cloned()
.map(HeaderName::from_static)
.collect::<Vec<HeaderName>>(),
)
.allow_headers(
DEFAULT_ALLOW_HEADERS
.iter()
.cloned()
.map(HeaderName::from_static)
.collect::<Vec<HeaderName>>(),
);
//...
// from example https://github.com/hyperium/tonic/pull/1320
Server::builder()
// GrpcWeb is over http1 so we must enable it.
.accept_http1(true)
.accept_http1(true)
// from above
.layer(cors_layer)
// Apply the tonic-web layer to convert
// http1 requests into something that
// the core tonic code can understand.
.layer(GrpcWebLayer::new())
.layer(GrpcWebLayer::new())
.add_service(greeter)
.add_service(greeter)
.serve(addr)
.serve(addr) |
I messed up and I thought we were removing |
Motivation
The grpc-web README example doesn't compile due to an unimplemented trait in
tower_http::cors::Cors
.It seems related to this PR, which added the tower_http Cors layer wrapper to
tonic_web::enable
.The error
Reproducing the error
You can repro the issue by cloning the tonic repo and modifying the code in this example.
Then run
cargo run -p examples --bin grpc-web-server
Solution
Added
NamedService
impl fortower_http::cors::Cors<S>
. Open to feedback on where exactly this should live.Also made the grpc-web server example consistent with the README. It now uses the
tonic_web::enable
helper which enables CORS support by default. As stated in this issue, CORS support is needed for a browser client to talk to a tonic server without a proxy. I imagine this is what the typical tonic-web user wants.After making this change, a grpc-web client running in my browser is able to talk to my tonic-web server without issue.