通过init收集路由并由Autoload注册

This commit is contained in:
xh
2025-08-28 13:10:44 +08:00
parent 606bcbef0a
commit 6f55ba748d
8 changed files with 43 additions and 12 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -23,6 +23,7 @@ func RegisterRoute(api *gin.RouterGroup, rootRouter *gin.Engine) {
// /api/admin
adminRoute.RegisterRoute(api)
// /api/common/captcha 验证码
commonController.CaptchaRoute(api)
}

View File

@@ -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)
}
}
func init() {
routeHandlers = append(routeHandlers, {{{ toUpperCamelCase .ModuleName }}}Route)
}