v0.0.326 ginext::WithSession
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 1m38s

This commit is contained in:
2023-12-02 13:07:36 +01:00
parent 8f15d42173
commit 4aa2f494b1
6 changed files with 73 additions and 18 deletions

View File

@@ -14,7 +14,13 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
reqctx := g.Request.Context()
wrap, stackTrace, panicObj := callPanicSafe(fn, PreContext{wrapper: w, ginCtx: g})
pctx := PreContext{
wrapper: w,
ginCtx: g,
persistantData: &preContextData{},
}
wrap, stackTrace, panicObj := callPanicSafe(fn, pctx)
if panicObj != nil {
fmt.Printf("\n======== ======== STACKTRACE ======== ========\n%s\n======== ======== ======== ========\n\n", stackTrace)
@@ -32,6 +38,13 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
panic("Writing in WrapperFunc is not supported")
}
if pctx.persistantData.sessionObj != nil {
err := pctx.persistantData.sessionObj.Finish(reqctx, wrap)
if err != nil {
wrap = Error(exerr.Wrap(err, "Failed to finish session").Any("originalResponse", wrap).Build())
}
}
if reqctx.Err() == nil {
wrap.Write(g)
}

View File

@@ -15,15 +15,20 @@ import (
)
type PreContext struct {
ginCtx *gin.Context
wrapper *GinWrapper
uri any
query any
body any
rawbody *[]byte
form any
header any
timeout *time.Duration
ginCtx *gin.Context
wrapper *GinWrapper
uri any
query any
body any
rawbody *[]byte
form any
header any
timeout *time.Duration
persistantData *preContextData // must be a ptr, so that we can get the values back in out Wrap func
}
type preContextData struct {
sessionObj SessionObject
}
func (pctx *PreContext) URI(uri any) *PreContext {
@@ -61,6 +66,11 @@ func (pctx *PreContext) WithTimeout(to time.Duration) *PreContext {
return pctx
}
func (pctx *PreContext) WithSession(sessionObj SessionObject) *PreContext {
pctx.persistantData.sessionObj = sessionObj
return pctx
}
func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
if pctx.uri != nil {
if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil {
@@ -152,6 +162,15 @@ func (pctx PreContext) Start() (*AppContext, *gin.Context, *HTTPResponse) {
}
ictx, cancel := context.WithTimeout(context.Background(), langext.Coalesce(pctx.timeout, pctx.wrapper.requestTimeout))
if pctx.persistantData.sessionObj != nil {
err := pctx.persistantData.sessionObj.Init(pctx.ginCtx, ictx)
if err != nil {
cancel()
return nil, nil, langext.Ptr(Error(exerr.Wrap(err, "Failed to init session").Build()))
}
}
actx := CreateAppContext(pctx.ginCtx, ictx, cancel)
return actx, pctx.ginCtx, nil

11
ginext/session.go Normal file
View File

@@ -0,0 +1,11 @@
package ginext
import (
"context"
"github.com/gin-gonic/gin"
)
type SessionObject interface {
Init(g *gin.Context, ctx context.Context) error
Finish(ctx context.Context, resp HTTPResponse) error
}