feat: 优化路由注册逻辑
This commit is contained in:
@@ -16,7 +16,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// controller 解析controller有哪些方法要注册为接口
|
// controller 解析controller有哪些方法要注册为接口
|
||||||
type controller struct {
|
type controllerParser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse 执行解析
|
// Parse 执行解析
|
||||||
@@ -26,11 +26,7 @@ type controller struct {
|
|||||||
// 1. 函数接受两个入参, 第一个参数为 gin.Context , 第二个参数为 任意结构体指针, 但是必须声明 Meta 相关信息, 否则会报错
|
// 1. 函数接受两个入参, 第一个参数为 gin.Context , 第二个参数为 任意结构体指针, 但是必须声明 Meta 相关信息, 否则会报错
|
||||||
//
|
//
|
||||||
// 2. 函数有两个返回值, 第一个返回值为任意结构体/结构体指针(限制死不能为map/slice, 方便后续统一标准化) , 第二个返回值为 error , 代表处理的异常, 会自动适配 exception.IException 类型
|
// 2. 函数有两个返回值, 第一个返回值为任意结构体/结构体指针(限制死不能为map/slice, 方便后续统一标准化) , 第二个返回值为 error , 代表处理的异常, 会自动适配 exception.IException 类型
|
||||||
//
|
func (c controllerParser) Parse(inputController any) map[string]UriConfig {
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 15:31 2025/1/27
|
|
||||||
func (c controller) Parse(inputController any) map[string]UriConfig {
|
|
||||||
parseRes := make(map[string]UriConfig)
|
parseRes := make(map[string]UriConfig)
|
||||||
if nil == inputController {
|
if nil == inputController {
|
||||||
return parseRes
|
return parseRes
|
||||||
@@ -51,7 +47,7 @@ func (c controller) Parse(inputController any) map[string]UriConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// preCheckMethod 预检查方法是否可以注册为接口
|
// 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 (
|
var (
|
||||||
metaField reflect.StructField
|
metaField reflect.StructField
|
||||||
metaFieldExist bool
|
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 (
|
var (
|
||||||
needRegister bool
|
needRegister bool
|
||||||
metaField reflect.StructField
|
metaField reflect.StructField
|
||||||
@@ -138,7 +134,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (UriConfig, bool)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// setUriMeta 设置接口的 meta 信息
|
// setUriMeta 设置接口的 meta 信息
|
||||||
func (c controller) setUriMeta(metaField reflect.StructField, cfg *UriConfig) {
|
func (c controllerParser) setUriMeta(metaField reflect.StructField, cfg *UriConfig) {
|
||||||
// 解析 meta 信息
|
// 解析 meta 信息
|
||||||
cfg.Path = metaField.Tag.Get(TagNamePath) // 接口路由
|
cfg.Path = metaField.Tag.Get(TagNamePath) // 接口路由
|
||||||
cfg.RequestMethod = strings.Split(strings.ToUpper(metaField.Tag.Get(TagNameMethod)), ",") // 请求方法
|
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)
|
s.uiInstance.RegisterHandler(s.router, s.option.swaggerBaseUri)
|
||||||
gracefulServer := graceful.NewServer(fmt.Sprintf(":%d", s.port), s.Router())
|
gracefulServer := graceful.NewServer(fmt.Sprintf(":%d", s.port), s.Router())
|
||||||
|
defer func() {
|
||||||
|
_ = gracefulServer.Close()
|
||||||
|
}()
|
||||||
if err := gracefulServer.ListenAndServe(); err != nil {
|
if err := gracefulServer.ListenAndServe(); err != nil {
|
||||||
if strings.Contains(err.Error(), "use of closed network connection") {
|
if strings.Contains(err.Error(), "use of closed network connection") {
|
||||||
fmt.Println("接收到退出指令, 服务平滑关闭")
|
fmt.Println("接收到退出指令, 服务平滑关闭")
|
||||||
@@ -163,38 +166,48 @@ func (s *server) SetCustomRouter(f func(r *gin.Engine)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Group 注册接口路由
|
// Group 注册接口路由
|
||||||
func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, cList ...any) {
|
func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, controllerList ...any) {
|
||||||
g := s.router.Group(routerPrefix)
|
routerGroup := s.router.Group(routerPrefix)
|
||||||
g.Use(middlewareList...)
|
routerGroup.Use(middlewareList...)
|
||||||
cParser := controller{}
|
parser := controllerParser{}
|
||||||
for _, c := range cList {
|
for _, itemController := range controllerList {
|
||||||
urlTable := cParser.Parse(c)
|
urlTable := parser.Parse(itemController)
|
||||||
for _, itemUriCfg := range urlTable {
|
for _, itemUriCfg := range urlTable {
|
||||||
_ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
|
_ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
|
||||||
|
// 普通 HTTP 请求
|
||||||
handleFunc := s.RequestHandler(itemUriCfg)
|
handleFunc := s.RequestHandler(itemUriCfg)
|
||||||
|
if itemUriCfg.IsSse {
|
||||||
|
// SSE 连接
|
||||||
|
handleFunc = s.SseHandler(itemUriCfg)
|
||||||
|
}
|
||||||
// 一个接口支持注册多种请求方法
|
// 一个接口支持注册多种请求方法
|
||||||
for _, method := range itemUriCfg.RequestMethod {
|
for _, method := range itemUriCfg.RequestMethod {
|
||||||
|
s.registerRouter(routerGroup, method, itemUriCfg, handleFunc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// registerRouter 注册路由
|
||||||
|
func (s *server) registerRouter(routerGroup *gin.RouterGroup, method string, itemUriCfg UriConfig, handleFunc gin.HandlerFunc) {
|
||||||
switch method {
|
switch method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
g.GET(itemUriCfg.Path, handleFunc)
|
routerGroup.GET(itemUriCfg.Path, handleFunc)
|
||||||
case http.MethodHead:
|
case http.MethodHead:
|
||||||
g.HEAD(itemUriCfg.Path, handleFunc)
|
routerGroup.HEAD(itemUriCfg.Path, handleFunc)
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
g.POST(itemUriCfg.Path, handleFunc)
|
routerGroup.POST(itemUriCfg.Path, handleFunc)
|
||||||
case http.MethodPut:
|
case http.MethodPut:
|
||||||
g.PUT(itemUriCfg.Path, handleFunc)
|
routerGroup.PUT(itemUriCfg.Path, handleFunc)
|
||||||
case http.MethodPatch:
|
case http.MethodPatch:
|
||||||
g.PATCH(itemUriCfg.Path, handleFunc)
|
routerGroup.PATCH(itemUriCfg.Path, handleFunc)
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
g.DELETE(itemUriCfg.Path, handleFunc)
|
routerGroup.DELETE(itemUriCfg.Path, handleFunc)
|
||||||
case http.MethodOptions:
|
case http.MethodOptions:
|
||||||
g.OPTIONS(itemUriCfg.Path, handleFunc)
|
routerGroup.OPTIONS(itemUriCfg.Path, handleFunc)
|
||||||
case http.MethodTrace:
|
case http.MethodTrace:
|
||||||
panic(`method Trace is not supported`)
|
panic(`method Trace is not supported`)
|
||||||
default:
|
default:
|
||||||
panic("method " + method + " is not support")
|
panic("method " + method + " is not support")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user