Compare commits

...

3 Commits

Author SHA1 Message Date
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
ff8e066135 v0.0.484
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m58s
2024-07-16 15:16:56 +02:00
bc5c61e43d v0.0.483
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 3m37s
2024-07-16 15:08:37 +02:00
5 changed files with 66 additions and 38 deletions

View File

@@ -27,6 +27,8 @@ type GinWrapper struct {
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 +40,16 @@ 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) 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
Timeout *time.Duration // The default handler timeout Timeout *time.Duration // The default handler timeout
ListenerBeforeRequest []func(g *gin.Context) // Register listener that are called before the handler method ListenerBeforeRequest []func(g *gin.Context) // Register listener that are called before the handler method
ListenerAfterRequest []func(g *gin.Context, resp HTTPResponse) // Register listener that are called after the handler method 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 DebugTrimHandlerPrefixes []string // Trim these prefixes from the handler names in the debug print
DebugReplaceHandlerNames map[string]string // Replace handler names in debug output 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
@@ -77,6 +80,7 @@ func NewEngine(opt Options) *GinWrapper {
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
@@ -222,3 +226,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)
}

View File

@@ -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))
} }
} }

View File

@@ -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())
} }

View File

@@ -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)

View File

@@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.482" const GoextVersion = "0.0.485"
const GoextVersionTimestamp = "2024-07-12T16:33:42+0200" const GoextVersionTimestamp = "2024-07-16T15:22:18+0200"