cors

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 24, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cors implements functions to enable New support for the mock server.

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	AllowedOrigin: "*",
	AllowedMethods: strings.Join([]string{
		http.MethodGet,
		http.MethodPost,
		http.MethodHead,
		http.MethodPut,
		http.MethodDelete,
		http.MethodPatch,
	}, ","),
	AllowedHeaders:    "",
	AllowCredentials:  false,
	ExposeHeaders:     "",
	MaxAge:            0,
	SuccessStatusCode: http.StatusNoContent,
}

ConfigDefault is the default config.

Functions

func New

func New(options Config) func(http.Handler) http.Handler

New returns a http.Handler that will be used to handle New requests. To build options more easily, use the options' builder cors.Configure(). Example:

cors.New(cors.Configure().AllowOrigin(""))

Types

type Config

type Config struct {
	AllowedOrigin     string
	AllowCredentials  bool
	AllowedMethods    string
	AllowedHeaders    string
	ExposeHeaders     string
	MaxAge            int
	SuccessStatusCode int
}

Config represents the possible options to configure New for the mock server.

type OptionsBuilder

type OptionsBuilder struct {
	// contains filtered or unexported fields
}

OptionsBuilder facilitates building New options.

func Configure

func Configure() *OptionsBuilder

Configure inits New OptionsBuilder.

func (*OptionsBuilder) AllowCredentials

func (b *OptionsBuilder) AllowCredentials(allow bool) *OptionsBuilder

AllowCredentials sets "Access-Control-Allow-Credentials" header.

func (*OptionsBuilder) AllowMethods

func (b *OptionsBuilder) AllowMethods(methods ...string) *OptionsBuilder

AllowMethods sets the allowed HTTP methods. The header "Access-Control-Allow-Methods" will be used.

func (*OptionsBuilder) AllowOrigin

func (b *OptionsBuilder) AllowOrigin(origin ...string) *OptionsBuilder

AllowOrigin sets allowed origins.

func (*OptionsBuilder) AllowedHeaders

func (b *OptionsBuilder) AllowedHeaders(headers ...string) *OptionsBuilder

AllowedHeaders sets allowed headers. It will set the header "Access-Control-Allow-Header".

func (*OptionsBuilder) Build

func (b *OptionsBuilder) Build() Config

Build returns an Option with previously configured values.

func (*OptionsBuilder) ExposeHeaders

func (b *OptionsBuilder) ExposeHeaders(headers ...string) *OptionsBuilder

ExposeHeaders sets "Access-Control-Expose-Header" header.

func (*OptionsBuilder) MaxAge

func (b *OptionsBuilder) MaxAge(maxAge int) *OptionsBuilder

MaxAge sets New max age.

func (*OptionsBuilder) SuccessStatusCode

func (b *OptionsBuilder) SuccessStatusCode(code int) *OptionsBuilder

SuccessStatusCode sets a custom status code returned on New Options request. If none is specified, the default status code is http.StatusNoContent.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL