diff --git a/ginext/routes.go b/ginext/routes.go index d925d3d..a698c63 100644 --- a/ginext/routes.go +++ b/ginext/routes.go @@ -2,6 +2,7 @@ package ginext import ( "github.com/gin-gonic/gin" + "gogs.mikescher.com/BlackForestBytes/goext/langext" "net/http" ) @@ -12,8 +13,9 @@ var anyMethods = []string{ } type GinRoutesWrapper struct { - wrapper *GinWrapper - routes gin.IRouter + wrapper *GinWrapper + routes gin.IRouter + defaultHandler []gin.HandlerFunc } type GinRouteBuilder struct { @@ -29,43 +31,49 @@ func (w *GinWrapper) Routes() *GinRoutesWrapper { } func (w *GinRoutesWrapper) Group(relativePath string) *GinRoutesWrapper { - return &GinRoutesWrapper{wrapper: w.wrapper, routes: w.routes.Group(relativePath)} + 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} } func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder { - return &GinRouteBuilder{routes: w, methods: []string{http.MethodGet}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} + return &GinRouteBuilder{routes: w, methods: []string{http.MethodGet}, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder { - return &GinRouteBuilder{routes: w, methods: []string{http.MethodPost}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} + return &GinRouteBuilder{routes: w, methods: []string{http.MethodPost}, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder { - return &GinRouteBuilder{routes: w, methods: []string{http.MethodDelete}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} + return &GinRouteBuilder{routes: w, methods: []string{http.MethodDelete}, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder { - return &GinRouteBuilder{routes: w, methods: []string{http.MethodPatch}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} + return &GinRouteBuilder{routes: w, methods: []string{http.MethodPatch}, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder { - return &GinRouteBuilder{routes: w, methods: []string{http.MethodPut}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} + return &GinRouteBuilder{routes: w, methods: []string{http.MethodPut}, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder { - return &GinRouteBuilder{routes: w, methods: []string{http.MethodOptions}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} + return &GinRouteBuilder{routes: w, methods: []string{http.MethodOptions}, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder { - return &GinRouteBuilder{routes: w, methods: []string{http.MethodHead}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} + return &GinRouteBuilder{routes: w, methods: []string{http.MethodHead}, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder { - return &GinRouteBuilder{routes: w, methods: []string{"COUNT"}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} + return &GinRouteBuilder{routes: w, methods: []string{"COUNT"}, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) Any(relativePath string) *GinRouteBuilder { - return &GinRouteBuilder{routes: w, methods: anyMethods, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} + return &GinRouteBuilder{routes: w, methods: anyMethods, relPath: relativePath, handlers: langext.ArrCopy(w.defaultHandler)} } func (w *GinRouteBuilder) Use(middleware ...gin.HandlerFunc) *GinRouteBuilder { diff --git a/goextVersion.go b/goextVersion.go index b2b6a36..22b3aa6 100644 --- a/goextVersion.go +++ b/goextVersion.go @@ -1,5 +1,5 @@ package goext -const GoextVersion = "0.0.198" +const GoextVersion = "0.0.199" -const GoextVersionTimestamp = "2023-07-24T14:16:02+0200" +const GoextVersionTimestamp = "2023-07-24T17:23:38+0200"