feat: 优化路由注册逻辑
This commit is contained in:
@@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
// controller 解析controller有哪些方法要注册为接口
|
||||
type controller struct {
|
||||
type controllerParser struct {
|
||||
}
|
||||
|
||||
// Parse 执行解析
|
||||
@@ -26,11 +26,7 @@ type controller struct {
|
||||
// 1. 函数接受两个入参, 第一个参数为 gin.Context , 第二个参数为 任意结构体指针, 但是必须声明 Meta 相关信息, 否则会报错
|
||||
//
|
||||
// 2. 函数有两个返回值, 第一个返回值为任意结构体/结构体指针(限制死不能为map/slice, 方便后续统一标准化) , 第二个返回值为 error , 代表处理的异常, 会自动适配 exception.IException 类型
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:31 2025/1/27
|
||||
func (c controller) Parse(inputController any) map[string]UriConfig {
|
||||
func (c controllerParser) Parse(inputController any) map[string]UriConfig {
|
||||
parseRes := make(map[string]UriConfig)
|
||||
if nil == inputController {
|
||||
return parseRes
|
||||
@@ -51,7 +47,7 @@ func (c controller) Parse(inputController any) map[string]UriConfig {
|
||||
}
|
||||
|
||||
// preCheckMethod 预检查方法是否可以注册为接口
|
||||
func (c controller) preCheckMethod(reflectMethod reflect.Method) (bool, reflect.Type, reflect.StructField) {
|
||||
func (c controllerParser) preCheckMethod(reflectMethod reflect.Method) (bool, reflect.Type, reflect.StructField) {
|
||||
var (
|
||||
metaField reflect.StructField
|
||||
metaFieldExist bool
|
||||
@@ -87,7 +83,7 @@ func (c controller) preCheckMethod(reflectMethod reflect.Method) (bool, reflect.
|
||||
// 参数 : 方法反射结果
|
||||
//
|
||||
// 返回值 : 第一个 -> 解析出的接口配置 第二个 -> 是否要注册为接口
|
||||
func (c controller) methodConfig(reflectMethod reflect.Method) (UriConfig, bool) {
|
||||
func (c controllerParser) methodConfig(reflectMethod reflect.Method) (UriConfig, bool) {
|
||||
var (
|
||||
needRegister bool
|
||||
metaField reflect.StructField
|
||||
@@ -138,7 +134,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (UriConfig, bool)
|
||||
}
|
||||
|
||||
// setUriMeta 设置接口的 meta 信息
|
||||
func (c controller) setUriMeta(metaField reflect.StructField, cfg *UriConfig) {
|
||||
func (c controllerParser) setUriMeta(metaField reflect.StructField, cfg *UriConfig) {
|
||||
// 解析 meta 信息
|
||||
cfg.Path = metaField.Tag.Get(TagNamePath) // 接口路由
|
||||
cfg.RequestMethod = strings.Split(strings.ToUpper(metaField.Tag.Get(TagNameMethod)), ",") // 请求方法
|
||||
|
||||
@@ -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