package ginext import ( "github.com/gin-gonic/gin" "gogs.mikescher.com/BlackForestBytes/goext/langext" "net/http" ) var anyMethods = []string{ http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect, http.MethodTrace, } type GinRoutesWrapper struct { wrapper *GinWrapper routes gin.IRouter defaultHandler []gin.HandlerFunc } type GinRouteBuilder struct { routes *GinRoutesWrapper methods []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), 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: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder { 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: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder { 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: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder { 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: langext.ArrCopy(w.defaultHandler)} } func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder { 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: langext.ArrCopy(w.defaultHandler)} } 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)) for _, m := range w.methods { w.routes.routes.Handle(m, w.relPath, w.handlers...) } } func (w *GinWrapper) NoRoute(handler WHandlerFunc) { w.engine.NoRoute(Wrap(w, handler)) }