goext/ginext/routes.go
2023-07-18 14:40:10 +02:00

69 lines
2.4 KiB
Go

package ginext
import (
"github.com/gin-gonic/gin"
"net/http"
)
type GinRoutesWrapper struct {
wrapper *GinWrapper
routes gin.IRouter
}
type GinRouteBuilder struct {
routes *GinRoutesWrapper
method 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)}
}
func (w *GinRoutesWrapper) GET(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodGet, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
}
func (w *GinRoutesWrapper) POST(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodPost, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
}
func (w *GinRoutesWrapper) DELETE(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodDelete, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
}
func (w *GinRoutesWrapper) PATCH(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodPatch, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
}
func (w *GinRoutesWrapper) PUT(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodPut, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
}
func (w *GinRoutesWrapper) OPTIONS(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodOptions, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
}
func (w *GinRoutesWrapper) HEAD(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: http.MethodHead, relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
}
func (w *GinRoutesWrapper) COUNT(relativePath string) *GinRouteBuilder {
return &GinRouteBuilder{routes: w, method: "COUNT", relPath: relativePath, handlers: make([]gin.HandlerFunc, 0)}
}
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))
w.routes.routes.Handle(w.method, w.relPath, w.handlers...)
}