Documentation
¶
Index ¶
- func Middleware(c *cors.Config) gin.HandlerFunc
- type Builder
- func (builder *Builder) Build() *cors.Config
- func (builder *Builder) New() *Builder
- func (builder *Builder) NewFromConfig(c *Conf) *Builder
- func (builder *Builder) WithCredentials(allowCredentials bool) *Builder
- func (builder *Builder) WithHeaders(headers ...string) *Builder
- func (builder *Builder) WithMethods(methods ...string) *Builder
- func (builder *Builder) WithOrigins(origins ...string) *Builder
- type Conf
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
func Middleware(c *cors.Config) gin.HandlerFunc
Middleware is a Gin middleware function that adds CORS (Cross-Origin Resource Sharing) support to the HTTP responses. It takes a cors.Config parameter and returns a Gin middleware handler. If the provided cors.Config is nil, it creates a default configuration using the Builder and applies it to the middleware.
Use the Builder to build the cors.Config.
Types ¶
type Builder ¶
Builder is a builder pattern for creating CORS (Cross-Origin Resource Sharing) configurations. It provides methods to set various CORS options and ultimately build a cors.Config.
func (*Builder) Build ¶
Build returns the finalized cors.Config after applying the configured options.
func (*Builder) NewFromConfig ¶
NewFromConfig creates a new Builder instance with a custom configuration provided by the Conf parameter.
func (*Builder) WithCredentials ¶
WithCredentials sets whether credentials (including cookies) can be sent with the CORS request.
func (*Builder) WithHeaders ¶
WithHeaders sets the allowed headers for CORS.
func (*Builder) WithMethods ¶
WithMethods sets the allowed HTTP methods for CORS.
func (*Builder) WithOrigins ¶
WithOrigins sets the allowed origins for CORS.
type Conf ¶
type Conf struct { AllowOrigins []string `yaml:"allow_origins" mapstructure:"allow_origins"` // List of allowed origins. "*" means allows all origins. AllowMethods []string `yaml:"allow_methods" mapstructure:"allow_methods"` // List of allowed HTTP methods. AllowHeaders []string `yaml:"allow_headers" mapstructure:"allow_headers"` // List of allowed headers. ExposeHeaders []string `yaml:"expose_headers" mapstructure:"expose_headers"` // List of headers exposed to the browser. AllowCredentials bool `yaml:"allow_credentials" mapstructure:"allow_credentials"` // Whether credentials can be included. MaxAge time.Duration `yaml:"max_age" mapstructure:"max_age"` // Maximum age of a preflight request. }
Conf represents the configuration structure for CORS (Cross-Origin Resource Sharing) settings. It includes fields for specifying allowed origins, methods, headers, exposed headers, allowing credentials, and the maximum age of a CORS preflight request. It is actually a mirror of cors.Config structure.