Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

feat: add ability to configure cors in the config #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

namit-chandwani
Copy link
Member

@namit-chandwani namit-chandwani commented Apr 2, 2021

Fixes #58
Through this PR, I have tried to add the functionality that enables CORS to be configured through the graphql-link.yaml file.

Todo (as suggested in #59 (comment)):

  • runable functionality
  • docs
  • including cors in config generation (init CMD)
  • default values

type CorsConfig struct {
AllowedOrigins []string `yaml:"allowed-origins"`
AllowOriginFunc func(origin string) bool `yaml:"allow-origin-func"`
AllowOriginRequestFunc func(r *http.Request, origin string) bool `yaml:"allow-origin-request-func"`
Copy link
Contributor

@wtrocki wtrocki Apr 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

April 2021 - Software engineer Namit invented new revolutionary way to deserialize functions from json

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

April 2021 - Software engineer Namit invented new revolutionary way to deserialize functions from json

Lol, I saw this coming 😂

The thing is that this PR is still Work in Progress and there are a couple of more changes I wanted to make in this PR before requesting for a review.
Since it was bedtime for me back then, I left those changes for today (sorry, forgot to mention this in the PR description).

The first change that I had planned for today was to fix the function extraction part from yaml (which is what you have pointed out too) and the second was to change the default values of some of the fields, like currently the AllowedOrigins field takes a default value of [ ] instead of ["*"], which needs to be changed.

Will push a commit that fixes both of these in some time.

type Config struct {
Server *httptest.Server `yaml:"-"`
Gateway *gateway.Gateway `yaml:"-"`
Listen string `yaml:"listen"`
gateway.Config `yaml:"-,inline"`
CorsConfig `yaml:"cors"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can put this into some better structure - cors is usually part of http gateway config

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in commit: 801544f

c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
})
c := cors.New((cors.Options)(config.CorsConfig))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will crash when config is missing or not properly deserialized

@@ -191,9 +191,7 @@ func watchFile(filename string, onChange func(in fsnotify.Event)) (*fsnotify.Wat

func startServer(config *config.Config) error {
postProcess(config)
c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing defaults that were applied here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The New function of the rs/cors package handles all the default values for us.

So even if a field of config.CorsConfig has not been assigned any value in the yaml file, the New function will consider the default value for it (without the need of us explicitly assigning it) and then return the cors handler.

Please have a look at this for reference: https://github.com/rs/cors/blob/f9bce55a4e61e3d1a061993e3453eb9848fcdc4d/cors.go#L122-L127

MaxAge int `yaml:"max-age"`
AllowCredentials bool `yaml:"allow-credentials"`
OptionsPassthrough bool `yaml:"options-passthrough"`
Debug bool `yaml:"debug"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if all of those options are needed.
We should only expose ones we want

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved in commit: f8eb6d5

AllowOriginRequestFunc func(r *http.Request, origin string) bool `yaml:"allow-origin-request-func"`
AllowedMethods []string `yaml:"allowed-methods"`
AllowedHeaders []string `yaml:"allowed-headers"`
ExposedHeaders []string `yaml:"exposed-headers"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each option should be documented as part of the config file

@wtrocki
Copy link
Contributor

wtrocki commented Apr 2, 2021

Is this WIP? Looks like some untested experiment.
Please add verification procedure next time so it is clear how you have tested it.

We are missing couple things like:

  • runable functionality
  • docs
  • including cors in config generation (init CMD)
  • default values

@namit-chandwani
Copy link
Member Author

namit-chandwani commented Apr 2, 2021

Is this WIP? Looks like some untested experiment.

@wtrocki Yes, this is WIP (sorry for not mentioning this in the PR description).
I had planned to make some changes today before asking for a review.

Please read this comment of mine: #59 (comment)

Please give me some time, I'll make all the changes that I had planned and also look into the review comments that you have added.
Thanks!

@wtrocki
Copy link
Contributor

wtrocki commented Apr 2, 2021

No rush. Sorry for premature review

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

Successfully merging this pull request may close these issues.

Add ability to configure cors in the config
2 participants