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
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m35s
This commit is contained in:
parent
6b211d1443
commit
3188b951fb
@ -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()
|
||||
|
@ -20,6 +20,10 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
|
||||
persistantData: &preContextData{},
|
||||
}
|
||||
|
||||
for _, lstr := range w.listenerBeforeRequest {
|
||||
lstr(g)
|
||||
}
|
||||
|
||||
wrap, stackTrace, panicObj := callPanicSafe(fn, pctx)
|
||||
if panicObj != nil {
|
||||
|
||||
@ -45,6 +49,10 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
for _, lstr := range w.listenerAfterRequest {
|
||||
lstr(g, wrap)
|
||||
}
|
||||
|
||||
if reqctx.Err() == nil {
|
||||
wrap.Write(g)
|
||||
}
|
||||
|
4
go.mod
4
go.mod
@ -8,7 +8,7 @@ require (
|
||||
github.com/rs/xid v1.5.0
|
||||
github.com/rs/zerolog v1.31.0
|
||||
go.mongodb.org/mongo-driver v1.13.1
|
||||
golang.org/x/crypto v0.16.0
|
||||
golang.org/x/crypto v0.17.0
|
||||
golang.org/x/sys v0.15.0
|
||||
golang.org/x/term v0.15.0
|
||||
)
|
||||
@ -44,6 +44,6 @@ require (
|
||||
golang.org/x/net v0.19.0 // indirect
|
||||
golang.org/x/sync v0.5.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
google.golang.org/protobuf v1.31.0 // indirect
|
||||
google.golang.org/protobuf v1.32.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@ -145,6 +145,8 @@ golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
|
||||
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
|
||||
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
|
||||
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
|
||||
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
|
||||
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
@ -209,6 +211,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
||||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
|
||||
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.347"
|
||||
const GoextVersion = "0.0.348"
|
||||
|
||||
const GoextVersionTimestamp = "2023-12-16T17:57:42+0100"
|
||||
const GoextVersionTimestamp = "2023-12-27T20:29:37+0100"
|
||||
|
Loading…
Reference in New Issue
Block a user