diff --git a/ginext/routes.go b/ginext/routes.go index 6a02e7a..5d0e2c3 100644 --- a/ginext/routes.go +++ b/ginext/routes.go @@ -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 { @@ -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] +} diff --git a/goextVersion.go b/goextVersion.go index 1ada67c..5b53c84 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.209" +const GoextVersion = "0.0.210" -const GoextVersionTimestamp = "2023-07-25T10:56:03+0200" +const GoextVersionTimestamp = "2023-07-25T11:16:11+0200"