Using cors.Allow procedurally instead of in the middleware stack
ian-lewis-cs opened this issue · 4 comments
The examples provided demonstrate using cors.Allow() in the Martini middleware stack.
In my case I am working on a router service which has a single handler as middleware and passes off the various actions in a procedural manner.
Is it valid to do the following inside my handler:
doCors := cors.Allow(&cors.Options{
AllowOrigins: permittedDomains,
})
doCors(response, request)
I've left out any additional logic.
Many thanks,
Ian
If you want to re-write the Martini router....
The second example in the readme demonstrates how to inject CORS before your Handler is called to respond to pre-flight requests for options.
I would just inject it before your handler like so:
allowCORSHandler := cors.Allow(&cors.Options{
AllowOrigins: []string{"https://*.foo.com"},
AllowMethods: []string{"PUT", "PATCH"},
AllowHeaders: []string{"Origin"},
})
m.Put("/api/books", allowCORSHandler, func() string {
// ...
})
You know, CORS is not that complex, you could write your own CORS implementation.
Like @vastbinderj said, you can add the cors handler to your handler chain, if you want it to set it globally for all routes.
Closing this issue, if this suggestion doesn't solve your problem, please reopen it.
Thanks for the help!