From d78550672e60c00fc50013b57f0391c5182579dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Tue, 18 Jul 2023 15:23:32 +0200 Subject: [PATCH] v0.0.174 --- ginext/commonHandler.go | 19 ++++--- ginext/funcWrapper.go | 85 ------------------------------ ginext/preContext.go | 91 +++++++++++++++++++++++++++++++++ ginext/{resp.go => response.go} | 13 +++++ ginext/routes.go | 1 + goextVersion.go | 4 +- 6 files changed, 116 insertions(+), 97 deletions(-) create mode 100644 ginext/preContext.go rename ginext/{resp.go => response.go} (92%) diff --git a/ginext/commonHandler.go b/ginext/commonHandler.go index 44662e6..26d2507 100644 --- a/ginext/commonHandler.go +++ b/ginext/commonHandler.go @@ -1,24 +1,23 @@ package ginext import ( - "github.com/gin-gonic/gin" "net/http" ) -func RedirectFound(newuri string) gin.HandlerFunc { - return func(g *gin.Context) { - g.Redirect(http.StatusFound, newuri) +func RedirectFound(newuri string) WHandlerFunc { + return func(pctx PreContext) HTTPResponse { + return Redirect(http.StatusFound, newuri) } } -func RedirectTemporary(newuri string) gin.HandlerFunc { - return func(g *gin.Context) { - g.Redirect(http.StatusTemporaryRedirect, newuri) +func RedirectTemporary(newuri string) WHandlerFunc { + return func(pctx PreContext) HTTPResponse { + return Redirect(http.StatusTemporaryRedirect, newuri) } } -func RedirectPermanent(newuri string) gin.HandlerFunc { - return func(g *gin.Context) { - g.Redirect(http.StatusPermanentRedirect, newuri) +func RedirectPermanent(newuri string) WHandlerFunc { + return func(pctx PreContext) HTTPResponse { + return Redirect(http.StatusPermanentRedirect, newuri) } } diff --git a/ginext/funcWrapper.go b/ginext/funcWrapper.go index 861fd61..48238e0 100644 --- a/ginext/funcWrapper.go +++ b/ginext/funcWrapper.go @@ -1,15 +1,11 @@ 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/commonapierr" - "gogs.mikescher.com/BlackForestBytes/goext/langext" - "runtime/debug" ) type WHandlerFunc func(PreContext) HTTPResponse @@ -41,84 +37,3 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc { } } } - -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 - stackTrace = string(debug.Stack()) - panicObj = rec - } - }() - - res = fn(pctx) - return res, "", nil -} diff --git a/ginext/preContext.go b/ginext/preContext.go new file mode 100644 index 0000000..36d7dc5 --- /dev/null +++ b/ginext/preContext.go @@ -0,0 +1,91 @@ +package ginext + +import ( + "context" + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" + "gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr" + "gogs.mikescher.com/BlackForestBytes/goext/langext" + "runtime/debug" +) + +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, *gin.Context, *HTTPResponse) { + if pctx.uri != nil { + if err := pctx.ginCtx.ShouldBindUri(pctx.uri); err != nil { + return nil, 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, 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, nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailJSON, "Failed to read body", err)) + } + } else { + return nil, 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, nil, langext.Ptr(APIError(pctx.ginCtx, commonapierr.BindFailFormData, "Failed to read multipart-form", err)) + } + } else { + return nil, 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, pctx.ginCtx, nil +} + +func callPanicSafe(fn WHandlerFunc, pctx PreContext) (res HTTPResponse, stackTrace string, panicObj any) { + defer func() { + if rec := recover(); rec != nil { + res = nil + stackTrace = string(debug.Stack()) + panicObj = rec + } + }() + + res = fn(pctx) + return res, "", nil +} diff --git a/ginext/resp.go b/ginext/response.go similarity index 92% rename from ginext/resp.go rename to ginext/response.go index 7434029..daf72c7 100644 --- a/ginext/resp.go +++ b/ginext/response.go @@ -66,6 +66,15 @@ func (j fileHTTPResponse) Write(g *gin.Context) { g.File(j.filepath) } +type redirectHTTPResponse struct { + statusCode int + url string +} + +func (j redirectHTTPResponse) Write(g *gin.Context) { + g.Redirect(j.statusCode, j.url) +} + func Status(sc int) HTTPResponse { return &emptyHTTPResponse{statusCode: sc} } @@ -90,6 +99,10 @@ func Download(mimetype string, filepath string, filename string) HTTPResponse { return &fileHTTPResponse{mimetype: mimetype, filepath: filepath, filename: &filename} } +func Redirect(sc int, newURL string) HTTPResponse { + return &redirectHTTPResponse{statusCode: sc, url: newURL} +} + func APIError(g *gin.Context, errcode commonapierr.APIErrorCode, msg string, e error) HTTPResponse { return createApiError(g, errcode, msg, e) } diff --git a/ginext/routes.go b/ginext/routes.go index ce240fd..4a956e4 100644 --- a/ginext/routes.go +++ b/ginext/routes.go @@ -62,6 +62,7 @@ func (w *GinRouteBuilder) Use(middleware gin.HandlerFunc) *GinRouteBuilder { w.handlers = append(w.handlers, middleware) return w } + func (w *GinRouteBuilder) Handle(handler WHandlerFunc) { w.handlers = append(w.handlers, Wrap(w.routes.wrapper, handler)) w.routes.routes.Handle(w.method, w.relPath, w.handlers...) diff --git a/goextVersion.go b/goextVersion.go index 1901ec3..db67f5b 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.173" +const GoextVersion = "0.0.174" -const GoextVersionTimestamp = "2023-07-18T15:12:06+0200" +const GoextVersionTimestamp = "2023-07-18T15:23:32+0200"