|
|
|
@@ -5,6 +5,7 @@ import (
|
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/rext"
|
|
|
|
|
"net/http"
|
|
|
|
|
"path"
|
|
|
|
|
"reflect"
|
|
|
|
|
"regexp"
|
|
|
|
|
"runtime"
|
|
|
|
@@ -20,6 +21,7 @@ var anyMethods = []string{
|
|
|
|
|
type GinRoutesWrapper struct {
|
|
|
|
|
wrapper *GinWrapper
|
|
|
|
|
routes gin.IRouter
|
|
|
|
|
absPath string
|
|
|
|
|
defaultHandler []gin.HandlerFunc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -28,15 +30,26 @@ type GinRouteBuilder struct {
|
|
|
|
|
|
|
|
|
|
method string
|
|
|
|
|
relPath string
|
|
|
|
|
absPath string
|
|
|
|
|
handlers []gin.HandlerFunc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinWrapper) Routes() *GinRoutesWrapper {
|
|
|
|
|
return &GinRoutesWrapper{wrapper: w, routes: w.engine}
|
|
|
|
|
return &GinRoutesWrapper{
|
|
|
|
|
wrapper: w,
|
|
|
|
|
routes: w.engine,
|
|
|
|
|
absPath: "",
|
|
|
|
|
defaultHandler: make([]gin.HandlerFunc, 0),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) Group(relativePath string) *GinRoutesWrapper {
|
|
|
|
|
return &GinRoutesWrapper{wrapper: w.wrapper, routes: w.routes.Group(relativePath), defaultHandler: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
|
return &GinRoutesWrapper{
|
|
|
|
|
wrapper: w.wrapper,
|
|
|
|
|
routes: w.routes.Group(relativePath),
|
|
|
|
|
defaultHandler: langext.ArrCopy(w.defaultHandler),
|
|
|
|
|
absPath: joinPaths(w.absPath, relativePath),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) Use(middleware ...gin.HandlerFunc) *GinRoutesWrapper {
|
|
|
|
@@ -46,39 +59,49 @@ func (w *GinRoutesWrapper) Use(middleware ...gin.HandlerFunc) *GinRoutesWrapper
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
|
|
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodGet, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
|
return w._route(http.MethodGet, relativePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder {
|
|
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodPost, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
|
return w._route(http.MethodPost, relativePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder {
|
|
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodDelete, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
|
return w._route(http.MethodDelete, relativePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder {
|
|
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodPatch, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
|
return w._route(http.MethodPatch, relativePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder {
|
|
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodPut, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
|
return w._route(http.MethodPut, relativePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder {
|
|
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodOptions, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
|
return w._route(http.MethodOptions, relativePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder {
|
|
|
|
|
return &GinRouteBuilder{routes: w, method: http.MethodHead, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
|
return w._route(http.MethodHead, relativePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder {
|
|
|
|
|
return &GinRouteBuilder{routes: w, method: "COUNT", relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
|
return w._route("COUNT", relativePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) Any(relativePath string) *GinRouteBuilder {
|
|
|
|
|
return &GinRouteBuilder{routes: w, method: "*", relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)}
|
|
|
|
|
return w._route("*", relativePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRoutesWrapper) _route(method string, relativePath string) *GinRouteBuilder {
|
|
|
|
|
return &GinRouteBuilder{
|
|
|
|
|
routes: w,
|
|
|
|
|
method: method,
|
|
|
|
|
relPath: relativePath,
|
|
|
|
|
absPath: joinPaths(w.absPath, relativePath),
|
|
|
|
|
handlers: langext.ArrCopy(w.defaultHandler),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (w *GinRouteBuilder) Use(middleware ...gin.HandlerFunc) *GinRouteBuilder {
|
|
|
|
@@ -106,7 +129,7 @@ func (w *GinRouteBuilder) Handle(handler WHandlerFunc) {
|
|
|
|
|
|
|
|
|
|
w.routes.wrapper.routeSpecs = append(w.routes.wrapper.routeSpecs, ginRouteSpec{
|
|
|
|
|
Method: methodName,
|
|
|
|
|
URL: w.relPath,
|
|
|
|
|
URL: w.absPath,
|
|
|
|
|
Middlewares: middlewareNames,
|
|
|
|
|
Handler: handlerName,
|
|
|
|
|
})
|
|
|
|
@@ -147,3 +170,23 @@ func nameOfFunction(f any) string {
|
|
|
|
|
|
|
|
|
|
return fname
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// joinPaths is copied verbatim from gin@v1.9.1/gin.go
|
|
|
|
|
func joinPaths(absolutePath, relativePath string) string {
|
|
|
|
|
if relativePath == "" {
|
|
|
|
|
return absolutePath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finalPath := path.Join(absolutePath, relativePath)
|
|
|
|
|
if lastChar(relativePath) == '/' && lastChar(finalPath) != '/' {
|
|
|
|
|
return finalPath + "/"
|
|
|
|
|
}
|
|
|
|
|
return finalPath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func lastChar(str string) uint8 {
|
|
|
|
|
if str == "" {
|
|
|
|
|
panic("The length of the string can't be 0")
|
|
|
|
|
}
|
|
|
|
|
return str[len(str)-1]
|
|
|
|
|
}
|
|
|
|
|