v0.0.486 add ginext -> CorsAllowHeader
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m51s

This commit is contained in:
Mike Schwörer 2024-07-18 17:29:18 +02:00
parent 84f124dd4d
commit 1962cb3c52
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
3 changed files with 9 additions and 5 deletions

View File

@ -3,13 +3,14 @@ package ginext
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"strings"
) )
func CorsMiddleware() gin.HandlerFunc { func CorsMiddleware(header []string) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*") c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") c.Writer.Header().Set("Access-Control-Allow-Headers", strings.Join(header, ", "))
c.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, PATCH, DELETE, COUNT") c.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, PATCH, DELETE, COUNT")
if c.Request.Method == "OPTIONS" { if c.Request.Method == "OPTIONS" {

View File

@ -21,6 +21,7 @@ type GinWrapper struct {
opt Options opt Options
allowCors bool allowCors bool
corsAllowHeader []string
ginDebug bool ginDebug bool
bufferBody bool bufferBody bool
requestTimeout time.Duration requestTimeout time.Duration
@ -41,6 +42,7 @@ type ginRouteSpec struct {
type Options struct { type Options struct {
AllowCors *bool // Add cors handler to allow all CORS requests on the default http methods AllowCors *bool // Add cors handler to allow all CORS requests on the default http methods
CorsAllowHeader *[]string // override the default values of Access-Control-Allow-Headers (AllowCors must be true)
GinDebug *bool // Set gin.debug to true (adds more logs) GinDebug *bool // Set gin.debug to true (adds more logs)
SuppressGinLogs *bool // Suppress our custom gin logs (even if GinDebug == true) SuppressGinLogs *bool // Suppress our custom gin logs (even if GinDebug == true)
BufferBody *bool // Buffers the input body stream, this way the ginext error handler can later include the whole request body BufferBody *bool // Buffers the input body stream, this way the ginext error handler can later include the whole request body
@ -75,6 +77,7 @@ func NewEngine(opt Options) *GinWrapper {
opt: opt, opt: opt,
suppressGinLogs: langext.Coalesce(opt.SuppressGinLogs, false), suppressGinLogs: langext.Coalesce(opt.SuppressGinLogs, false),
allowCors: langext.Coalesce(opt.AllowCors, false), allowCors: langext.Coalesce(opt.AllowCors, false),
corsAllowHeader: langext.Coalesce(opt.CorsAllowHeader, []string{"Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization", "accept", "origin", "Cache-Control", "X-Requested-With"}),
ginDebug: ginDebug, ginDebug: ginDebug,
bufferBody: langext.Coalesce(opt.BufferBody, false), bufferBody: langext.Coalesce(opt.BufferBody, false),
requestTimeout: langext.Coalesce(opt.Timeout, 24*time.Hour), requestTimeout: langext.Coalesce(opt.Timeout, 24*time.Hour),
@ -87,7 +90,7 @@ func NewEngine(opt Options) *GinWrapper {
engine.RedirectTrailingSlash = false engine.RedirectTrailingSlash = false
if wrapper.allowCors { if wrapper.allowCors {
engine.Use(CorsMiddleware()) engine.Use(CorsMiddleware(wrapper.corsAllowHeader))
} }
if ginDebug && !wrapper.suppressGinLogs { if ginDebug && !wrapper.suppressGinLogs {

View File

@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.485" const GoextVersion = "0.0.486"
const GoextVersionTimestamp = "2024-07-16T15:22:18+0200" const GoextVersionTimestamp = "2024-07-18T17:29:18+0200"