Compare commits

...

3 Commits

Author SHA1 Message Date
ec62ad436f v0.0.176 2023-07-18 16:01:34 +02:00
8d0ef0f002 v0.0.175 2023-07-18 15:59:12 +02:00
d78550672e v0.0.174 2023-07-18 15:23:32 +02:00
9 changed files with 129 additions and 108 deletions

View File

@@ -10,7 +10,7 @@ type AppContext struct {
inner context.Context
cancelFunc context.CancelFunc
cancelled bool
ginContext *gin.Context
GinContext *gin.Context
}
func CreateAppContext(g *gin.Context, innerCtx context.Context, cancelFn context.CancelFunc) *AppContext {
@@ -18,7 +18,7 @@ func CreateAppContext(g *gin.Context, innerCtx context.Context, cancelFn context
inner: innerCtx,
cancelFunc: cancelFn,
cancelled: false,
ginContext: g,
GinContext: g,
}
}
@@ -44,8 +44,8 @@ func (ac *AppContext) Cancel() {
}
func (ac *AppContext) RequestURI() string {
if ac.ginContext != nil && ac.ginContext.Request != nil {
return ac.ginContext.Request.Method + " :: " + ac.ginContext.Request.RequestURI
if ac.GinContext != nil && ac.GinContext.Request != nil {
return ac.GinContext.Request.Method + " :: " + ac.GinContext.Request.RequestURI
} else {
return ""
}

View File

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

View File

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

View File

@@ -2,6 +2,7 @@ package ginext
import (
"github.com/gin-gonic/gin"
"net/http"
"time"
)
@@ -45,3 +46,7 @@ func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool, timeout time
return wrapper
}
func (w *GinWrapper) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
w.engine.ServeHTTP(writer, request)
}

View File

@@ -1,15 +1,10 @@
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
@@ -29,7 +24,7 @@ func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
Interface("panicObj", panicObj).
Str("trace", stackTrace).
Msg("Panic occured (in gin handler)")
wrap = APIError(g, commonapierr.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() {
@@ -41,84 +36,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
}

90
ginext/preContext.go Normal file
View File

@@ -0,0 +1,90 @@
package ginext
import (
"context"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"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
}

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr"
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug"
@@ -66,6 +65,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,15 +98,19 @@ func Download(mimetype string, filepath string, filename string) HTTPResponse {
return &fileHTTPResponse{mimetype: mimetype, filepath: filepath, filename: &filename}
}
func APIError(g *gin.Context, errcode commonapierr.APIErrorCode, msg string, e error) HTTPResponse {
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)
}
func NotImplemented(g *gin.Context) HTTPResponse {
return createApiError(g, commonapierr.NotImplemented, "", nil)
return createApiError(g, commonApiErr.NotImplemented, "", nil)
}
func createApiError(g *gin.Context, errcode commonapierr.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

View File

@@ -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...)

View File

@@ -1,5 +1,5 @@
package goext
const GoextVersion = "0.0.173"
const GoextVersion = "0.0.176"
const GoextVersionTimestamp = "2023-07-18T15:12:06+0200"
const GoextVersionTimestamp = "2023-07-18T16:01:34+0200"