diff --git a/ginext/appcontext.go b/ginext/appcontext.go new file mode 100644 index 0000000..8dc802c --- /dev/null +++ b/ginext/appcontext.go @@ -0,0 +1,59 @@ +package ginext + +import ( + "context" + "github.com/gin-gonic/gin" + "time" +) + +type AppContext struct { + inner context.Context + cancelFunc context.CancelFunc + cancelled bool + ginContext *gin.Context +} + +func CreateAppContext(g *gin.Context, innerCtx context.Context, cancelFn context.CancelFunc) *AppContext { + return &AppContext{ + inner: innerCtx, + cancelFunc: cancelFn, + cancelled: false, + ginContext: g, + } +} + +func (ac *AppContext) Deadline() (deadline time.Time, ok bool) { + return ac.inner.Deadline() +} + +func (ac *AppContext) Done() <-chan struct{} { + return ac.inner.Done() +} + +func (ac *AppContext) Err() error { + return ac.inner.Err() +} + +func (ac *AppContext) Value(key any) any { + return ac.inner.Value(key) +} + +func (ac *AppContext) Cancel() { + ac.cancelled = true + ac.cancelFunc() +} + +func (ac *AppContext) RequestURI() string { + if ac.ginContext != nil && ac.ginContext.Request != nil { + return ac.ginContext.Request.Method + " :: " + ac.ginContext.Request.RequestURI + } else { + return "" + } +} + +func (ac *AppContext) FinishSuccess(res HTTPResponse) HTTPResponse { + if ac.cancelled { + panic("Cannot finish a cancelled request") + } + return res +} diff --git a/ginext/apierr/enums.go b/ginext/commonapierr/enums.go similarity index 96% rename from ginext/apierr/enums.go rename to ginext/commonapierr/enums.go index 60e3f14..e974687 100644 --- a/ginext/apierr/enums.go +++ b/ginext/commonapierr/enums.go @@ -1,4 +1,4 @@ -package apierr +package commonapierr type APIErrorCode struct { HTTPStatusCode int diff --git a/ginext/engine.go b/ginext/engine.go index c405375..291b508 100644 --- a/ginext/engine.go +++ b/ginext/engine.go @@ -1,6 +1,9 @@ package ginext -import "github.com/gin-gonic/gin" +import ( + "github.com/gin-gonic/gin" + "time" +) type GinWrapper struct { engine *gin.Engine @@ -9,9 +12,10 @@ type GinWrapper struct { allowCors bool ginDebug bool returnRawErrors bool + requestTimeout time.Duration } -func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool) *GinWrapper { +func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool, timeout time.Duration) *GinWrapper { engine := gin.New() wrapper := &GinWrapper{ @@ -20,6 +24,7 @@ func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool) *GinWrapper allowCors: allowCors, ginDebug: ginDebug, returnRawErrors: returnRawErrors, + requestTimeout: timeout, } engine.RedirectFixedPath = false diff --git a/ginext/funcWrapper.go b/ginext/funcWrapper.go index af979f5..861fd61 100644 --- a/ginext/funcWrapper.go +++ b/ginext/funcWrapper.go @@ -1,15 +1,18 @@ package ginext import ( + "context" "errors" "fmt" "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" "github.com/rs/zerolog/log" - "gogs.mikescher.com/BlackForestBytes/goext/ginext/apierr" + "gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr" + "gogs.mikescher.com/BlackForestBytes/goext/langext" "runtime/debug" ) -type WHandlerFunc func(*gin.Context) HTTPResponse +type WHandlerFunc func(PreContext) HTTPResponse func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc { @@ -19,14 +22,14 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc { reqctx := g.Request.Context() - wrap, stackTrace, panicObj := callPanicSafe(fn, g) + wrap, stackTrace, panicObj := callPanicSafe(fn, PreContext{wrapper: w, ginCtx: g}) if panicObj != nil { fmt.Printf("\n======== ======== STACKTRACE ======== ========\n%s\n======== ======== ======== ========\n\n", stackTrace) log.Error(). Interface("panicObj", panicObj). Str("trace", stackTrace). Msg("Panic occured (in gin handler)") - wrap = APIError(g, apierr.Panic, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v", panicObj))) + wrap = APIError(g, commonapierr.Panic, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v", panicObj))) } if g.Writer.Written() { @@ -39,7 +42,75 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc { } } -func callPanicSafe(fn WHandlerFunc, g *gin.Context) (res HTTPResponse, stackTrace string, panicObj any) { +type PreContext struct { + ginCtx *gin.Context + wrapper *GinWrapper + uri any + query any + body any + form any +} + +func (pctx *PreContext) URI(uri any) *PreContext { + pctx.uri = uri + return pctx +} + +func (pctx *PreContext) Query(query any) *PreContext { + pctx.query = query + return pctx +} + +func (pctx *PreContext) Body(body any) *PreContext { + pctx.body = body + return pctx +} + +func (pctx *PreContext) Form(form any) *PreContext { + pctx.form = form + return pctx +} + +func (pctx PreContext) Start() (*AppContext, *HTTPResponse) { + if pctx.uri != nil { + if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil { + return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailURI, "Failed to read uri", err)) + } + } + + if pctx.query != nil { + if err := pctx.ginCtx.ShouldBindQuery(pctx.query); err != nil { + return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailQuery, "Failed to read query", err)) + } + } + + if pctx.body != nil { + if pctx.ginCtx.ContentType() == "application/json" { + if err := pctx.ginCtx.ShouldBindJSON(pctx.body); err != nil { + return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailJSON, "Failed to read body", err)) + } + } else { + return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailJSON, "missing JSON body", nil)) + } + } + + if pctx.form != nil { + if pctx.ginCtx.ContentType() == "multipart/form-data" { + if err := pctx.ginCtx.ShouldBindWith(pctx.form, binding.Form); err != nil { + return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailFormData, "Failed to read multipart-form", err)) + } + } else { + return nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailJSON, "missing form body", nil)) + } + } + + ictx, cancel := context.WithTimeout(context.Background(), pctx.wrapper.requestTimeout) + actx := CreateAppContext(pctx.ginCtx, ictx, cancel) + + return actx, nil +} + +func callPanicSafe(fn WHandlerFunc, pctx PreContext) (res HTTPResponse, stackTrace string, panicObj any) { defer func() { if rec := recover(); rec != nil { res = nil @@ -48,6 +119,6 @@ func callPanicSafe(fn WHandlerFunc, g *gin.Context) (res HTTPResponse, stackTrac } }() - res = fn(g) + res = fn(pctx) return res, "", nil } diff --git a/ginext/resp.go b/ginext/resp.go index a4ccb17..7434029 100644 --- a/ginext/resp.go +++ b/ginext/resp.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/rs/zerolog/log" - "gogs.mikescher.com/BlackForestBytes/goext/ginext/apierr" + "gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr" json "gogs.mikescher.com/BlackForestBytes/goext/gojson" "gogs.mikescher.com/BlackForestBytes/goext/langext" "runtime/debug" @@ -90,15 +90,15 @@ func Download(mimetype string, filepath string, filename string) HTTPResponse { return &fileHTTPResponse{mimetype: mimetype, filepath: filepath, filename: &filename} } -func APIError(g *gin.Context, errcode apierr.APIErrorCode, msg string, e error) HTTPResponse { +func APIError(g *gin.Context, errcode commonapierr.APIErrorCode, msg string, e error) HTTPResponse { return createApiError(g, errcode, msg, e) } func NotImplemented(g *gin.Context) HTTPResponse { - return createApiError(g, apierr.NotImplemented, "", nil) + return createApiError(g, commonapierr.NotImplemented, "", nil) } -func createApiError(g *gin.Context, errcode apierr.APIErrorCode, msg string, e error) HTTPResponse { +func createApiError(g *gin.Context, errcode commonapierr.APIErrorCode, msg string, e error) HTTPResponse { reqUri := "" if g != nil && g.Request != nil { reqUri = g.Request.Method + " :: " + g.Request.RequestURI diff --git a/goextVersion.go b/goextVersion.go index 04a725f..1901ec3 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.172" +const GoextVersion = "0.0.173" -const GoextVersionTimestamp = "2023-07-18T14:40:10+0200" +const GoextVersionTimestamp = "2023-07-18T15:12:06+0200"