The CORS middleware adds CORS headers to requests within a scope. The headers can configured or default allow all host settings can be used.
Configuration
The CORS headers can be configured using a CORSConfig structure.
type CORSConfig struct {
AllowOrigins []string
AllowMethods []string
AllowHeaders []string
AllowCredentials bool
ExposeHeaders []string
Age int
}
AllowOrigins - Allow origins can be a list of specific hosts to allow requests from or “” to allow requests from any hosts. (DEFAULT: []string{””})
AllowMethods - The specific methods that clients are allowed to request. This can be an empty array to allow all methods. (DEFAULT: []string{})
AllowHeaders - The headers the client is allowed to send. This can be an empty array to handle. (DEFAULT: []string{})
ExposeHeaders - The headers that the client is allowed to access. This can be set to an empty array to allow all. (DEFAULT: []string{})
Age - The maximum amount of time the client should cache the results before asking again. If set to “0” then no requests should be cached. (DEFAULT: 0)
Usage
Default options
svr := celerity.New()
svr.Pre(middleware.CORS())
Configured options
svr := celerity.New()
svr.Pre(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"myhost.com", "myotherhost.com"},
}))