cors
The cors middleware configures Cross-Origin Resource Sharingopen in new window for Flame instances.
You can read source code of this middleware on GitHubopen in new window and API documentation on pkg.go.devopen in new window.
Installation
The minimum requirement of Go is 1.16.
go get github.com/flamego/cors
Usage examples
The cors.CORS
open in new window works out-of-the-box with an optional cors.Options
open in new window:
package main
import (
"github.com/flamego/cors"
"github.com/flamego/flamego"
)
func main() {
f := flamego.Classic()
f.Get("/",
cors.CORS(),
func(c flamego.Context) string {
return "This endpoint can be shared cross-origin"
},
)
f.Run()
}
The cors.Options
open in new window can be used to further customize the behavior of the middleware:
package main
import (
"github.com/flamego/cors"
"github.com/flamego/flamego"
)
func main() {
f := flamego.Classic()
f.Get("/",
cors.CORS(
cors.Options{
AllowDomain: []string{"cors.example.com"},
},
),
func(c flamego.Context) string {
return "This endpoint can be shared cross-origin"
},
)
f.Run()
}