cors

package
v0.0.0-...-38a9cd2 Latest Latest
Warning

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

Go to latest
Published: May 21, 2022 License: MIT Imports: 4 Imported by: 0

README

CORS Middleware

Cross-Origin Resource Sharing

Examples

package main
import (
  "github.com/gostack-labs/bytego"
  "github.com/gostack-labs/bytego/middleware/cors"
)

func main() {
    app := bytego.New()

    //Default config
    app.Use(cors.New())

    //Custom config
    app.Use(cors.New(cors.Config{
        AllowOrigins: []string{"https://bytego.dev", "https://github.com"},
        AllowHeaders:  []string{"Origin", "Content-Type", "Accept"},
    }))
    app.GET("/", func(c *bytego.Ctx) error {
        return c.String(200, "hello")
    })
    _ = app.Run(":8080")
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	AllowOrigins: []string{"*"},
	AllowMethods: []string{
		http.MethodGet,
		http.MethodPost,
		http.MethodHead,
		http.MethodPut,
		http.MethodDelete,
		http.MethodPatch,
	},
	AllowHeaders:     []string{"Content-Type"},
	AllowCredentials: false,
	ExposeHeaders:    []string{},
	MaxAge:           12 * 60 * 60,
}

Functions

func New

func New(config ...Config) bytego.HandlerFunc

Types

type Config

type Config struct {
	// AllowOrigin defines a list of origins that may access the resource.
	// Default value []string{"*"}
	AllowOrigins []string

	// request `Access-Control-Allow-Methods` header value
	// Default value []string{"GET","POST","HEAD","PUT","DELETE","PATCH"}
	AllowMethods []string

	// AllowHeaders defines a list of request headers that can be used when
	// making the actual request. This is in response to a preflight request.
	//
	// Optional. Default value "".
	AllowHeaders []string
	// AllowCredentials indicates whether or not the response to the request
	// can be exposed when the credentials flag is true. When used as part of
	// a response to a preflight request, this indicates whether or not the
	// actual request can be made using credentials.
	//
	// Optional. Default value false.
	AllowCredentials bool

	// ExposeHeaders defines a whitelist headers that clients are allowed to
	// access.
	//
	// Optional. Default value "".
	ExposeHeaders []string

	// MaxAge indicates how long (in seconds) the results of a preflight request
	// can be cached.
	//
	// Optional. Default value 0.
	MaxAge int
}

Jump to

Keyboard shortcuts

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