This commit is contained in:
Mike Schwörer 2023-07-18 15:12:06 +02:00
parent f7d291056d
commit 1d629f6db8
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
6 changed files with 150 additions and 15 deletions

59
ginext/appcontext.go Normal file
View File

@ -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
}

View File

@ -1,4 +1,4 @@
package apierr package commonapierr
type APIErrorCode struct { type APIErrorCode struct {
HTTPStatusCode int HTTPStatusCode int

View File

@ -1,6 +1,9 @@
package ginext package ginext
import "github.com/gin-gonic/gin" import (
"github.com/gin-gonic/gin"
"time"
)
type GinWrapper struct { type GinWrapper struct {
engine *gin.Engine engine *gin.Engine
@ -9,9 +12,10 @@ type GinWrapper struct {
allowCors bool allowCors bool
ginDebug bool ginDebug bool
returnRawErrors 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() engine := gin.New()
wrapper := &GinWrapper{ wrapper := &GinWrapper{
@ -20,6 +24,7 @@ func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool) *GinWrapper
allowCors: allowCors, allowCors: allowCors,
ginDebug: ginDebug, ginDebug: ginDebug,
returnRawErrors: returnRawErrors, returnRawErrors: returnRawErrors,
requestTimeout: timeout,
} }
engine.RedirectFixedPath = false engine.RedirectFixedPath = false

View File

@ -1,15 +1,18 @@
package ginext package ginext
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/rs/zerolog/log" "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" "runtime/debug"
) )
type WHandlerFunc func(*gin.Context) HTTPResponse type WHandlerFunc func(PreContext) HTTPResponse
func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc { func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
@ -19,14 +22,14 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
reqctx := g.Request.Context() reqctx := g.Request.Context()
wrap, stackTrace, panicObj := callPanicSafe(fn, g) wrap, stackTrace, panicObj := callPanicSafe(fn, PreContext{wrapper: w, ginCtx: g})
if panicObj != nil { if panicObj != nil {
fmt.Printf("\n======== ======== STACKTRACE ======== ========\n%s\n======== ======== ======== ========\n\n", stackTrace) fmt.Printf("\n======== ======== STACKTRACE ======== ========\n%s\n======== ======== ======== ========\n\n", stackTrace)
log.Error(). log.Error().
Interface("panicObj", panicObj). Interface("panicObj", panicObj).
Str("trace", stackTrace). Str("trace", stackTrace).
Msg("Panic occured (in gin handler)") 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() { 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() { defer func() {
if rec := recover(); rec != nil { if rec := recover(); rec != nil {
res = 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 return res, "", nil
} }

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rs/zerolog/log" "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" json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug" "runtime/debug"
@ -90,15 +90,15 @@ func Download(mimetype string, filepath string, filename string) HTTPResponse {
return &fileHTTPResponse{mimetype: mimetype, filepath: filepath, filename: &filename} 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) return createApiError(g, errcode, msg, e)
} }
func NotImplemented(g *gin.Context) HTTPResponse { 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 := "" reqUri := ""
if g != nil && g.Request != nil { if g != nil && g.Request != nil {
reqUri = g.Request.Method + " :: " + g.Request.RequestURI reqUri = g.Request.Method + " :: " + g.Request.RequestURI

View File

@ -1,5 +1,5 @@
package goext 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"