From 6f55ba748d520651ebd3bc769437edfb79c66922 Mon Sep 17 00:00:00 2001 From: xh <11675084@qq.com> Date: Thu, 28 Aug 2025 13:10:44 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87init=E6=94=B6=E9=9B=86?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E5=B9=B6=E7=94=B1Autoload=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/router/adminRoute/flow_route.go | 3 ++ .../router/adminRoute/monitor_client_route.go | 3 ++ .../router/adminRoute/monitor_error_route.go | 3 ++ .../adminRoute/monitor_project_route.go | 3 ++ server/router/adminRoute/route.go | 28 ++++++++++++++----- .../router/adminRoute/user_protocol_route.go | 6 ++-- server/router/route.go | 1 + .../tpl_utils/templates/gocode/route.go.tpl | 8 ++++-- 8 files changed, 43 insertions(+), 12 deletions(-) diff --git a/server/router/adminRoute/flow_route.go b/server/router/adminRoute/flow_route.go index 72562ec..4c4beaf 100644 --- a/server/router/adminRoute/flow_route.go +++ b/server/router/adminRoute/flow_route.go @@ -81,3 +81,6 @@ func FlowTemplateRoute(rg *gin.RouterGroup) { rg.POST("/flow_template/edit", handle.Edit) rg.POST("/flow_template/del", handle.Del) } +func init() { + routeHandlers = append(routeHandlers, FlowApplyRoute, FlowHistoryRoute, FlowTemplateRoute) +} diff --git a/server/router/adminRoute/monitor_client_route.go b/server/router/adminRoute/monitor_client_route.go index e7d7e45..52047f0 100644 --- a/server/router/adminRoute/monitor_client_route.go +++ b/server/router/adminRoute/monitor_client_route.go @@ -61,3 +61,6 @@ func MonitorClientRoute(rg *gin.RouterGroup) { r.GET("/monitor_client/ExportFile", middleware.RecordLog("监控-客户端信息导出"), handle.ExportFile) r.POST("/monitor_client/ImportFile", handle.ImportFile) } +func init() { + routeHandlers = append(routeHandlers, MonitorClientRoute) +} diff --git a/server/router/adminRoute/monitor_error_route.go b/server/router/adminRoute/monitor_error_route.go index c50ff2d..a0b4d8b 100644 --- a/server/router/adminRoute/monitor_error_route.go +++ b/server/router/adminRoute/monitor_error_route.go @@ -58,3 +58,6 @@ func MonitorErrorRoute(rg *gin.RouterGroup) { r.GET("/monitor_error/ExportFile", middleware.RecordLog("监控-错误列导出"), handle.ExportFile) r.POST("/monitor_error/ImportFile", handle.ImportFile) } +func init() { + routeHandlers = append(routeHandlers, MonitorErrorRoute) +} diff --git a/server/router/adminRoute/monitor_project_route.go b/server/router/adminRoute/monitor_project_route.go index c271ffd..558f44c 100644 --- a/server/router/adminRoute/monitor_project_route.go +++ b/server/router/adminRoute/monitor_project_route.go @@ -60,3 +60,6 @@ func MonitorProjectRoute(rg *gin.RouterGroup) { r.GET("/monitor_project/ExportFile", middleware.RecordLog("监控项目导出"), handle.ExportFile) r.POST("/monitor_project/ImportFile", handle.ImportFile) } +func init() { + routeHandlers = append(routeHandlers, MonitorProjectRoute) +} diff --git a/server/router/adminRoute/route.go b/server/router/adminRoute/route.go index b9c12a5..0cb2637 100644 --- a/server/router/adminRoute/route.go +++ b/server/router/adminRoute/route.go @@ -10,6 +10,18 @@ import ( "github.com/gin-gonic/gin" ) +type RouteHandlerFunc func(*gin.RouterGroup) + +// routeHandlers 全局的路由注册函数切片,用于自动加载路由,通过每个文件的init()收集 +var routeHandlers []RouteHandlerFunc + +// Autoload 自动加载所有路由 +func Autoload(rg *gin.RouterGroup) { + for _, handler := range routeHandlers { + handler(rg) + } +} + func RegisterRoute(rg *gin.RouterGroup) { rg = rg.Group("/admin") @@ -37,13 +49,15 @@ func RegisterRoute(rg *gin.RouterGroup) { systemController.RoleRoute(rg) systemController.LogRoute(rg) generatorController.GenRoute(rg) - FlowTemplateRoute(rg) - FlowApplyRoute(rg) - FlowHistoryRoute(rg) + // FlowTemplateRoute(rg) + // FlowApplyRoute(rg) + // FlowHistoryRoute(rg) - MonitorProjectRoute(rg) - MonitorClientRoute(rg) - MonitorErrorRoute(rg) + // MonitorProjectRoute(rg) + // MonitorClientRoute(rg) + // MonitorErrorRoute(rg) - UserProtocolRoute(rg) + // UserProtocolRoute(rg) + + Autoload(rg) } diff --git a/server/router/adminRoute/user_protocol_route.go b/server/router/adminRoute/user_protocol_route.go index 92ef825..47f49b0 100644 --- a/server/router/adminRoute/user_protocol_route.go +++ b/server/router/adminRoute/user_protocol_route.go @@ -13,8 +13,7 @@ import ( - 请先提交git避免文件覆盖!!! - 下载并解压压缩包后,直接复制server、admin文件夹到项目根目录即可 -2. 注册路由 -请在 router/admin/entry.go 文件引入 UserProtocolRoute 注册路由 +2. 注册路由(通过init函数收集路由,Autoload自动注册) 3. 后台手动添加菜单和按钮 admin:user_protocol:add @@ -60,3 +59,6 @@ func UserProtocolRoute(rg *gin.RouterGroup) { r.GET("/user_protocol/ExportFile", middleware.RecordLog("用户协议导出"), handle.ExportFile) r.POST("/user_protocol/ImportFile", handle.ImportFile) } +func init() { + routeHandlers = append(routeHandlers, UserProtocolRoute) +} diff --git a/server/router/route.go b/server/router/route.go index affc4da..23e5011 100644 --- a/server/router/route.go +++ b/server/router/route.go @@ -23,6 +23,7 @@ func RegisterRoute(api *gin.RouterGroup, rootRouter *gin.Engine) { // /api/admin adminRoute.RegisterRoute(api) + // /api/common/captcha 验证码 commonController.CaptchaRoute(api) } diff --git a/server/service/generatorService/tpl_utils/templates/gocode/route.go.tpl b/server/service/generatorService/tpl_utils/templates/gocode/route.go.tpl index 3f28cca..2116a76 100644 --- a/server/service/generatorService/tpl_utils/templates/gocode/route.go.tpl +++ b/server/service/generatorService/tpl_utils/templates/gocode/route.go.tpl @@ -12,8 +12,7 @@ import ( - 请先提交git避免文件覆盖!!! - 下载并解压压缩包后,直接复制server、admin文件夹到项目根目录即可 -2. 注册路由 -请在 router/admin/entry.go 文件引入 {{{ toUpperCamelCase .ModuleName }}}Route 注册路由 +2. 注册路由(通过init函数收集路由,Autoload自动注册) 3. 后台手动添加菜单和按钮 @@ -47,4 +46,7 @@ func {{{ toUpperCamelCase .ModuleName }}}Route(rg *gin.RouterGroup) { r.GET("/{{{ .ModuleName }}}/ExportFile", middleware.RecordLog("{{{ .FunctionName }}}导出"), handle.ExportFile) r.POST("/{{{ .ModuleName }}}/ImportFile", handle.ImportFile) -} \ No newline at end of file +} +func init() { + routeHandlers = append(routeHandlers, {{{ toUpperCamelCase .ModuleName }}}Route) +}