v0.0.174
This commit is contained in:
parent
1d629f6db8
commit
d78550672e
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
91
ginext/preContext.go
Normal file
91
ginext/preContext.go
Normal file
@ -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
|
||||
}
|
@ -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)
|
||||
}
|
@ -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...)
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user