copy langext & timeext from bm
This commit is contained in:
69
gin/ginext/bindings.go
Normal file
69
gin/ginext/bindings.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package ginext
|
||||
|
||||
import (
|
||||
"bringman.de/common/shared/bmerror"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func ShouldBind(g *gin.Context, uri interface{}, query interface{}, body interface{}) error {
|
||||
if uri != nil {
|
||||
if err := g.ShouldBindUri(uri); err != nil {
|
||||
if vErrs, ok := err.(validator.ValidationErrors); ok {
|
||||
return bmerror.Wrap(vErrs, "Could not validate request parameter (uri)").
|
||||
Errs("inner", convertValidationErrors(vErrs)).
|
||||
WithType(bmerror.ErrQueryValidation).
|
||||
WithStatuscode(http.StatusBadRequest).
|
||||
Build()
|
||||
} else {
|
||||
return bmerror.Wrap(err, "Could not parse request parameter (uri)").
|
||||
WithType(bmerror.ErrQueryParse).
|
||||
WithStatuscode(http.StatusBadRequest).
|
||||
Build()
|
||||
}
|
||||
}
|
||||
}
|
||||
if query != nil {
|
||||
if err := g.ShouldBindQuery(query); err != nil {
|
||||
if vErrs, ok := err.(validator.ValidationErrors); ok {
|
||||
return bmerror.Wrap(vErrs, "Could not validate request parameter (query)").
|
||||
Errs("inner", convertValidationErrors(vErrs)).
|
||||
WithType(bmerror.ErrQueryValidation).
|
||||
WithStatuscode(http.StatusBadRequest).
|
||||
Build()
|
||||
} else {
|
||||
return bmerror.Wrap(err, "Could not parse request parameter (query)").
|
||||
WithType(bmerror.ErrQueryParse).
|
||||
WithStatuscode(http.StatusBadRequest).
|
||||
Build()
|
||||
}
|
||||
}
|
||||
}
|
||||
if body != nil {
|
||||
if err := g.ShouldBindJSON(body); err != nil {
|
||||
if vErrs, ok := err.(validator.ValidationErrors); ok {
|
||||
return bmerror.Wrap(vErrs, "Could not validate request parameter (body:json)").
|
||||
Errs("inner", convertValidationErrors(vErrs)).
|
||||
WithType(bmerror.ErrQueryValidation).
|
||||
WithStatuscode(http.StatusBadRequest).
|
||||
Build()
|
||||
} else {
|
||||
return bmerror.Wrap(err, "Could not parse request parameter (body:json)").
|
||||
WithType(bmerror.ErrQueryParse).
|
||||
WithStatuscode(http.StatusBadRequest).
|
||||
Build()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertValidationErrors(e validator.ValidationErrors) []error {
|
||||
r := make([]error, 0, len(e))
|
||||
for _, v := range e {
|
||||
r = append(r, v)
|
||||
}
|
||||
return r
|
||||
}
|
||||
42
gin/ginext/gin.go
Normal file
42
gin/ginext/gin.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package ginext
|
||||
|
||||
import (
|
||||
"bringman.de/common/shared/bmerror"
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func NewEngine() *gin.Engine {
|
||||
engine := gin.New()
|
||||
|
||||
engine.RedirectFixedPath = false
|
||||
engine.RedirectTrailingSlash = false
|
||||
|
||||
engine.Use(gin.CustomRecovery(func(c *gin.Context, err interface{}) {
|
||||
ctx := context.Background()
|
||||
|
||||
bmerror.
|
||||
New(bmerror.ErrGinPanic, "gin request caused panic").
|
||||
Interface("panic-object", err).
|
||||
Stack().
|
||||
GinReq(ctx, c, c.Request).
|
||||
WithStatuscode(http.StatusInternalServerError).
|
||||
Output(ctx, c)
|
||||
}))
|
||||
|
||||
return engine
|
||||
}
|
||||
|
||||
func NoRouteHandler() func(c *gin.Context) {
|
||||
return func(g *gin.Context) {
|
||||
bmerror.New(bmerror.ErrRouteNotFound, "Route not found").
|
||||
Str("FullPath", g.FullPath()).
|
||||
Str("Method", g.Request.Method).
|
||||
Str("URL", g.Request.URL.String()).
|
||||
Str("RequestURI", g.Request.RequestURI).
|
||||
Str("Proto", g.Request.Proto).
|
||||
Any("Header", g.Request.Header).
|
||||
Output(context.Background(), g)
|
||||
}
|
||||
}
|
||||
3
gin/go.mod
Normal file
3
gin/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module blackforestbytes.com/goext/gin
|
||||
|
||||
go 1.19
|
||||
Reference in New Issue
Block a user