Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

cors(feature): adds cors support via textile config #387

Merged
merged 3 commits into from Dec 5, 2018

Conversation

carsonfarmer
Copy link
Member

Take a look at this one @sanderpick. Should be pretty straight forward. Should be reasonable defaults. Basically just adds relevant CORS headers as Gin middle-wear. By default, leaves Allowed-Origin as length-one slice with API address, and Methods and Headers response Headers contain what you'd expect. Easy to change defaults, tried to keep these out of the default config setup though.


control, ok := headers["Access-Control-Allow-Origin"]
if ok && len(control) > 0 {
cconfig.AllowOrigins = control
Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

should just be able to set AllowOrigins to[]string{"*"} but released version doesn't support that it seems.

core/api.go Outdated
if ok && len(control) > 0 {
cconfig.AllowMethods = control
} else {
cconfig.AllowMethods = []string{"GET", "POST", "DELETE", "OPTIONS"}
Copy link
Member Author

Choose a reason for hiding this comment

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

Should be good set of defaults?

core/api.go Outdated
cconfig.AllowHeaders = control
} else {
// default already has "Origin", "Content-Length", "Content-Type"
textileHeaders := []string{"Method", "X-Textile-Args", "X-Textile-Opts", "X-Requested-With", "Access-Control-Allow-Headers"}
Copy link
Member Author

Choose a reason for hiding this comment

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

Seems like a reasonable set? I think this is all we ever need to deal with?

} else {
// By default use API origin host
defaultHost := config.Addresses.API
match, _ := regexp.MatchString("^https?://", defaultHost)
Copy link
Member Author

Choose a reason for hiding this comment

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

This feels messy to me, but the cors package requires http{s} prefixes on origins... ipfs for example, hard-codes:
localhosts: https://github.com/ipfs/go-ipfs/blob/af73c5097ce0ff70a18db25f801b34fa66a98202/core/corehttp/commands.go#L40

Copy link
Member Author

Choose a reason for hiding this comment

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

leaving this in, seems to work, and is simpler than requiring prefixing etc...

@sanderpick
Copy link
Member

Nice! Just took a quick look... I was imagining adding something to the textile config as defaults? Just so that people can see that as a tweakable setting when looking at the config... IPFS has these defaults for the gateway:

"Gateway": {
    "APICommands": [],
    "HTTPHeaders": {
      "Access-Control-Allow-Headers": [
        "X-Requested-With",
        "Range"
      ],
      "Access-Control-Allow-Methods": [
        "GET"
      ],
      "Access-Control-Allow-Origin": [
        "*"
      ]
    },
    "PathPrefixes": [],
    "RootRedirect": "",
    "Writable": false
  },

@carsonfarmer
Copy link
Member Author

Oh yeh for sure can add defaults to config... was thinking we'd want to keep these out, but that actually makes things simpler, so sure 👍. Also, I haven't touched the Gateway stuff yet, but can also add that to this branch.

@sanderpick
Copy link
Member

Ok nice. I'd say skip the gateway. I was just using that as an example snippet from the config... IPFS does't put those as defaults for the API... dunno why.

@carsonfarmer
Copy link
Member Author

probs because you don't want anything other than localhost to access your API 99% of the time?


control, ok := headers["Access-Control-Allow-Origin"]
if ok && len(control) > 0 {
cconfig.AllowOrigins = control
Copy link
Member Author

Choose a reason for hiding this comment

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

should just be able to set AllowOrigins to[]string{"*"} but released version doesn't support that it seems.

} else {
// By default use API origin host
defaultHost := config.Addresses.API
match, _ := regexp.MatchString("^https?://", defaultHost)
Copy link
Member Author

Choose a reason for hiding this comment

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

leaving this in, seems to work, and is simpler than requiring prefixing etc...

repo/config/init.go Show resolved Hide resolved
"Access-Control-Allow-Headers": []string{
"Content-Type", "Method", "X-Textile-Args", "X-Textile-Opts", "X-Requested-With",
},
"Access-Control-Allow-Origin": []string{},
Copy link
Member Author

Choose a reason for hiding this comment

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

default is empty, which in turn means "http://127.0.0.1:40600" using current defaults...

core/api.go Show resolved Hide resolved
Copy link
Member

@sanderpick sanderpick left a comment

Choose a reason for hiding this comment

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

Awesome

repo/config/init.go Show resolved Hide resolved
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.

None yet

2 participants