This commit is contained in:
Mike Schwörer 2023-07-18 16:08:24 +02:00
parent ec62ad436f
commit 1c2d3f541f
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 28 additions and 12 deletions

View File

@ -5,6 +5,12 @@ import (
"net/http" "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 { type GinRoutesWrapper struct {
wrapper *GinWrapper wrapper *GinWrapper
routes gin.IRouter routes gin.IRouter
@ -13,7 +19,7 @@ type GinRoutesWrapper struct {
type GinRouteBuilder struct { type GinRouteBuilder struct {
routes *GinRoutesWrapper routes *GinRoutesWrapper
method string methods []string
relPath string relPath string
handlers []gin.HandlerFunc handlers []gin.HandlerFunc
} }
@ -27,35 +33,39 @@ func (w *GinRoutesWrapper) Group(relativePath string) *GinRoutesWrapper {
} }
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodGet, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return &GinRouteBuilder{routes: w, methods: []string{http.MethodGet}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
} }
func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodPost, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return &GinRouteBuilder{routes: w, methods: []string{http.MethodPost}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
} }
func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodDelete, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return &GinRouteBuilder{routes: w, methods: []string{http.MethodDelete}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
} }
func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodPatch, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return &GinRouteBuilder{routes: w, methods: []string{http.MethodPatch}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
} }
func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodPut, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return &GinRouteBuilder{routes: w, methods: []string{http.MethodPut}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
} }
func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodOptions, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return &GinRouteBuilder{routes: w, methods: []string{http.MethodOptions}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
} }
func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodHead, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return &GinRouteBuilder{routes: w, methods: []string{http.MethodHead}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
} }
func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder { func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: "COUNT", relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)} return &GinRouteBuilder{routes: w, methods: []string{"COUNT"}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
}
func (w *GinRoutesWrapper) Any(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, methods: anyMethods, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
} }
func (w *GinRouteBuilder) Use(middleware gin.HandlerFunc) *GinRouteBuilder { func (w *GinRouteBuilder) Use(middleware gin.HandlerFunc) *GinRouteBuilder {
@ -65,5 +75,11 @@ func (w *GinRouteBuilder) Use(middleware gin.HandlerFunc) *GinRouteBuilder {
func (w *GinRouteBuilder) Handle(handler WHandlerFunc) { func (w *GinRouteBuilder) Handle(handler WHandlerFunc) {
w.handlers = append(w.handlers, Wrap(w.routes.wrapper, handler)) w.handlers = append(w.handlers, Wrap(w.routes.wrapper, handler))
w.routes.routes.Handle(w.method, w.relPath, w.handlers...) 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))
} }

View File

@ -1,5 +1,5 @@
package goext package goext
const GoextVersion = "0.0.176" const GoextVersion = "0.0.177"
const GoextVersionTimestamp = "2023-07-18T16:01:34+0200" const GoextVersionTimestamp = "2023-07-18T16:08:24+0200"