Files
aiops/backend/internal/api/routes/routes.go
T
attacker ff0b268984 feat: 修复仪表盘数据管线 — 补充应用发布种子数据 + 修复limit参数解析 + LDAP认证集成 + User类型完善
后端改进:
- 修复 dashboard/recent-alerts 的 limit 参数解析 (原代码错误地将数字解析为日期)
- 为 dashboard/recent-releases 添加 limit 参数解析支持
- 新增 generateAppReleases 种子数据生成函数 (10条应用发布记录)
- 修复 AlertEvent 的 rule_name/rule_id/severity 数据一致性
- 添加 go-ldap/ldap/v3 依赖并集成 LDAP 认证处理器
- 添加 seed.go 自动种子数据初始化 (admin用户+LDAP配置)
- User model 添加 AuthSource/LDAPConfig 字段

前端改进:
- 修复 User 类型定义以匹配后端 API (real_name/is_admin/auth_source等)
- 修复 Header.tsx 的 real_name 属性引用导致 TypeScript 编译错误
- 统一 API 服务层、暗色主题样式、组件联调优化

验证:
- go build ./... 编译通过
- npm run build 构建通过
- 仪表盘发布历史、告警消息等区域数据正常展示
- 健康检查 /health 正常
2026-06-14 02:11:10 +08:00

225 lines
7.8 KiB
Go

package routes
import (
"ezops/internal/api/handlers"
"ezops/internal/api/middleware"
"ezops/internal/config"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"gorm.io/gorm"
)
// Register 注册所有路由
func Register(r *gin.Engine, db *gorm.DB, log *zap.Logger, cfg *config.Config) {
// API版本前缀
api := r.Group("/api/v1")
// 健康检查
r.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{"status": "ok"})
})
// 公开路由
authHandler := handlers.NewUserHandler(db, cfg)
api.POST("/auth/login", authHandler.Login)
api.POST("/auth/register", authHandler.Register)
// 需要认证的路由
authorized := api.Group("")
authorized.Use(middleware.JWTAuth(&cfg.JWT))
// 用户路由
user := authorized.Group("/user")
{
user.GET("/profile", authHandler.GetProfile)
user.PUT("/profile", authHandler.UpdateProfile)
user.PUT("/password", authHandler.ChangePassword)
}
// 管理员路由
admin := authorized.Group("/admin")
admin.Use(middleware.AdminAuth())
{
admin.GET("/users", authHandler.ListUsers)
admin.GET("/users/:id", authHandler.GetUser)
admin.POST("/users", authHandler.CreateUser)
admin.PUT("/users/:id", authHandler.UpdateUser)
admin.DELETE("/users/:id", authHandler.DeleteUser)
}
// LDAP 配置路由
ldapHandler := handlers.NewLDAPHandler(db)
ldapGroup := authorized.Group("/admin/ldap-config")
ldapGroup.Use(middleware.AdminAuth())
{
ldapGroup.GET("", ldapHandler.GetLDAPConfig)
ldapGroup.PUT("", ldapHandler.UpdateLDAPConfig)
ldapGroup.POST("/test", ldapHandler.TestLDAPConnection)
ldapGroup.POST("/users", ldapHandler.ListLDAPUsers)
ldapGroup.POST("/import", ldapHandler.BatchImportLDAP)
}
// 仪表盘路由
dashboardHandler := handlers.NewDashboardHandler(db)
dashboard := authorized.Group("/dashboard")
{
dashboard.GET("/stats", dashboardHandler.GetStats)
dashboard.GET("/recent-alerts", dashboardHandler.GetRecentAlerts)
dashboard.GET("/recent-releases", dashboardHandler.GetRecentReleases)
dashboard.GET("/metrics", dashboardHandler.GetMetrics)
}
// 服务器路由
serverHandler := handlers.NewServerHandler(db)
servers := authorized.Group("/servers")
{
servers.GET("", serverHandler.ListServers)
servers.GET("/stats", serverHandler.GetServerStats)
servers.GET("/:id", serverHandler.GetServer)
servers.POST("", serverHandler.CreateServer)
servers.PUT("/:id", serverHandler.UpdateServer)
servers.DELETE("/:id", serverHandler.DeleteServer)
servers.POST("/batch-delete", serverHandler.BatchDeleteServers)
}
// 云账号路由
cloudAccountHandler := handlers.NewCloudAccountHandler(db)
cloudAccounts := authorized.Group("/cloud-accounts")
{
cloudAccounts.GET("", cloudAccountHandler.ListCloudAccounts)
cloudAccounts.POST("", cloudAccountHandler.CreateCloudAccount)
cloudAccounts.DELETE("/:id", cloudAccountHandler.DeleteCloudAccount)
}
// 域名路由
domainHandler := handlers.NewDomainHandler(db)
domains := authorized.Group("/domains")
{
domains.GET("", domainHandler.ListDomains)
domains.POST("", domainHandler.CreateDomain)
domains.DELETE("/:id", domainHandler.DeleteDomain)
}
// K8s集群路由
k8sHandler := handlers.NewK8sHandler(db)
clusters := authorized.Group("/k8s-clusters")
{
clusters.GET("", k8sHandler.ListClusters)
clusters.GET("/:id", k8sHandler.GetCluster)
clusters.POST("", k8sHandler.CreateCluster)
clusters.PUT("/:id", k8sHandler.UpdateCluster)
clusters.DELETE("/:id", k8sHandler.DeleteCluster)
clusters.GET("/:id/nodes", k8sHandler.ListNodes)
clusters.GET("/:id/pods", k8sHandler.ListPods)
clusters.GET("/:id/deployments", k8sHandler.ListDeployments)
clusters.GET("/:id/services", k8sHandler.ListServices)
clusters.GET("/:id/ingresses", k8sHandler.ListIngresses)
}
// 告警路由
alertHandler := handlers.NewAlertHandler(db)
alerts := authorized.Group("/alerts")
{
alerts.GET("", alertHandler.ListAlerts)
alerts.GET("/rules", alertHandler.ListRules)
alerts.POST("/rules", alertHandler.CreateRule)
alerts.PUT("/rules/:id", alertHandler.UpdateRule)
alerts.DELETE("/rules/:id", alertHandler.DeleteRule)
alerts.POST("/rules/:id/toggle", alertHandler.ToggleRule)
alerts.POST("/:id/ack", alertHandler.AcknowledgeAlert)
}
// 流水线路由
pipelineHandler := handlers.NewPipelineHandler(db)
pipelines := authorized.Group("/pipelines")
{
pipelines.GET("", pipelineHandler.ListPipelines)
pipelines.POST("", pipelineHandler.CreatePipeline)
pipelines.GET("/:id", pipelineHandler.GetPipeline)
pipelines.PUT("/:id", pipelineHandler.UpdatePipeline)
pipelines.DELETE("/:id", pipelineHandler.DeletePipeline)
pipelines.POST("/:id/trigger", pipelineHandler.TriggerPipeline)
pipelines.GET("/:id/runs", pipelineHandler.ListRuns)
}
// 应用发布路由
releases := authorized.Group("/releases")
{
releases.GET("", pipelineHandler.ListReleases)
releases.POST("", pipelineHandler.CreateRelease)
releases.POST("/:id/approve", pipelineHandler.ApproveRelease)
releases.POST("/:id/deploy", pipelineHandler.DeployRelease)
releases.POST("/:id/rollback", pipelineHandler.RollbackRelease)
}
// 自动化运维路由
automationHandler := handlers.NewAutomationHandler(db)
cronJobs := authorized.Group("/cron-jobs")
{
cronJobs.GET("", automationHandler.ListCronJobs)
cronJobs.POST("", automationHandler.CreateCronJob)
cronJobs.GET("/:id", automationHandler.GetCronJob)
cronJobs.PUT("/:id", automationHandler.UpdateCronJob)
cronJobs.DELETE("/:id", automationHandler.DeleteCronJob)
cronJobs.POST("/:id/toggle", automationHandler.ToggleCronJob)
cronJobs.POST("/:id/execute", automationHandler.ExecuteCronJob)
}
scripts := authorized.Group("/scripts")
{
scripts.GET("", automationHandler.ListScripts)
scripts.POST("", automationHandler.CreateScript)
scripts.GET("/:id", automationHandler.GetScript)
scripts.PUT("/:id", automationHandler.UpdateScript)
scripts.DELETE("/:id", automationHandler.DeleteScript)
scripts.POST("/:id/execute", automationHandler.ExecuteScript)
}
// 审批流程路由
approvalHandler := handlers.NewApprovalHandler(db)
approvals := authorized.Group("/approvals")
{
approvals.GET("/templates", approvalHandler.ListTemplates)
approvals.POST("/templates", approvalHandler.CreateTemplate)
approvals.GET("/templates/:id", approvalHandler.GetTemplate)
approvals.PUT("/templates/:id", approvalHandler.UpdateTemplate)
approvals.DELETE("/templates/:id", approvalHandler.DeleteTemplate)
approvals.GET("/processes", approvalHandler.ListProcesses)
approvals.POST("/processes", approvalHandler.CreateProcess)
approvals.GET("/processes/:id", approvalHandler.GetProcess)
approvals.POST("/processes/:id/approve", approvalHandler.ApproveProcess)
approvals.POST("/processes/:id/reject", approvalHandler.RejectProcess)
}
// 导航路由
navHandler := handlers.NewNavigationHandler(db)
navigations := authorized.Group("/navigations")
{
navigations.GET("/groups", navHandler.ListGroups)
navigations.POST("/groups", navHandler.CreateGroup)
navigations.PUT("/groups/:id", navHandler.UpdateGroup)
navigations.DELETE("/groups/:id", navHandler.DeleteGroup)
navigations.GET("/links", navHandler.ListLinks)
navigations.POST("/links", navHandler.CreateLink)
navigations.PUT("/links/:id", navHandler.UpdateLink)
navigations.DELETE("/links/:id", navHandler.DeleteLink)
navigations.POST("/links/:id/favorite", navHandler.ToggleFavorite)
}
// 审计日志路由
auditHandler := handlers.NewAuditHandler(db)
audit := authorized.Group("/audit")
{
audit.GET("/logs", auditHandler.ListAuditLogs)
audit.GET("/logs/:id", auditHandler.GetAuditLogDetail)
audit.GET("/modules", auditHandler.GetAuditModules)
audit.GET("/actions", auditHandler.GetAuditActions)
audit.GET("/logs/export", auditHandler.ExportAuditLogs)
audit.GET("/statistics", auditHandler.GetAuditStatistics)
audit.POST("/logs/clear", auditHandler.ClearAuditLogs)
}
}