1 Star 0 Fork 0

igo/pkg

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
router.go 2.69 KB
一键复制 编辑 原始数据 按行查看 历史
layte.xiao 提交于 2022-10-15 11:20 . init
package xgin
import (
"github.com/gin-gonic/gin"
"net/http"
"path/filepath"
"strings"
)
type RouteInfo struct {
Name string `json:"name"`
Path string `json:"path"`
baseName string
basePath string
}
type RoutesInfo []RouteInfo
type RouterGroup struct {
Name string
Group *gin.RouterGroup
}
var routes RoutesInfo
func Routes(prefix string) RoutesInfo {
// 对数据进行处理
var result RoutesInfo
for _, v := range routes {
if v.Name == "" {
continue
}
path := filepath.Join(v.basePath, v.Path)
if strings.Contains(path, prefix) {
result = append(result, v)
}
}
return result
}
func (info *RouteInfo) BasePath() string {
return info.basePath
}
func (info *RouteInfo) BaseName() string {
return info.baseName
}
func (group *RouterGroup) Handle(httpMethod string, info RouteInfo, handlers ...gin.HandlerFunc) gin.IRoutes {
routes = append(routes, RouteInfo{
Name: info.Name,
Path: info.Path,
baseName: group.Name,
basePath: group.Group.BasePath(),
})
return group.Group.Handle(httpMethod, info.Path, handlers...)
}
func (group *RouterGroup) Any(info RouteInfo, handlers ...gin.HandlerFunc) gin.IRoutes {
routes = append(routes, RouteInfo{
Name: info.Name,
Path: info.Path,
baseName: group.Name,
basePath: group.Group.BasePath(),
})
return group.Group.Any(info.Path, handlers...)
}
func (group *RouterGroup) POST(info RouteInfo, handlers ...gin.HandlerFunc) gin.IRoutes {
return group.Handle(http.MethodPost, info, handlers...)
}
func (group *RouterGroup) GET(info RouteInfo, handlers ...gin.HandlerFunc) gin.IRoutes {
return group.Handle(http.MethodGet, info, handlers...)
}
// DELETE is a shortcut for router.Handle("DELETE", path, handle).
func (group *RouterGroup) DELETE(info RouteInfo, handlers ...gin.HandlerFunc) gin.IRoutes {
return group.Handle(http.MethodDelete, info, handlers...)
}
// PATCH is a shortcut for router.Handle("PATCH", path, handle).
func (group *RouterGroup) PATCH(info RouteInfo, handlers ...gin.HandlerFunc) gin.IRoutes {
return group.Handle(http.MethodPatch, info, handlers...)
}
// PUT is a shortcut for router.Handle("PUT", path, handle).
func (group *RouterGroup) PUT(info RouteInfo, handlers ...gin.HandlerFunc) gin.IRoutes {
return group.Handle(http.MethodPut, info, handlers...)
}
// OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle).
func (group *RouterGroup) OPTIONS(info RouteInfo, handlers ...gin.HandlerFunc) gin.IRoutes {
return group.Handle(http.MethodOptions, info, handlers...)
}
// HEAD is a shortcut for router.Handle("HEAD", path, handle).
func (group *RouterGroup) HEAD(info RouteInfo, handlers ...gin.HandlerFunc) gin.IRoutes {
return group.Handle(http.MethodHead, info, handlers...)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/igolang/pkg.git
git@gitee.com:igolang/pkg.git
igolang
pkg
pkg
v1.18.6

搜索帮助