21
0
Fork 0

remove ginext/mongoext (no-dep lib)

This commit is contained in:
Mike Schwörer 2022-10-27 16:07:42 +02:00
parent 568d7bd5e3
commit 47f123b86f
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
31 changed files with 6 additions and 123 deletions

View File

View File

@ -1,3 +0,0 @@
module blackforestbytes.com/goext/error
go 1.19

View File

@ -1,69 +0,0 @@
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
}

View File

@ -1,42 +0,0 @@
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)
}
}

View File

@ -1,3 +0,0 @@
module blackforestbytes.com/goext/gin
go 1.19

View File

View File

@ -1,6 +0,0 @@
module blackforestbytes.com/goext/mongo
require (
go.mongodb.org/mongo-driver v1.5.3
)
go 1.19

View File

View File

@ -2,6 +2,7 @@ package timeext
import (
"fmt"
"math"
"time"
)
@ -125,3 +126,8 @@ func Max(a time.Time, b time.Time) time.Time {
return b
}
}
func UnixFloatSeconds(v float64) time.Time {
sec, dec := math.Modf(v)
return time.Unix(int64(sec), int64(dec*(1e9)))
}