feat: 优化路由注册逻辑
This commit is contained in:
@@ -135,6 +135,9 @@ func (s *server) Start() {
|
||||
// 注册文档
|
||||
s.uiInstance.RegisterHandler(s.router, s.option.swaggerBaseUri)
|
||||
gracefulServer := graceful.NewServer(fmt.Sprintf(":%d", s.port), s.Router())
|
||||
defer func() {
|
||||
_ = gracefulServer.Close()
|
||||
}()
|
||||
if err := gracefulServer.ListenAndServe(); err != nil {
|
||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||
fmt.Println("接收到退出指令, 服务平滑关闭")
|
||||
@@ -163,38 +166,48 @@ func (s *server) SetCustomRouter(f func(r *gin.Engine)) {
|
||||
}
|
||||
|
||||
// Group 注册接口路由
|
||||
func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, cList ...any) {
|
||||
g := s.router.Group(routerPrefix)
|
||||
g.Use(middlewareList...)
|
||||
cParser := controller{}
|
||||
for _, c := range cList {
|
||||
urlTable := cParser.Parse(c)
|
||||
func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, controllerList ...any) {
|
||||
routerGroup := s.router.Group(routerPrefix)
|
||||
routerGroup.Use(middlewareList...)
|
||||
parser := controllerParser{}
|
||||
for _, itemController := range controllerList {
|
||||
urlTable := parser.Parse(itemController)
|
||||
for _, itemUriCfg := range urlTable {
|
||||
_ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
|
||||
// 普通 HTTP 请求
|
||||
handleFunc := s.RequestHandler(itemUriCfg)
|
||||
if itemUriCfg.IsSse {
|
||||
// SSE 连接
|
||||
handleFunc = s.SseHandler(itemUriCfg)
|
||||
}
|
||||
// 一个接口支持注册多种请求方法
|
||||
for _, method := range itemUriCfg.RequestMethod {
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
g.GET(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodHead:
|
||||
g.HEAD(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodPost:
|
||||
g.POST(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodPut:
|
||||
g.PUT(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodPatch:
|
||||
g.PATCH(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodDelete:
|
||||
g.DELETE(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodOptions:
|
||||
g.OPTIONS(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodTrace:
|
||||
panic(`method Trace is not supported`)
|
||||
default:
|
||||
panic("method " + method + " is not support")
|
||||
}
|
||||
s.registerRouter(routerGroup, method, itemUriCfg, handleFunc)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// registerRouter 注册路由
|
||||
func (s *server) registerRouter(routerGroup *gin.RouterGroup, method string, itemUriCfg UriConfig, handleFunc gin.HandlerFunc) {
|
||||
switch method {
|
||||
case http.MethodGet:
|
||||
routerGroup.GET(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodHead:
|
||||
routerGroup.HEAD(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodPost:
|
||||
routerGroup.POST(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodPut:
|
||||
routerGroup.PUT(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodPatch:
|
||||
routerGroup.PATCH(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodDelete:
|
||||
routerGroup.DELETE(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodOptions:
|
||||
routerGroup.OPTIONS(itemUriCfg.Path, handleFunc)
|
||||
case http.MethodTrace:
|
||||
panic(`method Trace is not supported`)
|
||||
default:
|
||||
panic("method " + method + " is not support")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user