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

[go-server] Add featureCORS option #4400

Merged
merged 1 commit into from Nov 8, 2019

Conversation

@Jesse0Michael
Copy link
Contributor

Jesse0Michael commented Nov 7, 2019

Add a Go server configuration option of "featureCORS" that defaults to false. When set to true the Go server routers will be generated with Cross-Origin Resource Sharing middleware through gorilla mux.

https://www.gorillatoolkit.org/pkg/handlers#CORS

Accepting cross origin requests is something that I needed in my project. I accomplished it with a custom template, but just in case anyone else would like this feature I put up this PR.

@antihax (2017/11) @bvwells (2017/12) @grokify (2018/07) @kemokemo (2018/09) @bkabrda (2019/07)

PR checklist

  • Read the contribution guidelines.
  • If contributing template-only or documentation-only changes which will change sample output, build the project before.
  • Run the shell script(s) under ./bin/ (or Windows batch scripts under.\bin\windows) to update Petstore samples related to your fix. This is important, as CI jobs will verify all generator outputs of your HEAD commit, and these must match the expectations made by your contribution. You only need to run ./bin/{LANG}-petstore.sh, ./bin/openapi3/{LANG}-petstore.sh if updating the code or mustache templates for a language ({LANG}) (e.g. php, ruby, python, etc).
  • File the PR against the correct branch: master, 4.3.x, 5.0.x. Default: master.
  • Copy the technical committee to review the pull request if your PR is targeting a particular programming language.
@auto-labeler auto-labeler bot added the Server: Go label Nov 7, 2019
@auto-labeler

This comment has been minimized.

Copy link

auto-labeler bot commented Nov 7, 2019

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

Add a Go server configuration option of "featureCORS" that defaults to false. When set to true the Go server routers will be generated with Cross-Origin Resource Sharing middleware through gorrilla mux.

https://www.gorillatoolkit.org/pkg/handlers#CORS
@Jesse0Michael Jesse0Michael force-pushed the Jesse0Michael:go-server-cors-feature branch from 9e417a5 to bab753c Nov 7, 2019

"github.com/gorilla/handlers"{{/featureCORS}}

This comment has been minimized.

Copy link
@wing328

wing328 Nov 8, 2019

Member

What about removing line 11 and use the following instead?

{{#featureCORS}}
	"github.com/gorilla/handlers"
{{/featureCORS}}

(which will avoid blank lines)

@@ -34,7 +36,8 @@ func NewRouter(routers ...Router) *mux.Router {
for _, route := range api.Routes() {
var handler http.Handler
handler = route.HandlerFunc
handler = Logger(handler, route.Name)
handler = Logger(handler, route.Name){{#featureCORS}}
handler = handlers.CORS()(handler){{/featureCORS}}

This comment has been minimized.

Copy link
@wing328

wing328 Nov 8, 2019

Member

Same suggestion as above:

			{{#featureCORS}}
			handler = handlers.CORS()(handler)
			{{/featureCORS}}
@wing328

This comment has been minimized.

Copy link
Member

wing328 commented Nov 8, 2019

No worry. I'll take care of the suggested fix in another PR.

@wing328 wing328 merged commit 5b44418 into OpenAPITools:master Nov 8, 2019
5 checks passed
5 checks passed
Shippable Run 11890 status is SUCCESS.
Details
ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/drone/pr Build is passing
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@wing328 wing328 added this to the 4.2.1 milestone Nov 8, 2019
@Jesse0Michael Jesse0Michael deleted the Jesse0Michael:go-server-cors-feature branch Nov 8, 2019
jimschubert added a commit to jimschubert/openapi-generator that referenced this pull request Nov 10, 2019
* master: (28 commits)
  [meta] Support Kotlin meta generator (OpenAPITools#4156)
  [Go][Server] minor enhancement to the template (OpenAPITools#4417)
  Replace the old ResourceSupport (OpenAPITools#4426)
  [Core, Rust Server, ASP.NET Core] Fix Codegen Operation Scope Consistency (OpenAPITools#3495)
  Add Go Server featureCORS option (OpenAPITools#4400)
  Fix treatment of nullable types in a few more places (OpenAPITools#4315)
  prefix local variable with localVar (OpenAPITools#4402)
  [kotlin][client] gson complete integration (OpenAPITools#4332)
  [kotlin] [bugfix] [maven-plugin]: prevent ClassCastException with boolean config options (OpenAPITools#4361)
  add sbt, bazel to integration (OpenAPITools#4416)
  Add a blog post tutorial about generating Java clients using OpenAPI v3 (OpenAPITools#4405)
  add freshcells to company list (OpenAPITools#4414)
  Update isSet when the object is received from callback. (OpenAPITools#4385)
  Ruby client nullable (OpenAPITools#4391)
  Fixes Kotlin client property names that include a dollar sign for template override (OpenAPITools#4351)
  [Python] [Performance] Avoid unnessacary checks inside the loop (OpenAPITools#4305)
  Add QEDIT as a company that's using OpenAPI Generator (OpenAPITools#4392)
  update cpp flag for pistache (OpenAPITools#4386)
  Feature optional emit default values (OpenAPITools#4347)
  skip the test as async call may have finished (OpenAPITools#4377)
  ...
@wing328

This comment has been minimized.

Copy link
Member

wing328 commented Nov 15, 2019

@Jesse0Michael thanks for the PR, which has been included in the v4.2.1 release: https://twitter.com/oas_generator/status/1195339336922759168

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.