This commit is contained in:
2023-07-18 14:40:10 +02:00
parent 710c257c64
commit f7d291056d
11 changed files with 513 additions and 2 deletions

16
ginext/apiError.go Normal file
View File

@@ -0,0 +1,16 @@
package ginext
type apiError struct {
ErrorCode string `json:"errorcode"`
Message string `json:"message"`
FAPIErrorMessage *string `json:"fapiMessage,omitempty"`
}
type extAPIError struct {
ErrorCode string `json:"errorcode"`
Message string `json:"message"`
FAPIErrorMessage *string `json:"fapiMessage,omitempty"`
RawError *string `json:"__error"`
Trace []string `json:"__trace"`
}

21
ginext/apierr/enums.go Normal file
View File

@@ -0,0 +1,21 @@
package apierr
type APIErrorCode struct {
HTTPStatusCode int
Key string
}
//goland:noinspection GoSnakeCaseUsage
var (
NotImplemented = APIErrorCode{500, "NOT_IMPLEMENTED"}
InternalError = APIErrorCode{500, "INTERNAL_ERROR"}
Panic = APIErrorCode{500, "PANIC"}
BindFailURI = APIErrorCode{400, "BINDFAIL_URI"}
BindFailQuery = APIErrorCode{400, "BINDFAIL_QUERY"}
BindFailJSON = APIErrorCode{400, "BINDFAIL_JSON"}
BindFailFormData = APIErrorCode{400, "BINDFAIL_FORMDATA"}
Unauthorized = APIErrorCode{401, "UNAUTHORIZED"}
AuthFailed = APIErrorCode{401, "AUTH_FAILED"}
)

24
ginext/commonHandler.go Normal file
View File

@@ -0,0 +1,24 @@
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 RedirectTemporary(newuri string) gin.HandlerFunc {
return func(g *gin.Context) {
g.Redirect(http.StatusTemporaryRedirect, newuri)
}
}
func RedirectPermanent(newuri string) gin.HandlerFunc {
return func(g *gin.Context) {
g.Redirect(http.StatusPermanentRedirect, newuri)
}
}

21
ginext/cors.go Normal file
View File

@@ -0,0 +1,21 @@
package ginext
import (
"github.com/gin-gonic/gin"
"net/http"
)
func CorsMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, PATCH, DELETE, COUNT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusOK)
} else {
c.Next()
}
}
}

42
ginext/engine.go Normal file
View File

@@ -0,0 +1,42 @@
package ginext
import "github.com/gin-gonic/gin"
type GinWrapper struct {
engine *gin.Engine
SuppressGinLogs bool
allowCors bool
ginDebug bool
returnRawErrors bool
}
func NewEngine(allowCors bool, ginDebug bool, returnRawErrors bool) *GinWrapper {
engine := gin.New()
wrapper := &GinWrapper{
engine: engine,
SuppressGinLogs: false,
allowCors: allowCors,
ginDebug: ginDebug,
returnRawErrors: returnRawErrors,
}
engine.RedirectFixedPath = false
engine.RedirectTrailingSlash = false
if allowCors {
engine.Use(CorsMiddleware())
}
if ginDebug {
ginlogger := gin.Logger()
engine.Use(func(context *gin.Context) {
if !wrapper.SuppressGinLogs {
ginlogger(context)
}
})
}
return wrapper
}

53
ginext/funcWrapper.go Normal file
View File

@@ -0,0 +1,53 @@
package ginext
import (
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext/apierr"
"runtime/debug"
)
type WHandlerFunc func(*gin.Context) HTTPResponse
func Wrap(w *GinWrapper, fn WHandlerFunc) gin.HandlerFunc {
return func(g *gin.Context) {
g.Set("__returnRawErrors", w.returnRawErrors)
reqctx := g.Request.Context()
wrap, stackTrace, panicObj := callPanicSafe(fn, g)
if panicObj != nil {
fmt.Printf("\n======== ======== STACKTRACE ======== ========\n%s\n======== ======== ======== ========\n\n", stackTrace)
log.Error().
Interface("panicObj", panicObj).
Str("trace", stackTrace).
Msg("Panic occured (in gin handler)")
wrap = APIError(g, apierr.Panic, "A panic occured in the HTTP handler", errors.New(fmt.Sprintf("%+v", panicObj)))
}
if g.Writer.Written() {
panic("Writing in WrapperFunc is not supported")
}
if reqctx.Err() == nil {
wrap.Write(g)
}
}
}
func callPanicSafe(fn WHandlerFunc, g *gin.Context) (res HTTPResponse, stackTrace string, panicObj any) {
defer func() {
if rec := recover(); rec != nil {
res = nil
stackTrace = string(debug.Stack())
panicObj = rec
}
}()
res = fn(g)
return res, "", nil
}

141
ginext/resp.go Normal file
View File

@@ -0,0 +1,141 @@
package ginext
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/ginext/apierr"
json "gogs.mikescher.com/BlackForestBytes/goext/gojson"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"runtime/debug"
"strings"
)
type HTTPResponse interface {
Write(g *gin.Context)
}
type jsonHTTPResponse struct {
statusCode int
data any
}
func (j jsonHTTPResponse) Write(g *gin.Context) {
g.Render(j.statusCode, json.GoJsonRender{Data: j.data, NilSafeSlices: true, NilSafeMaps: true})
}
type emptyHTTPResponse struct {
statusCode int
}
func (j emptyHTTPResponse) Write(g *gin.Context) {
g.Status(j.statusCode)
}
type textHTTPResponse struct {
statusCode int
data string
}
func (j textHTTPResponse) Write(g *gin.Context) {
g.String(j.statusCode, "%s", j.data)
}
type dataHTTPResponse struct {
statusCode int
data []byte
contentType string
}
func (j dataHTTPResponse) Write(g *gin.Context) {
g.Data(j.statusCode, j.contentType, j.data)
}
type fileHTTPResponse struct {
mimetype string
filepath string
filename *string
}
func (j fileHTTPResponse) Write(g *gin.Context) {
g.Header("Content-Type", j.mimetype) // if we don't set it here gin does weird file-sniffing later...
if j.filename != nil {
g.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", *j.filename))
}
g.File(j.filepath)
}
func Status(sc int) HTTPResponse {
return &emptyHTTPResponse{statusCode: sc}
}
func JSON(sc int, data any) HTTPResponse {
return &jsonHTTPResponse{statusCode: sc, data: data}
}
func Data(sc int, contentType string, data []byte) HTTPResponse {
return &dataHTTPResponse{statusCode: sc, contentType: contentType, data: data}
}
func Text(sc int, data string) HTTPResponse {
return &textHTTPResponse{statusCode: sc, data: data}
}
func File(mimetype string, filepath string) HTTPResponse {
return &fileHTTPResponse{mimetype: mimetype, filepath: filepath}
}
func Download(mimetype string, filepath string, filename string) HTTPResponse {
return &fileHTTPResponse{mimetype: mimetype, filepath: filepath, filename: &filename}
}
func APIError(g *gin.Context, errcode apierr.APIErrorCode, msg string, e error) HTTPResponse {
return createApiError(g, errcode, msg, e)
}
func NotImplemented(g *gin.Context) HTTPResponse {
return createApiError(g, apierr.NotImplemented, "", nil)
}
func createApiError(g *gin.Context, errcode apierr.APIErrorCode, msg string, e error) HTTPResponse {
reqUri := ""
if g != nil && g.Request != nil {
reqUri = g.Request.Method + " :: " + g.Request.RequestURI
}
log.Error().
Str("errorcode.key", errcode.Key).
Int("errcode.status", errcode.HTTPStatusCode).
Str("uri", reqUri).
AnErr("err", e).
Stack().
Msg(msg)
var fapiMessage *string = nil
if v, ok := e.(interface{ FAPIMessage() string }); ok {
fapiMessage = langext.Ptr(v.FAPIMessage())
}
if g.GetBool("__returnRawErrors") {
return &jsonHTTPResponse{
statusCode: errcode.HTTPStatusCode,
data: extAPIError{
ErrorCode: errcode.Key,
Message: msg,
RawError: langext.Ptr(langext.Conditional(e == nil, "", fmt.Sprintf("%+v", e))),
FAPIErrorMessage: fapiMessage,
Trace: strings.Split(string(debug.Stack()), "\n"),
},
}
} else {
return &jsonHTTPResponse{
statusCode: errcode.HTTPStatusCode,
data: apiError{
ErrorCode: errcode.Key,
Message: msg,
FAPIErrorMessage: fapiMessage,
},
}
}
}

68
ginext/routes.go Normal file
View File

@@ -0,0 +1,68 @@
package ginext
import (
"github.com/gin-gonic/gin"
"net/http"
)
type GinRoutesWrapper struct {
wrapper *GinWrapper
routes gin.IRouter
}
type GinRouteBuilder struct {
routes *GinRoutesWrapper
method string
relPath string
handlers []gin.HandlerFunc
}
func (w *GinWrapper) Routes() *GinRoutesWrapper {
return &GinRoutesWrapper{wrapper: w, routes: w.engine}
}
func (w *GinRoutesWrapper) Group(relativePath string) *GinRoutesWrapper {
return &GinRoutesWrapper{wrapper: w.wrapper, routes: w.routes.Group(relativePath)}
}
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: 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)}
}
func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: 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)}
}
func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: 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)}
}
func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: 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)}
}
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...)
}