Documentation ¶
Overview ¶
Example ¶
package main import ( "net/http" "github.com/go-http-utils/cors" ) func main() { mux := http.NewServeMux() mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { res.Write([]byte("Hello World")) }) http.ListenAndServe(":8080", cors.Handler(mux)) }
Output:
Index ¶
- Constants
- func Handler(h http.Handler, opts ...Option) http.Handler
- type Option
- func SetAllowHeaders(allows []string) Option
- func SetAllowOrigin(allow bool) Option
- func SetAllowOriginValidator(validator func(*http.Request) string) Option
- func SetCredentials(credentials bool) Option
- func SetExposeHeaders(exposes []string) Option
- func SetMaxAge(maxAge int) Option
- func SetMethods(methods []string) Option
Examples ¶
Constants ¶
const Version = "1.1.0"
Version is this package's version
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Option ¶
type Option func(*options) error
Option returns a configuration func to configurate the CORS middleware.
func SetAllowHeaders ¶
SetAllowHeaders configures the Access-Control-Allow-Headers CORS header.
func SetAllowOrigin ¶
SetAllowOrigin configures the Access-Control-Allow-Origin CORS header. Set to true to reflect the request origin. Set to false to disable CORS.
func SetAllowOriginValidator ¶
SetAllowOriginValidator configures the Access-Control-Allow-Origin CORS header by run the validator function `func(*http.Request) string`. The validator function accpets an `*http.Request` argument and return the Access-Control-Allow-Origin value.
func SetCredentials ¶
SetCredentials configures the Access-Control-Allow-Credentials CORS header.
func SetExposeHeaders ¶
SetExposeHeaders configures the Access-Control-Expose-Headers CORS header.
func SetMaxAge ¶
SetMaxAge configures the Access-Control-Max-Age CORS header (in seconds). If the maxAge > 600 (10 minutes), then it will be reset to 600 (10 minutes).
func SetMethods ¶
SetMethods configures the Access-Control-Allow-Methods CORS header.