cors
Martini middleware/handler to enable CORS support.
Usage
import (
"github.com/codegangsta/martini"
"github.com/martini-contrib/cors"
)
func main() {
m := martini.Classic()
m.Use(cors.Allow(&cors.Options{
AllowOrigins: []string{"https://foo\\.*"},
AllowMethods: []string{"PUT", "PATCH"},
AllowHeaders: []string{"Origin"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}))
m.Run()
}
You may alternatively prefer to allow CORS only for certain routes. Instead of using the CORS middleware app-wide, register it for the prefered routes. The following snippet demonstrates how to enable CORS for /api/books
endpoint's PUT handler.
m := martini.Classic()
allowCORSHandler := cors.Allow(&cors.Options{
AllowOrigins: []string{"https://foo\\.*"},
AllowMethods: []string{"PUT", "PATCH"},
AllowHeaders: []string{"Origin"},
})
m.Put("/api/books", allowCORSHandler, func() string {
})
Authors