Files
EasyGoAdmin_GoFrame_EleVue/internal/router/router.go
T
2026-10-20 11:37:25 +08:00

111 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package router
import (
"fmt"
"github.com/gogf/gf/v2/net/ghttp"
"easygoadmin/internal/controller"
"easygoadmin/internal/middleware"
"easygoadmin/utility/response"
)
// ============================== 路由注册 ==============================
// errorf 构造状态页错误
//
// 响应层将其映射为 code=1 并附上消息
func errorf(format string, args ...any) error {
return fmt.Errorf(format, args...)
}
// Register 注册全局中间件链与路由
//
// 中间件顺序:CORS → Response(panic 兜底)→ RateLimit(限流)→ Demo(演示拦截)→ JWT → Tenant → OperationLog → 处理器。
// 权限校验在 Bind 包装层完成,与路由元数据同源;操作日志按元数据异步落库。
func Register(s *ghttp.Server) {
s.Use(
middleware.CORS,
middleware.Response,
middleware.RateLimit,
middleware.Demo,
middleware.JWT,
middleware.Tenant,
middleware.OperationLog,
)
// 非 200 状态统一输出 JSON(404/500 不返回 HTML)
s.BindStatusHandler(404, func(r *ghttp.Request) {
response.Write(r, response.Fail(errorf("接口不存在: %s %s", r.Method, r.URL.Path)))
})
s.BindStatusHandler(500, func(r *ghttp.Request) {
response.Write(r, response.Fail(errorf("服务器内部错误")))
})
// 静态文件:将 resource 目录作为静态资源根目录对外访问
s.SetServerRoot("resource")
// 健康检查(免登录;公开路由不带 /v1,见 middleware/public.go)
s.BindHandler("get:/api/health", func(r *ghttp.Request) {
response.Write(r, response.Ok(map[string]any{"status": "up"}))
})
// API 文档页(本地托管 Redoc,静态资源由 SetServerRoot("resource") 提供)
s.BindHandler("get:/swagger", func(r *ghttp.Request) {
r.Response.RedirectTo("/swagger/index.html")
})
registerControllers(s)
}
// registerControllers 注册各模块控制器
//
// 随实施进度追加
func registerControllers(s *ghttp.Server) {
// —— auth 认证域 ——
Bind(s, new(controller.CAuth))
// —— index 个人中心 ——
Bind(s, new(controller.CIndex))
// —— system 域 ——
Bind(s, new(controller.CUser))
Bind(s, new(controller.CRole))
Bind(s, new(controller.CMenu))
Bind(s, new(controller.CDept))
Bind(s, new(controller.CLevel))
Bind(s, new(controller.CPosition))
Bind(s, new(controller.CTenant))
// —— data 域 ——
Bind(s, new(controller.CConfig))
Bind(s, new(controller.CConfigItem))
Bind(s, new(controller.CParam))
Bind(s, new(controller.CDict))
Bind(s, new(controller.CDictItem))
Bind(s, new(controller.CNotice))
Bind(s, new(controller.CCity))
// —— content 域 ——
Bind(s, new(controller.CArticle))
Bind(s, new(controller.CCategory))
Bind(s, new(controller.CLink))
// —— monitor 域 ——
Bind(s, new(controller.COperLog))
Bind(s, new(controller.CLoginLog))
Bind(s, new(controller.CJob))
Bind(s, new(controller.CJobLog))
Bind(s, new(controller.COnlineUser))
Bind(s, new(controller.CServerMonitor))
Bind(s, new(controller.CCacheMonitor))
// —— tool 域 ——
Bind(s, new(controller.CFileTemplate))
Bind(s, new(controller.CGenerator))
Bind(s, new(controller.CUpload))
// —— tool 域(代码生成器注册示例) ——
Bind(s, new(controller.CExample))
}