cors

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2024 License: MIT Imports: 3 Imported by: 0

README

Cors

The Cors module enhances github.com/gin-contrib/cors with more convenient methods and configuration.

This project has been developped by the Aloe team and is now open source.

tests Go Reference

Overview

The cors module offers:

  • A Conf structure with yaml and mapstructure tags
  • Builder methods for the convenient creation of a new middleware
  • Default values to reduce code repetition in your projects
  • Easy integration because it embed a native github.com/gin-contrib/cors

Concepts

Build your middleware on default configuration

The default middleware provides an open CORS configuration that can be useful during the development process.

    builder := new(cors.Builder)
    conf := builder.New().Build()
Easily add custom values

Enhance the security of your Gin API by specifying allowed origins in addition to the default configuration.

    builder := new(cors.Builder)
    conf := builder.New().WithOrigins("http://localhost:8080").Build()

Usage

Use the cors middleware on your gin endpoints.

    Router := gin.New()
    corsBuilder := new(cors.Builder)

    Router.Use(cors.Middleware(corsBuilder.New().WithOrigins("http://localhost:8080").Build()))

Use it with default configuration.

    Router := gin.New()
    Router.Use(cors.Middleware(nil))

Use if with the Conf struct

    Router := gin.New()

    conf := &cors.Conf{
        // your parsed configuration
    }

    corsBuilder := new(cors.Builder)
  
    Router.Use(cors.Middleware(corsBuilder.NewFromConf(conf).Build()))

Contributing

This section will be added soon.

License

Client is released under the MIT license. See LICENSE.txt.

Documentation

Index

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

type Builder struct {
	*cors.Config
}

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

func (builder *Builder) Build() *cors.Config

Build returns the finalized cors.Config after applying the configured options.

func (*Builder) New

func (builder *Builder) New() *Builder

New creates a new Builder instance with default CORS configuration values.

func (*Builder) NewFromConfig

func (builder *Builder) NewFromConfig(c *Conf) *Builder

NewFromConfig creates a new Builder instance with a custom configuration provided by the Conf parameter.

func (*Builder) WithCredentials

func (builder *Builder) WithCredentials(allowCredentials bool) *Builder

WithCredentials sets whether credentials (including cookies) can be sent with the CORS request.

func (*Builder) WithExposeHeaders added in v1.0.1

func (builder *Builder) WithExposeHeaders(headers ...string) *Builder

WithExposeHeaders sets the expose headers for CORS.

func (*Builder) WithHeaders

func (builder *Builder) WithHeaders(headers ...string) *Builder

WithHeaders sets the allowed headers for CORS.

func (*Builder) WithMethods

func (builder *Builder) WithMethods(methods ...string) *Builder

WithMethods sets the allowed HTTP methods for CORS.

func (*Builder) WithOrigins

func (builder *Builder) WithOrigins(origins ...string) *Builder

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.

Jump to

Keyboard shortcuts

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