cors

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2024 License: GPL-3.0 Imports: 7 Imported by: 0

README

Gin Cors Middleware

The gin-cors package can be used in every Gin project and configure the cors behaviour of your application.

Getting started

After installing and setting up Go you can create yor first project with gin.

In your main go file create a basic gin http server with the gin-cors middleware package

package main

import (
  "net/http"

  "github.com/gin-gonic/gin"
  cors "github.com/OnlyNico43/gin-cors"
)

func main() {
  r := gin.Default()

  r.Use(cors.CorsMiddleware(cors.DefaultConfig()))

  r.GET("/ping", func(c *gin.Context) {
    c.String(http.StatusOK, "pong")
  })

  r.Run(":8080")
}

Done, now your application is equipped with a basic cors configuration

How to configure

You can either use the default configuration or decide to use your own values in the Config object.

For more details please see the documentation

Documentation

Overview

This is a cors middleware for the gin http framwork and can be used to configure the cors behaviour of your application

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CorsMiddleware

func CorsMiddleware(config Config) gin.HandlerFunc

CORS Middleware for Gin which handles CORS headers and preflight requests needs a cors config

Types

type Config

type Config struct {
	// All the allowed origins in an array. The default is "*"
	// The default cannot be used when AllowCredentials is true
	// [MDN Web Docs]
	//
	// [MDN Web Docs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
	AllowedOrigins []string
	// All the allowed HTTP Methodes. The default is "*"
	// The default cannot be used when AllowCredentials is true
	// [MDN Web Docs]
	//
	// [MDN Web Docs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
	AllowedMethods []string
	// All the allowed Headers that can be sent from the client. The default is "*"
	// The default cannot be used when AllowCredentials is true
	// Note that the Authorization header cannot be wildcarded and needs to be listed explicitly [MDN Web Docs]
	//
	// [MDN Web Docs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
	AllowedHeaders []string
	// The headers which should be readable by the client. The default is "*"
	// The default cannot be used when AllowCredentials is true
	// [MDN Web Docs]
	//
	// [MDN Web Docs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Expose-Headers
	ExposeHeaders []string
	// If you allow receiving cookies and Authorization headers. The default is false
	// [MDN Web Docs]
	//
	// [MDN Web Docs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
	AllowCredentials bool
	// The maximum age of your preflight requests. The default is 1 day
	// [MDN Web Docs]
	//
	// [MDN Web Docs]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Max-Age
	MaxAge time.Duration
}

Defines the configuration of your cors middleware

func DefaultConfig

func DefaultConfig() Config

The default config for the cors middleware

Config{
	AllowedOrigins: []string{"*"},
	AllowedMethods: []string{"*"},
	AllowedHeaders: []string{"Authorization", "Conten-Type", "Content-Length"},
	ExposeHeaders:  []string{"Content-Length"},
	AllowCredentials: false,
	MaxAge: 24 * time.Hour,
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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