Compare commits

...

4 Commits

Author SHA1 Message Date
3188b951fb 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
2023-12-27 20:29:37 +01:00
6b211d1443 fix tests
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m37s
2023-12-17 14:04:35 +01:00
b2b9b40792 v0.0.347
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m39s
2023-12-16 17:57:42 +01:00
2f915cb6c1 v0.0.346
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Failing after 1m56s
2023-12-13 16:22:15 +01:00
10 changed files with 73 additions and 35 deletions

View File

@@ -18,10 +18,12 @@ type GinWrapper struct {
engine *gin.Engine engine *gin.Engine
SuppressGinLogs bool SuppressGinLogs bool
allowCors bool allowCors bool
ginDebug bool ginDebug bool
bufferBody bool bufferBody bool
requestTimeout time.Duration requestTimeout time.Duration
listenerBeforeRequest []func(g *gin.Context)
listenerAfterRequest []func(g *gin.Context, resp HTTPResponse)
routeSpecs []ginRouteSpec routeSpecs []ginRouteSpec
} }
@@ -33,35 +35,41 @@ type ginRouteSpec struct {
Handler string 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 // NewEngine creates a new (wrapped) ginEngine
// Parameters are: func NewEngine(opt Options) *GinWrapper {
// - [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 {
engine := gin.New() engine := gin.New()
wrapper := &GinWrapper{ wrapper := &GinWrapper{
engine: engine, engine: engine,
SuppressGinLogs: false, SuppressGinLogs: false,
allowCors: allowCors, allowCors: langext.Coalesce(opt.AllowCors, false),
ginDebug: ginDebug, ginDebug: langext.Coalesce(opt.GinDebug, true),
bufferBody: bufferBody, bufferBody: langext.Coalesce(opt.BufferBody, false),
requestTimeout: timeout, requestTimeout: langext.Coalesce(opt.Timeout, 24*time.Hour),
listenerBeforeRequest: opt.ListenerBeforeRequest,
listenerAfterRequest: opt.ListenerAfterRequest,
} }
engine.RedirectFixedPath = false engine.RedirectFixedPath = false
engine.RedirectTrailingSlash = false engine.RedirectTrailingSlash = false
if allowCors { if wrapper.allowCors {
engine.Use(CorsMiddleware()) engine.Use(CorsMiddleware())
} }
// do not debug-print routes // do not debug-print routes
gin.DebugPrintRouteFunc = func(_, _, _ string, _ int) {} gin.DebugPrintRouteFunc = func(_, _, _ string, _ int) {}
if !ginDebug { if !wrapper.ginDebug {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
ginlogger := gin.Logger() ginlogger := gin.Logger()

View File

@@ -20,6 +20,10 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
persistantData: &preContextData{}, persistantData: &preContextData{},
} }
for _, lstr := range w.listenerBeforeRequest {
lstr(g)
}
wrap, stackTrace, panicObj := callPanicSafe(fn, pctx) wrap, stackTrace, panicObj := callPanicSafe(fn, pctx)
if panicObj != nil { 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 { if reqctx.Err() == nil {
wrap.Write(g) wrap.Write(g)
} }

6
go.mod
View File

@@ -8,7 +8,7 @@ require (
github.com/rs/xid v1.5.0 github.com/rs/xid v1.5.0
github.com/rs/zerolog v1.31.0 github.com/rs/zerolog v1.31.0
go.mongodb.org/mongo-driver v1.13.1 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/sys v0.15.0
golang.org/x/term v0.15.0 golang.org/x/term v0.15.0
) )
@@ -33,7 +33,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect github.com/ugorji/go/codec v1.2.12 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect
@@ -44,6 +44,6 @@ require (
golang.org/x/net v0.19.0 // indirect golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect golang.org/x/sync v0.5.0 // indirect
golang.org/x/text v0.14.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 gopkg.in/yaml.v3 v3.0.1 // indirect
) )

6
go.sum
View File

@@ -88,6 +88,8 @@ github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4= github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI=
github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -143,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.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 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= 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/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-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -207,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.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 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= 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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 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= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.345" const GoextVersion = "0.0.348"
const GoextVersionTimestamp = "2023-12-07T19:39:31+0100" const GoextVersionTimestamp = "2023-12-27T20:29:37+0100"

View File

@@ -1,7 +1,6 @@
package googleapi package googleapi
import ( import (
"fmt"
"gogs.mikescher.com/BlackForestBytes/goext/tst" "gogs.mikescher.com/BlackForestBytes/goext/tst"
"os" "os"
"testing" "testing"
@@ -18,7 +17,7 @@ func TestEncodeMimeMail(t *testing.T) {
MailBody{Plain: "Plain Text"}, MailBody{Plain: "Plain Text"},
nil) nil)
fmt.Printf("%s\n\n", mail) verifyMime(mail)
} }
func TestEncodeMimeMail2(t *testing.T) { func TestEncodeMimeMail2(t *testing.T) {
@@ -35,7 +34,7 @@ func TestEncodeMimeMail2(t *testing.T) {
}, },
nil) nil)
fmt.Printf("%s\n\n", mail) verifyMime(mail)
} }
func TestEncodeMimeMail3(t *testing.T) { func TestEncodeMimeMail3(t *testing.T) {
@@ -53,12 +52,12 @@ func TestEncodeMimeMail3(t *testing.T) {
{Data: []byte("HelloWorld"), Filename: "test.txt", IsInline: false, ContentType: "text/plain"}, {Data: []byte("HelloWorld"), Filename: "test.txt", IsInline: false, ContentType: "text/plain"},
}) })
fmt.Printf("%s\n\n", mail) verifyMime(mail)
} }
func TestEncodeMimeMail4(t *testing.T) { func TestEncodeMimeMail4(t *testing.T) {
b := tst.Must(os.ReadFile("/home/mike/Pictures/Screenshot_20220706_190205.png"))(t) b := tst.Must(os.ReadFile("test_placeholder.png"))(t)
mail := encodeMimeMail( mail := encodeMimeMail(
"noreply@heydyno.de", "noreply@heydyno.de",
@@ -73,5 +72,9 @@ func TestEncodeMimeMail4(t *testing.T) {
{Data: b, Filename: "img.png", IsInline: true, ContentType: "image/png"}, {Data: b, Filename: "img.png", IsInline: true, ContentType: "image/png"},
}) })
fmt.Printf("%s\n\n", mail) verifyMime(mail)
}
func verifyMime(mail string) {
//fmt.Printf("%s\n\n", mail)
} }

View File

@@ -127,7 +127,7 @@ func TestSendMail4(t *testing.T) {
gclient := NewGoogleClient(auth) gclient := NewGoogleClient(auth)
b := tst.Must(os.ReadFile("/home/mike/Pictures/Screenshot_20220706_190205.png"))(t) b := tst.Must(os.ReadFile("test_placeholder.png"))(t)
mail, err := gclient.SendMail( mail, err := gclient.SendMail(
ctx, ctx,

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@@ -10,10 +10,23 @@ var PTrue = Ptr(true)
// PFalse := &false // PFalse := &false
var PFalse = Ptr(false) var PFalse = Ptr(false)
// PNil := &nil
var PNil = Ptr[any](nil)
func Ptr[T any](v T) *T { func Ptr[T any](v T) *T {
return &v return &v
} }
func DblPtr[T any](v T) **T {
v_ := &v
return &v_
}
func DblPtrNil[T any]() **T {
var v *T = nil
return &v
}
func PtrInt32(v int32) *int32 { func PtrInt32(v int32) *int32 {
return &v return &v
} }

View File

@@ -1,11 +1,11 @@
package pagination package pagination
type Pagination struct { type Pagination struct {
Page int `json:"page"` // page (first page == 1) Page int `json:"page"` // page (first page == 1)
Limit int `json:"limit"` // max-page-size Limit int `json:"limit"` // max-page-size
TotalPages int `json:"totalPages"` // total page-count TotalPages int `json:"totalPages"` // total page-count
TotalItems int `json:"totalItems"` // total items-count TotalItems int `json:"totalItems"` // total items-count
CurrentPageCount int `json:"currntPageCount"` // item-count in current page ( == len(data) ) CurrentPageCount int `json:"currentPageCount"` // item-count in current page ( == len(data) )
} }
func CalcPaginationTotalPages(totalItems int, limit int) int { func CalcPaginationTotalPages(totalItems int, limit int) int {