Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
49d423915c
|
|||
1962cb3c52
|
|||
84f124dd4d
|
|||
ff8e066135
|
|||
bc5c61e43d
|
@@ -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" {
|
||||||
|
@@ -21,12 +21,16 @@ 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
|
||||||
listenerBeforeRequest []func(g *gin.Context)
|
listenerBeforeRequest []func(g *gin.Context)
|
||||||
listenerAfterRequest []func(g *gin.Context, resp HTTPResponse)
|
listenerAfterRequest []func(g *gin.Context, resp HTTPResponse)
|
||||||
|
|
||||||
|
buildRequestBindError func(g *gin.Context, fieldtype string, err error) HTTPResponse
|
||||||
|
|
||||||
routeSpecs []ginRouteSpec
|
routeSpecs []ginRouteSpec
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,15 +42,18 @@ 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
|
||||||
GinDebug *bool // Set gin.debug to true (adds more logs)
|
CorsAllowHeader *[]string // override the default values of Access-Control-Allow-Headers (AllowCors must be true)
|
||||||
SuppressGinLogs *bool // Suppress our custom gin logs (even if GinDebug == true)
|
CorsExposeHeader *[]string // return Access-Control-Expose-Headers (AllowCors must be true)
|
||||||
BufferBody *bool // Buffers the input body stream, this way the ginext error handler can later include the whole request body
|
GinDebug *bool // Set gin.debug to true (adds more logs)
|
||||||
Timeout *time.Duration // The default handler timeout
|
SuppressGinLogs *bool // Suppress our custom gin logs (even if GinDebug == true)
|
||||||
ListenerBeforeRequest []func(g *gin.Context) // Register listener that are called before the handler method
|
BufferBody *bool // Buffers the input body stream, this way the ginext error handler can later include the whole request body
|
||||||
ListenerAfterRequest []func(g *gin.Context, resp HTTPResponse) // Register listener that are called after the handler method
|
Timeout *time.Duration // The default handler timeout
|
||||||
DebugTrimHandlerPrefixes []string // Trim these prefixes from the handler names in the debug print
|
ListenerBeforeRequest []func(g *gin.Context) // Register listener that are called before the handler method
|
||||||
DebugReplaceHandlerNames map[string]string // Replace handler names in debug output
|
ListenerAfterRequest []func(g *gin.Context, resp HTTPResponse) // Register listener that are called after the handler method
|
||||||
|
DebugTrimHandlerPrefixes []string // Trim these prefixes from the handler names in the debug print
|
||||||
|
DebugReplaceHandlerNames map[string]string // Replace handler names in debug output
|
||||||
|
BuildRequestBindError func(g *gin.Context, fieldtype string, err error) HTTPResponse // Override function which generates the HTTPResponse errors that are returned by the preContext..Start() methids
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEngine creates a new (wrapped) ginEngine
|
// NewEngine creates a new (wrapped) ginEngine
|
||||||
@@ -72,18 +79,21 @@ 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),
|
||||||
listenerBeforeRequest: opt.ListenerBeforeRequest,
|
listenerBeforeRequest: opt.ListenerBeforeRequest,
|
||||||
listenerAfterRequest: opt.ListenerAfterRequest,
|
listenerAfterRequest: opt.ListenerAfterRequest,
|
||||||
|
buildRequestBindError: langext.Conditional(opt.BuildRequestBindError == nil, defaultBuildRequestBindError, opt.BuildRequestBindError),
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.RedirectFixedPath = false
|
engine.RedirectFixedPath = false
|
||||||
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 {
|
||||||
@@ -222,3 +232,7 @@ func (w *GinWrapper) ForwardRequest(writer http.ResponseWriter, req *http.Reques
|
|||||||
func (w *GinWrapper) ListRoutes() []gin.RouteInfo {
|
func (w *GinWrapper) ListRoutes() []gin.RouteInfo {
|
||||||
return w.engine.Routes()
|
return w.engine.Routes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func defaultBuildRequestBindError(g *gin.Context, fieldtype string, err error) HTTPResponse {
|
||||||
|
return Error(err)
|
||||||
|
}
|
||||||
|
@@ -15,16 +15,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type PreContext struct {
|
type PreContext struct {
|
||||||
ginCtx *gin.Context
|
ginCtx *gin.Context
|
||||||
wrapper *GinWrapper
|
wrapper *GinWrapper
|
||||||
uri any
|
uri any
|
||||||
query any
|
query any
|
||||||
body any
|
body any
|
||||||
rawbody *[]byte
|
rawbody *[]byte
|
||||||
form any
|
form any
|
||||||
header any
|
header any
|
||||||
timeout *time.Duration
|
timeout *time.Duration
|
||||||
persistantData *preContextData // must be a ptr, so that we can get the values back in out Wrap func
|
persistantData *preContextData // must be a ptr, so that we can get the values back in out Wrap func
|
||||||
|
ignoreWrongContentType bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type preContextData struct {
|
type preContextData struct {
|
||||||
@@ -71,6 +72,11 @@ func (pctx *PreContext) WithSession(sessionObj SessionObject) *PreContext {
|
|||||||
return pctx
|
return pctx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pctx *PreContext) IgnoreWrongContentType() *PreContext {
|
||||||
|
pctx.ignoreWrongContentType = true
|
||||||
|
return pctx
|
||||||
|
}
|
||||||
|
|
||||||
func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
|
func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
|
||||||
if pctx.uri != nil {
|
if pctx.uri != nil {
|
||||||
if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil {
|
if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil {
|
||||||
@@ -78,7 +84,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
|
|||||||
WithType(exerr.TypeBindFailURI).
|
WithType(exerr.TypeBindFailURI).
|
||||||
Str("struct_type", fmt.Sprintf("%T", pctx.uri)).
|
Str("struct_type", fmt.Sprintf("%T", pctx.uri)).
|
||||||
Build()
|
Build()
|
||||||
return nil, nil, langext.Ptr(Error(err))
|
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "URI", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +94,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
|
|||||||
WithType(exerr.TypeBindFailQuery).
|
WithType(exerr.TypeBindFailQuery).
|
||||||
Str("struct_type", fmt.Sprintf("%T", pctx.query)).
|
Str("struct_type", fmt.Sprintf("%T", pctx.query)).
|
||||||
Build()
|
Build()
|
||||||
return nil, nil, langext.Ptr(Error(err))
|
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "QUERY", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,13 +105,15 @@ 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(Error(err))
|
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := exerr.New(exerr.TypeBindFailJSON, "missing JSON body").
|
if !pctx.ignoreWrongContentType {
|
||||||
Str("struct_type", fmt.Sprintf("%T", pctx.body)).
|
err := exerr.New(exerr.TypeBindFailJSON, "missing JSON body").
|
||||||
Build()
|
Str("struct_type", fmt.Sprintf("%T", pctx.body)).
|
||||||
return nil, nil, langext.Ptr(Error(err))
|
Build()
|
||||||
|
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "JSON", err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,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(Error(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(Error(err))
|
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "BODY", err))
|
||||||
}
|
}
|
||||||
*pctx.rawbody = buf.Bytes()
|
*pctx.rawbody = buf.Bytes()
|
||||||
}
|
}
|
||||||
@@ -133,7 +141,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
|
|||||||
WithType(exerr.TypeBindFailFormData).
|
WithType(exerr.TypeBindFailFormData).
|
||||||
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
|
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
|
||||||
Build()
|
Build()
|
||||||
return nil, nil, langext.Ptr(Error(err))
|
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err))
|
||||||
}
|
}
|
||||||
} else if pctx.ginCtx.ContentType() == "application/x-www-form-urlencoded" {
|
} else if pctx.ginCtx.ContentType() == "application/x-www-form-urlencoded" {
|
||||||
if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil {
|
if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil {
|
||||||
@@ -141,13 +149,15 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
|
|||||||
WithType(exerr.TypeBindFailFormData).
|
WithType(exerr.TypeBindFailFormData).
|
||||||
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
|
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
|
||||||
Build()
|
Build()
|
||||||
return nil, nil, langext.Ptr(Error(err))
|
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err := exerr.New(exerr.TypeBindFailFormData, "missing form body").
|
if !pctx.ignoreWrongContentType {
|
||||||
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
|
err := exerr.New(exerr.TypeBindFailFormData, "missing form body").
|
||||||
Build()
|
Str("struct_type", fmt.Sprintf("%T", pctx.form)).
|
||||||
return nil, nil, langext.Ptr(Error(err))
|
Build()
|
||||||
|
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "FORM", err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +167,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
|
|||||||
WithType(exerr.TypeBindFailHeader).
|
WithType(exerr.TypeBindFailHeader).
|
||||||
Str("struct_type", fmt.Sprintf("%T", pctx.query)).
|
Str("struct_type", fmt.Sprintf("%T", pctx.query)).
|
||||||
Build()
|
Build()
|
||||||
return nil, nil, langext.Ptr(Error(err))
|
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "HEADER", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +179,7 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
|
|||||||
err := pctx.persistantData.sessionObj.Init(pctx.ginCtx, actx)
|
err := pctx.persistantData.sessionObj.Init(pctx.ginCtx, actx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
actx.Cancel()
|
actx.Cancel()
|
||||||
return nil, nil, langext.Ptr(Error(exerr.Wrap(err, "Failed to init session").Build()))
|
return nil, nil, langext.Ptr(pctx.wrapper.buildRequestBindError(pctx.ginCtx, "INIT", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -36,6 +36,12 @@ type InspectableHTTPResponse interface {
|
|||||||
Headers() []string
|
Headers() []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HTTPErrorResponse interface {
|
||||||
|
HTTPResponse
|
||||||
|
|
||||||
|
Error() error
|
||||||
|
}
|
||||||
|
|
||||||
func NotImplemented() HTTPResponse {
|
func NotImplemented() HTTPResponse {
|
||||||
return Error(exerr.New(exerr.TypeNotImplemented, "").Build())
|
return Error(exerr.New(exerr.TypeNotImplemented, "").Build())
|
||||||
}
|
}
|
||||||
|
@@ -13,6 +13,10 @@ type jsonAPIErrResponse struct {
|
|||||||
cookies []cookieval
|
cookies []cookieval
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (j jsonAPIErrResponse) Error() error {
|
||||||
|
return j.err
|
||||||
|
}
|
||||||
|
|
||||||
func (j jsonAPIErrResponse) Write(g *gin.Context) {
|
func (j jsonAPIErrResponse) Write(g *gin.Context) {
|
||||||
for _, v := range j.headers {
|
for _, v := range j.headers {
|
||||||
g.Header(v.Key, v.Val)
|
g.Header(v.Key, v.Val)
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.482"
|
const GoextVersion = "0.0.487"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-07-12T16:33:42+0200"
|
const GoextVersionTimestamp = "2024-07-18T17:45:56+0200"
|
||||||
|
Reference in New Issue
Block a user