cors

The cors middleware configures Cross-Origin Resource Sharingcors - 图1open in new window for Flame instances.

You can read source code of this middleware on GitHubcors - 图2open in new window and API documentation on pkg.go.devcors - 图3open in new window.

Installation

The minimum requirement of Go is 1.16.

  1. go get github.com/flamego/cors

Usage examples

The cors.CORScors - 图4open in new window works out-of-the-box with an optional cors.Optionscors - 图5open in new window:

  1. package main
  2. import (
  3. "github.com/flamego/cors"
  4. "github.com/flamego/flamego"
  5. )
  6. func main() {
  7. f := flamego.Classic()
  8. f.Get("/",
  9. cors.CORS(),
  10. func(c flamego.Context) string {
  11. return "This endpoint can be shared cross-origin"
  12. },
  13. )
  14. f.Run()
  15. }

The cors.Optionscors - 图6open in new window can be used to further customize the behavior of the middleware:

  1. package main
  2. import (
  3. "github.com/flamego/cors"
  4. "github.com/flamego/flamego"
  5. )
  6. func main() {
  7. f := flamego.Classic()
  8. f.Get("/",
  9. cors.CORS(
  10. cors.Options{
  11. AllowDomain: []string{"cors.example.com"},
  12. },
  13. ),
  14. func(c flamego.Context) string {
  15. return "This endpoint can be shared cross-origin"
  16. },
  17. )
  18. f.Run()
  19. }