v0.0.348 added listener and options to goext
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m35s

This commit is contained in:
2023-12-27 20:29:37 +01:00
parent 6b211d1443
commit 3188b951fb
5 changed files with 42 additions and 22 deletions

View File

@@ -18,10 +18,12 @@ type GinWrapper struct {
engine *gin.Engine
SuppressGinLogs bool
allowCors bool
ginDebug bool
bufferBody bool
requestTimeout time.Duration
allowCors bool
ginDebug bool
bufferBody bool
requestTimeout time.Duration
listenerBeforeRequest []func(g *gin.Context)
listenerAfterRequest []func(g *gin.Context, resp HTTPResponse)
routeSpecs []ginRouteSpec
}
@@ -33,35 +35,41 @@ type ginRouteSpec struct {
Handler string
}
type Options struct {
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)
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
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
}
// NewEngine creates a new (wrapped) ginEngine
// Parameters are:
// - [allowCors] Add cors handler to allow all CORS requests on the default http methods
// - [ginDebug] Set gin.debug to true (adds more logs)
// - [bufferBody] Buffers the input body stream, this way the ginext error handler can later include the whole request body
// - [timeout] The default handler timeout
func NewEngine(allowCors bool, ginDebug bool, bufferBody bool, timeout time.Duration) *GinWrapper {
func NewEngine(opt Options) *GinWrapper {
engine := gin.New()
wrapper := &GinWrapper{
engine: engine,
SuppressGinLogs: false,
allowCors: allowCors,
ginDebug: ginDebug,
bufferBody: bufferBody,
requestTimeout: timeout,
engine: engine,
SuppressGinLogs: false,
allowCors: langext.Coalesce(opt.AllowCors, false),
ginDebug: langext.Coalesce(opt.GinDebug, true),
bufferBody: langext.Coalesce(opt.BufferBody, false),
requestTimeout: langext.Coalesce(opt.Timeout, 24*time.Hour),
listenerBeforeRequest: opt.ListenerBeforeRequest,
listenerAfterRequest: opt.ListenerAfterRequest,
}
engine.RedirectFixedPath = false
engine.RedirectTrailingSlash = false
if allowCors {
if wrapper.allowCors {
engine.Use(CorsMiddleware())
}
// do not debug-print routes
gin.DebugPrintRouteFunc = func(_, _, _ string, _ int) {}
if !ginDebug {
if !wrapper.ginDebug {
gin.SetMode(gin.ReleaseMode)
ginlogger := gin.Logger()