2023-07-18 14:40:10 +02:00
|
|
|
package ginext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-07-24 17:23:38 +02:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
2023-07-18 14:40:10 +02:00
|
|
|
"net/http"
|
2023-07-24 18:22:36 +02:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2023-07-18 14:40:10 +02:00
|
|
|
)
|
|
|
|
|
2023-07-18 16:08:24 +02:00
|
|
|
var anyMethods = []string{
|
|
|
|
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch,
|
|
|
|
http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect,
|
|
|
|
http.MethodTrace,
|
|
|
|
}
|
|
|
|
|
2023-07-18 14:40:10 +02:00
|
|
|
type GinRoutesWrapper struct {
|
2023-07-24 17:23:38 +02:00
|
|
|
wrapper *GinWrapper
|
|
|
|
routes gin.IRouter
|
|
|
|
defaultHandler []gin.HandlerFunc
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type GinRouteBuilder struct {
|
|
|
|
routes *GinRoutesWrapper
|
|
|
|
|
2023-07-24 18:22:36 +02:00
|
|
|
method string
|
2023-07-18 14:40:10 +02:00
|
|
|
relPath string
|
|
|
|
handlers []gin.HandlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinWrapper) Routes() *GinRoutesWrapper {
|
|
|
|
return &GinRoutesWrapper{wrapper: w, routes: w.engine}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) Group(relativePath string) *GinRoutesWrapper {
|
2023-07-24 17:23:38 +02:00
|
|
|
return &GinRoutesWrapper{wrapper: w.wrapper, routes: w.routes.Group(relativePath), defaultHandler: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) Use(middleware ...gin.HandlerFunc) *GinRoutesWrapper {
|
|
|
|
defHandler := langext.ArrCopy(w.defaultHandler)
|
|
|
|
defHandler = append(defHandler, middleware...)
|
|
|
|
return &GinRoutesWrapper{wrapper: w.wrapper, routes: w.routes, defaultHandler: defHandler}
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
|
2023-07-24 18:22:36 +02:00
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodGet, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder {
|
2023-07-24 18:22:36 +02:00
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodPost, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder {
|
2023-07-24 18:22:36 +02:00
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodDelete, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder {
|
2023-07-24 18:22:36 +02:00
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodPatch, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder {
|
2023-07-24 18:22:36 +02:00
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodPut, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder {
|
2023-07-24 18:22:36 +02:00
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodOptions, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder {
|
2023-07-24 18:22:36 +02:00
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodHead, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder {
|
2023-07-24 18:22:36 +02:00
|
|
|
return &GinRouteBuilder{routes: w, method: "COUNT", relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
2023-07-18 16:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) Any(relativePath string) *GinRouteBuilder {
|
2023-07-24 18:22:36 +02:00
|
|
|
return &GinRouteBuilder{routes: w, method: "*", relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|
|
|
|
|
2023-07-19 11:20:35 +02:00
|
|
|
func (w *GinRouteBuilder) Use(middleware ...gin.HandlerFunc) *GinRouteBuilder {
|
|
|
|
w.handlers = append(w.handlers, middleware...)
|
2023-07-18 14:40:10 +02:00
|
|
|
return w
|
|
|
|
}
|
2023-07-18 15:23:32 +02:00
|
|
|
|
2023-07-18 14:40:10 +02:00
|
|
|
func (w *GinRouteBuilder) Handle(handler WHandlerFunc) {
|
2023-07-24 18:22:36 +02:00
|
|
|
|
|
|
|
middlewareNames := langext.ArrMap(w.handlers, func(v gin.HandlerFunc) string { return nameOfFunction(v) })
|
|
|
|
handlerName := nameOfFunction(handler)
|
|
|
|
|
2023-07-18 14:40:10 +02:00
|
|
|
w.handlers = append(w.handlers, Wrap(w.routes.wrapper, handler))
|
2023-07-24 18:22:36 +02:00
|
|
|
|
|
|
|
methodName := w.method
|
|
|
|
|
|
|
|
if w.method == "*" {
|
|
|
|
methodName = "ANY"
|
|
|
|
for _, method := range anyMethods {
|
|
|
|
w.routes.routes.Handle(method, w.relPath, w.handlers...)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
w.routes.routes.Handle(w.method, w.relPath, w.handlers...)
|
2023-07-18 16:08:24 +02:00
|
|
|
}
|
2023-07-24 18:22:36 +02:00
|
|
|
|
|
|
|
w.routes.wrapper.routeSpecs = append(w.routes.wrapper.routeSpecs, ginRouteSpec{
|
|
|
|
Method: methodName,
|
|
|
|
URL: w.relPath,
|
|
|
|
Middlewares: middlewareNames,
|
|
|
|
Handler: handlerName,
|
|
|
|
})
|
2023-07-18 16:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *GinWrapper) NoRoute(handler WHandlerFunc) {
|
|
|
|
w.engine.NoRoute(Wrap(w, handler))
|
2023-07-24 18:22:36 +02:00
|
|
|
|
|
|
|
w.routeSpecs = append(w.routeSpecs, ginRouteSpec{
|
|
|
|
Method: "ANY",
|
|
|
|
URL: "[NO_ROUTE]",
|
|
|
|
Middlewares: nil,
|
|
|
|
Handler: nameOfFunction(handler),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func nameOfFunction(f any) string {
|
|
|
|
|
|
|
|
n := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
|
|
|
|
|
|
|
|
split := strings.Split(n, "/")
|
|
|
|
if len(split) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return split[len(split)-1]
|
2023-07-18 14:40:10 +02:00
|
|
|
}
|