Skip to content
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

Impossible to do anything about cors. #1524

Closed
ta3pks opened this issue Sep 17, 2023 · 4 comments
Closed

Impossible to do anything about cors. #1524

ta3pks opened this issue Sep 17, 2023 · 4 comments

Comments

@ta3pks
Copy link

ta3pks commented Sep 17, 2023

I came across a couple of discussions and a PR concerning cors however none of the explanations in the docs about cors make any sense and there isnt a single example showing how to use a cors layer.
Docs state one can customize cors config but for some reason the author has no explanation for how to do that and the example following that statement has nothing to do with cors. Unfortunately, contrary to the belief of the author most of us dont have physic powers.
image

There's a CorsGrpcWeb struct in the project however that as well has no explanation or a constructor.

Seems adding a third party library -tower- should fix this issue but adding tower cors layer also didnt seem to fix the cors issue. It is obvious i did something wrong there but since the documentation just expects us to know how to do this out of the womb.

Overall I just thought using grpc can improve development speed since the client and server stubs are just auto generated so no need to do double job by writing a rest endpoint for everything and then writing a request for that endpoint on client-side I was sooo wrong. Just wasted hours trying to get cors running for a project that claims to be a grpc framework with web support which seems to be lacking most basic things to get started.

@seddonm1
Copy link

Hi,
Maybe this would help. This is effectively what .enable does. Get the constants from the top of that file.

  Server::builder()
       .accept_http1(true)
        .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>>(),
                ),
        )
       .layer(GrpcWebLayer::new())
       .add_service(greeter)
       .serve(addr)
       .await?;

@ta3pks
Copy link
Author

ta3pks commented Sep 25, 2023

Hi, Maybe this would help. This is effectively what .enable does. Get the constants from the top of that file.

  Server::builder()
       .accept_http1(true)
        .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>>(),
                ),
        )
       .layer(GrpcWebLayer::new())
       .add_service(greeter)
       .serve(addr)
       .await?;

Thank you I really appreciate the help. Until grpc echosystem little bit matures tho i think i will stick with rest for now

@ta3pks ta3pks closed this as completed Sep 25, 2023
@Larkooo
Copy link

Larkooo commented Feb 4, 2024

Hi, Maybe this would help. This is effectively what .enable does. Get the constants from the top of that file.

  Server::builder()
       .accept_http1(true)
        .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>>(),
                ),
        )
       .layer(GrpcWebLayer::new())
       .add_service(greeter)
       .serve(addr)
       .await?;

Setting the cors layer doesn't seem to work. Gives a trait bound error the on .serve()...

@boan-anbo
Copy link

Hi, Maybe this would help. This is effectively what .enable does. Get the constants from the top of that file.

  Server::builder()
       .accept_http1(true)
        .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>>(),
                ),
        )
       .layer(GrpcWebLayer::new())
       .add_service(greeter)
       .serve(addr)
       .await?;

Setting the cors layer doesn't seem to work. Gives a trait bound error the on .serve()...

I have the same issue: see also #1636

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants