Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
caa69c3629 | |||
0ff5f0aa28 | |||
d5cb1e48ed | |||
1c2d3f541f
|
|||
ec62ad436f
|
|||
8d0ef0f002
|
@@ -38,7 +38,11 @@ printf "package goext\n\nconst GoextVersion = \"%s\"\n\nconst GoextVersionTimest
|
||||
|
||||
git add --verbose .
|
||||
|
||||
msg="v${next_ver}"
|
||||
echo "Insert option commit message:" -n
|
||||
|
||||
read commitMessage
|
||||
|
||||
msg="v${next_ver} ${commitMessage}"
|
||||
|
||||
if [ $# -gt 0 ]; then
|
||||
msg="$1"
|
||||
|
@@ -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 ""
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
package commonapierr
|
||||
package commonApiErr
|
||||
|
||||
type APIErrorCode struct {
|
||||
HTTPStatusCode int
|
@@ -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)
|
||||
}
|
||||
|
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gogs.mikescher.com/BlackForestBytes/goext/ginext/commonapierr"
|
||||
)
|
||||
|
||||
type WHandlerFunc func(PreContext) HTTPResponse
|
||||
@@ -25,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() {
|
||||
|
@@ -4,7 +4,6 @@ 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"
|
||||
)
|
||||
@@ -41,33 +40,33 @@ func (pctx *PreContext) Form(form any) *PreContext {
|
||||
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))
|
||||
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))
|
||||
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))
|
||||
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))
|
||||
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))
|
||||
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))
|
||||
return nil, nil, langext.Ptr(APIError(pctx.ginCtx, commonApiErr.BindFailJSON, "missing form body", nil))
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -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"
|
||||
@@ -103,15 +102,15 @@ 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 {
|
||||
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
|
||||
|
@@ -5,6 +5,12 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var anyMethods = []string{
|
||||
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch,
|
||||
http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect,
|
||||
http.MethodTrace,
|
||||
}
|
||||
|
||||
type GinRoutesWrapper struct {
|
||||
wrapper *GinWrapper
|
||||
routes gin.IRouter
|
||||
@@ -13,7 +19,7 @@ type GinRoutesWrapper struct {
|
||||
type GinRouteBuilder struct {
|
||||
routes *GinRoutesWrapper
|
||||
|
||||
method string
|
||||
methods []string
|
||||
relPath string
|
||||
handlers []gin.HandlerFunc
|
||||
}
|
||||
@@ -27,43 +33,53 @@ func (w *GinRoutesWrapper) Group(relativePath string) *GinRoutesWrapper {
|
||||
}
|
||||
|
||||
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
|
||||
return &GinRouteBuilder{routes: w, method: http.MethodGet, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
return &GinRouteBuilder{routes: w, methods: []string{http.MethodGet}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
}
|
||||
|
||||
func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder {
|
||||
return &GinRouteBuilder{routes: w, method: http.MethodPost, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
return &GinRouteBuilder{routes: w, methods: []string{http.MethodPost}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
}
|
||||
|
||||
func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder {
|
||||
return &GinRouteBuilder{routes: w, method: http.MethodDelete, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
return &GinRouteBuilder{routes: w, methods: []string{http.MethodDelete}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
}
|
||||
|
||||
func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder {
|
||||
return &GinRouteBuilder{routes: w, method: http.MethodPatch, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
return &GinRouteBuilder{routes: w, methods: []string{http.MethodPatch}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
}
|
||||
|
||||
func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder {
|
||||
return &GinRouteBuilder{routes: w, method: http.MethodPut, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
return &GinRouteBuilder{routes: w, methods: []string{http.MethodPut}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
}
|
||||
|
||||
func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder {
|
||||
return &GinRouteBuilder{routes: w, method: http.MethodOptions, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
return &GinRouteBuilder{routes: w, methods: []string{http.MethodOptions}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
}
|
||||
|
||||
func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder {
|
||||
return &GinRouteBuilder{routes: w, method: http.MethodHead, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
return &GinRouteBuilder{routes: w, methods: []string{http.MethodHead}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
}
|
||||
|
||||
func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder {
|
||||
return &GinRouteBuilder{routes: w, method: "COUNT", relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
return &GinRouteBuilder{routes: w, methods: []string{"COUNT"}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
}
|
||||
|
||||
func (w *GinRouteBuilder) Use(middleware gin.HandlerFunc) *GinRouteBuilder {
|
||||
w.handlers = append(w.handlers, middleware)
|
||||
func (w *GinRoutesWrapper) Any(relativePath string) *GinRouteBuilder {
|
||||
return &GinRouteBuilder{routes: w, methods: anyMethods, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
|
||||
}
|
||||
|
||||
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...)
|
||||
for _, m := range w.methods {
|
||||
w.routes.routes.Handle(m, w.relPath, w.handlers...)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *GinWrapper) NoRoute(handler WHandlerFunc) {
|
||||
w.engine.NoRoute(Wrap(w, handler))
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
package goext
|
||||
|
||||
const GoextVersion = "0.0.174"
|
||||
const GoextVersion = "0.0.180"
|
||||
|
||||
const GoextVersionTimestamp = "2023-07-18T15:23:32+0200"
|
||||
const GoextVersionTimestamp = "2023-07-19T11:24:01+0200"
|
||||
|
Reference in New Issue
Block a user