beego: github.com/astaxie/beego/plugins/cors Index | Files

package cors

import "github.com/astaxie/beego/plugins/cors"

Package cors provides handlers to enable CORS support. Usage

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/plugins/cors"

)

func main() {
	// CORS for https://foo.* origins, allowing:
	// - PUT and PATCH methods
	// - Origin header
	// - Credentials share
	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
		AllowOrigins:     []string{"https://*.foo.com"},
		AllowMethods:     []string{"PUT", "PATCH"},
		AllowHeaders:     []string{"Origin"},
		ExposeHeaders:    []string{"Content-Length"},
		AllowCredentials: true,
	}))
	beego.Run()
}

Index

Package Files

cors.go

func Allow Uses

func Allow(opts *Options) beego.FilterFunc

Allow enables CORS for requests those match the provided options.

type Options Uses

type Options struct {
    // If set, all origins are allowed.
    AllowAllOrigins bool
    // A list of allowed origins. Wild cards and FQDNs are supported.
    AllowOrigins []string
    // If set, allows to share auth credentials such as cookies.
    AllowCredentials bool
    // A list of allowed HTTP methods.
    AllowMethods []string
    // A list of allowed HTTP headers.
    AllowHeaders []string
    // A list of exposed HTTP headers.
    ExposeHeaders []string
    // Max age of the CORS headers.
    MaxAge time.Duration
}

Options represents Access Control options.

func (*Options) Header Uses

func (o *Options) Header(origin string) (headers map[string]string)

Header converts options into CORS headers.

func (*Options) IsOriginAllowed Uses

func (o *Options) IsOriginAllowed(origin string) (allowed bool)

IsOriginAllowed looks up if the origin matches one of the patterns generated from Options.AllowOrigins patterns.

func (*Options) PreflightHeader Uses

func (o *Options) PreflightHeader(origin, rMethod, rHeaders string) (headers map[string]string)

PreflightHeader converts options into CORS headers for a preflight response.

Package cors imports 7 packages (graph) and is imported by 28 packages. Updated 2019-06-07. Refresh now. Tools for package owners.