Compare commits

...

3 Commits

Author SHA1 Message Date
49d423915c v0.0.487
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m54s
2024-07-18 17:45:56 +02:00
1962cb3c52 v0.0.486 add ginext -> CorsAllowHeader
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m51s
2024-07-18 17:29:18 +02:00
84f124dd4d v0.0.485
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m44s
2024-07-16 15:22:18 +02:00
5 changed files with 20 additions and 10 deletions

View File

@@ -3,13 +3,17 @@ 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(allowheader []string, exposeheader []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(allowheader, ", "))
if len(exposeheader) > 0 {
c.Writer.Header().Set("Access-Control-Expose-Headers", strings.Join(exposeheader, ", "))
}
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,8 @@ type GinWrapper struct {
opt Options opt Options
allowCors bool allowCors bool
corsAllowHeader []string
corsExposeHeader []string
ginDebug bool ginDebug bool
bufferBody bool bufferBody bool
requestTimeout time.Duration requestTimeout time.Duration
@@ -41,6 +43,8 @@ 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)
CorsExposeHeader *[]string // return Access-Control-Expose-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 +79,8 @@ 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"}),
corsExposeHeader: langext.Coalesce(opt.CorsExposeHeader, []string{}),
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 +93,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, wrapper.corsExposeHeader))
} }
if ginDebug && !wrapper.suppressGinLogs { if ginDebug && !wrapper.suppressGinLogs {

View File

@@ -105,14 +105,14 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
WithType(exerr.TypeBindFailJSON). WithType(exerr.TypeBindFailJSON).
Str("struct_type", fmt.Sprintf("%T", pctx.body)). Str("struct_type", fmt.Sprintf("%T", pctx.body)).
Build() Build()
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err)) return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err))
} }
} else { } else {
if !pctx.ignoreWrongContentType { if !pctx.ignoreWrongContentType {
err := exerr.New(exerr.TypeBindFailJSON, "missing JSON body"). err := exerr.New(exerr.TypeBindFailJSON, "missing JSON body").
Str("struct_type", fmt.Sprintf("%T", pctx.body)). Str("struct_type", fmt.Sprintf("%T", pctx.body)).
Build() Build()
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err)) return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err))
} }
} }
} }
@@ -121,14 +121,14 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
if brc, ok := pctx.ginCtx.Request.Body.(dataext.BufferedReadCloser); ok { if brc, ok := pctx.ginCtx.Request.Body.(dataext.BufferedReadCloser); ok {
v, err := brc.BufferedAll() v, err := brc.BufferedAll()
if err != nil { if err != nil {
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "URI", err)) return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err))
} }
*pctx.rawbody = v *pctx.rawbody = v
} else { } else {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
_, err := io.Copy(buf, pctx.ginCtx.Request.Body) _, err := io.Copy(buf, pctx.ginCtx.Request.Body)
if err != nil { if err != nil {
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "URI", err)) return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err))
} }
*pctx.rawbody = buf.Bytes() *pctx.rawbody = buf.Bytes()
} }

View File

@@ -68,7 +68,7 @@ func (j jsonAPIErrResponse) Unwrap() error {
return j.err return j.err
} }
func Error(e error) InspectableHTTPErrorResponse { func Error(e error) HTTPResponse {
return &jsonAPIErrResponse{ return &jsonAPIErrResponse{
err: exerr.FromError(e), err: exerr.FromError(e),
} }

View File

@@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.484" const GoextVersion = "0.0.487"
const GoextVersionTimestamp = "2024-07-16T15:16:56+0200" const GoextVersionTimestamp = "2024-07-18T17:45:56+0200"