goext/ginext/routes.go

86 lines
3.0 KiB
Go
Raw Normal View History

2023-07-18 14:40:10 +02:00
package ginext
import (
"github.com/gin-gonic/gin"
"net/http"
)
2023-07-18 16:08:24 +02:00
var anyMethods = []string{
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch,
http.MethodHead, http.MethodOptions, http.MethodDelete, http.MethodConnect,
http.MethodTrace,
}
2023-07-18 14:40:10 +02:00
type GinRoutesWrapper struct {
wrapper *GinWrapper
routes gin.IRouter
}
type GinRouteBuilder struct {
routes *GinRoutesWrapper
2023-07-18 16:08:24 +02:00
methods []string
2023-07-18 14:40:10 +02:00
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)}
}
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
2023-07-18 16:08:24 +02:00
return &GinRouteBuilder{routes: w, methods: []string{http.MethodGet}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
2023-07-18 14:40:10 +02:00
}
func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder {
2023-07-18 16:08:24 +02:00
return &GinRouteBuilder{routes: w, methods: []string{http.MethodPost}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
2023-07-18 14:40:10 +02:00
}
func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder {
2023-07-18 16:08:24 +02:00
return &GinRouteBuilder{routes: w, methods: []string{http.MethodDelete}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
2023-07-18 14:40:10 +02:00
}
func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder {
2023-07-18 16:08:24 +02:00
return &GinRouteBuilder{routes: w, methods: []string{http.MethodPatch}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
2023-07-18 14:40:10 +02:00
}
func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder {
2023-07-18 16:08:24 +02:00
return &GinRouteBuilder{routes: w, methods: []string{http.MethodPut}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
2023-07-18 14:40:10 +02:00
}
func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder {
2023-07-18 16:08:24 +02:00
return &GinRouteBuilder{routes: w, methods: []string{http.MethodOptions}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
2023-07-18 14:40:10 +02:00
}
func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder {
2023-07-18 16:08:24 +02:00
return &GinRouteBuilder{routes: w, methods: []string{http.MethodHead}, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
2023-07-18 14:40:10 +02:00
}
func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder {
2023-07-18 16:08:24 +02:00
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)}
2023-07-18 14:40:10 +02:00
}
func (w *GinRouteBuilder) Use(middleware gin.HandlerFunc) *GinRouteBuilder {
w.handlers = append(w.handlers, middleware)
return w
}
2023-07-18 15:23:32 +02:00
2023-07-18 14:40:10 +02:00
func (w *GinRouteBuilder) Handle(handler WHandlerFunc) {
w.handlers = append(w.handlers, Wrap(w.routes.wrapper, handler))
2023-07-18 16:08:24 +02:00
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))
2023-07-18 14:40:10 +02:00
}