371 lines
19 KiB
Go
371 lines
19 KiB
Go
package routes
|
||
|
||
import (
|
||
"goalfymax-admin/internal/api/handlers"
|
||
"goalfymax-admin/internal/api/middlewares"
|
||
"goalfymax-admin/internal/config"
|
||
"goalfymax-admin/internal/models"
|
||
"goalfymax-admin/internal/services"
|
||
"goalfymax-admin/internal/storage"
|
||
"goalfymax-admin/pkg/middleware"
|
||
"goalfymax-admin/pkg/redis"
|
||
"goalfymax-admin/pkg/utils"
|
||
"time"
|
||
|
||
"github.com/gin-contrib/cors"
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// SetupRoutes 设置路由
|
||
func SetupRoutes(
|
||
userService services.UserService,
|
||
roleService services.RoleService,
|
||
pageService services.PageService,
|
||
quotaService services.QuotaService,
|
||
ssoService services.SSOService,
|
||
rbacService services.RBACService,
|
||
userLevelConfigService services.UserLevelConfigService,
|
||
systemConfigService services.SystemConfigService,
|
||
redisClient *redis.Client,
|
||
logger *utils.Logger,
|
||
appConfig *config.Config,
|
||
) *gin.Engine {
|
||
// 创建Gin引擎
|
||
r := gin.New()
|
||
|
||
// 添加CORS中间件
|
||
r.Use(cors.New(cors.Config{
|
||
AllowOrigins: []string{"http://localhost:5173", "http://localhost:5174", "http://localhost:3000", "http://localhost:3003", "http://localhost:3004"},
|
||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
|
||
AllowHeaders: []string{"Origin", "Content-Type", "Accept", "Authorization", "X-Requested-With", "Cookie"},
|
||
ExposeHeaders: []string{"Content-Length", "Content-Type"},
|
||
AllowCredentials: true,
|
||
MaxAge: 12 * time.Hour,
|
||
}))
|
||
|
||
// 添加中间件
|
||
r.Use(middlewares.RequestLogMiddleware(logger))
|
||
r.Use(middlewares.APILogMiddleware(logger)) // API日志中间件(记录数据修改接口)
|
||
r.Use(gin.Recovery())
|
||
|
||
// 创建SSO客户端和认证中间件
|
||
ssoConfig := &models.SSOConfig{
|
||
SSOServerURL: appConfig.SSO.SSOServerURL,
|
||
ClientID: appConfig.SSO.ClientID,
|
||
ClientSecret: appConfig.SSO.ClientSecret,
|
||
RedirectURI: appConfig.SSO.RedirectURI,
|
||
Scope: appConfig.SSO.Scope,
|
||
ResourceAud: appConfig.SSO.ResourceAud,
|
||
Timeout: appConfig.SSO.Timeout,
|
||
}
|
||
ssoClient := middleware.NewSSOClient(ssoConfig, logger)
|
||
sessionManager := middleware.NewMemorySessionManager()
|
||
authMiddleware := middleware.NewAuthMiddleware(ssoClient, sessionManager, "/login")
|
||
|
||
// RBAC中间件已简化,不再需要全局实例
|
||
|
||
// 创建处理器
|
||
quotaHandler := handlers.NewQuotaHandler(quotaService)
|
||
userProjectQuotaHandler := handlers.NewUserProjectQuotaHandler(
|
||
services.NewUserProjectQuotaService(
|
||
storage.NewUserProjectQuotaStorage(),
|
||
),
|
||
)
|
||
ssoHandler := handlers.NewSSOHandler(ssoService, logger)
|
||
messagePushService := services.NewMessagePushService()
|
||
ssoAdminService := services.NewSSOAdminService()
|
||
userHandler := handlers.NewUserHandler(userService, rbacService, logger)
|
||
auditLogService := services.NewAuditLogService(storage.NewAuditLogStorage())
|
||
goalfyUserHandler := handlers.NewGoalfyMaxUserHandler(
|
||
services.NewGoalfyMaxUserService(storage.NewGoalfyMaxUserStorage(), messagePushService, ssoAdminService, redisClient, storage.NewBalanceOperationLogStorage(), auditLogService, logger),
|
||
)
|
||
auditLogHandler := handlers.NewAuditLogHandler(auditLogService)
|
||
userFeedbackHandler := handlers.NewUserFeedbackHandler(
|
||
services.NewUserFeedbackService(storage.NewUserFeedbackStorage()),
|
||
)
|
||
messagePushHandler := handlers.NewMessagePushHandler(
|
||
messagePushService,
|
||
)
|
||
roleHandler := handlers.NewRoleHandler(roleService, rbacService, logger)
|
||
pageHandler := handlers.NewPageHandler(pageService, logger)
|
||
rbacHandler := handlers.NewRBACHandler(rbacService, logger)
|
||
vendorPricingHandler := handlers.NewVendorModelPricingHandler(storage.GetDB())
|
||
vmPricingHandler := handlers.NewVmPricingHandler(storage.GetDB())
|
||
mcpProviderHandler := handlers.NewMCPProviderHandler()
|
||
financeHandler := handlers.NewFinanceHandler()
|
||
userLevelConfigHandler := handlers.NewUserLevelConfigHandler(userLevelConfigService, logger)
|
||
systemConfigHandler := handlers.NewSystemConfigHandler(systemConfigService, logger)
|
||
inviteCodeHandler := handlers.NewInviteCodeHandler(
|
||
services.NewInviteCodeService(storage.NewInviteCodeStorage()),
|
||
userLevelConfigService,
|
||
)
|
||
inviteCodeApplicationHandler := handlers.NewInviteCodeApplicationHandler(storage.GetDB())
|
||
|
||
// 健康检查
|
||
r.GET("/health", func(c *gin.Context) {
|
||
c.JSON(200, gin.H{"status": "ok"})
|
||
})
|
||
|
||
// API路由组
|
||
api := r.Group("/api")
|
||
{
|
||
// 公开接口(不需要认证)
|
||
public := api.Group("/public")
|
||
{
|
||
// 官网提交邀请码申请
|
||
public.POST("/invite-code/apply", inviteCodeApplicationHandler.SubmitApplication)
|
||
}
|
||
|
||
// SSO相关路由
|
||
sso := api.Group("/sso")
|
||
{
|
||
sso.POST("/login", ssoHandler.HandleSSOLogin) // SSO登录
|
||
sso.POST("/callback", ssoHandler.HandleSSOCallback) // SSO回调
|
||
sso.POST("/refresh", ssoHandler.HandleRefreshToken) // 刷新令牌
|
||
sso.POST("/logout", ssoHandler.HandleLogout) // 登出
|
||
sso.GET("/userinfo", ssoHandler.HandleUserInfo) // 获取用户信息
|
||
sso.GET("/online-users", ssoHandler.GetOnlineUsers) // 获取在线用户列表
|
||
sso.GET("/online-count", ssoHandler.GetOnlineUserCount) // 获取在线用户数量
|
||
sso.POST("/batch-logout", ssoHandler.BatchLogout) // 批量登出
|
||
}
|
||
|
||
// 管理员路由组(需要认证和动态权限检查)
|
||
admin := api.Group("/admin")
|
||
admin.Use(authMiddleware.RequireAuth())
|
||
// 使用简化的页面权限检查
|
||
{
|
||
// 用户管理 - 所有路由通过动态权限检查
|
||
users := admin.Group("/users")
|
||
{
|
||
users.GET("", userHandler.List) // 获取用户列表
|
||
users.POST("", userHandler.Create) // 创建用户
|
||
users.GET("/:id", userHandler.GetByID) // 获取用户详情
|
||
users.PUT("/:id", userHandler.Update) // 更新用户
|
||
users.DELETE("/:id", userHandler.Delete) // 删除用户
|
||
users.PUT("/:id/status", userHandler.UpdateStatus) // 更新用户状态
|
||
users.PUT("/:id/roles", userHandler.UpdateRoles) // 更新用户角色
|
||
users.GET("/:id/roles", userHandler.GetUserRoles) // 获取用户角色
|
||
users.GET("/:id/permissions", userHandler.GetUserPermissions) // 获取用户权限
|
||
users.GET("/check-role/:user_id", userHandler.CheckUserRole) // 检查用户系统角色
|
||
users.POST("/change-system-role", userHandler.ChangeUserSystemRole) // 变更用户系统角色
|
||
}
|
||
|
||
// GoalfyMax 用户管理
|
||
goalfyUsers := admin.Group("/goalfymax-users")
|
||
{
|
||
goalfyUsers.GET("", goalfyUserHandler.List) // 列表
|
||
goalfyUsers.POST("", goalfyUserHandler.Create) // 新增
|
||
goalfyUsers.GET(":id", goalfyUserHandler.GetByID) // 详情
|
||
goalfyUsers.PUT(":id", goalfyUserHandler.Update) // 编辑
|
||
goalfyUsers.DELETE(":id", goalfyUserHandler.Delete) // 删除
|
||
goalfyUsers.POST(":id/ban", goalfyUserHandler.Ban) // 封禁
|
||
goalfyUsers.POST(":id/unban", goalfyUserHandler.Unban) // 解封
|
||
goalfyUsers.POST(":id/add-balance", goalfyUserHandler.AddBalance) // 增加余额
|
||
goalfyUsers.POST(":id/deduct-balance", goalfyUserHandler.DeductBalance) // 减少余额
|
||
}
|
||
|
||
// 用户反馈管理
|
||
userFeedbacks := admin.Group("/user-feedback")
|
||
{
|
||
userFeedbacks.GET("", userFeedbackHandler.List) // 获取反馈列表
|
||
userFeedbacks.GET("/:id", userFeedbackHandler.GetByID) // 获取反馈详情
|
||
userFeedbacks.POST("/:id/mark-handled", userFeedbackHandler.MarkHandled) // 标记为已处理
|
||
userFeedbacks.DELETE("/:id", userFeedbackHandler.Delete) // 删除反馈
|
||
userFeedbacks.GET("/statistics", userFeedbackHandler.GetStatistics) // 获取统计信息
|
||
}
|
||
|
||
// 消息推送管理
|
||
messagePush := admin.Group("/message-push")
|
||
{
|
||
messagePush.POST("/send", messagePushHandler.SendMessage) // 发送消息
|
||
messagePush.GET("/logs", messagePushHandler.GetPushLogs) // 获取推送记录
|
||
messagePush.GET("/logs/:id", messagePushHandler.GetPushLogByID) // 获取推送记录详情
|
||
messagePush.GET("/users/search", messagePushHandler.SearchUsers) // 搜索用户
|
||
}
|
||
|
||
// 角色管理 - 所有路由通过动态权限检查
|
||
roles := admin.Group("/roles")
|
||
{
|
||
roles.GET("", roleHandler.List) // 获取角色列表
|
||
roles.POST("", roleHandler.Create) // 创建角色
|
||
roles.GET("/:id", roleHandler.GetByID) // 获取角色详情
|
||
roles.PUT("/:id", roleHandler.Update) // 更新角色
|
||
roles.DELETE("/:id", roleHandler.Delete) // 删除角色
|
||
roles.PUT("/:id/status", roleHandler.UpdateStatus) // 更新角色状态
|
||
roles.PUT("/:id/permissions", roleHandler.UpdatePermissions) // 更新角色权限
|
||
roles.GET("/:id/permissions", roleHandler.GetRolePermissions) // 获取角色权限
|
||
}
|
||
|
||
// 页面管理 - 所有路由通过页面权限检查
|
||
pages := admin.Group("/pages")
|
||
{
|
||
pages.GET("", pageHandler.List) // 获取页面列表
|
||
pages.POST("", pageHandler.Create) // 创建页面
|
||
pages.GET("/:id", pageHandler.GetByID) // 获取页面详情
|
||
pages.PUT("/:id", pageHandler.Update) // 更新页面
|
||
pages.DELETE("/:id", pageHandler.Delete) // 删除页面
|
||
}
|
||
|
||
// RBAC管理 - 所有路由通过动态权限检查
|
||
rbac := admin.Group("/rbac")
|
||
{
|
||
rbac.POST("/role-page-permissions", rbacHandler.AssignRolePagePermissions) // 分配角色页面权限
|
||
rbac.DELETE("/roles/:id/page-permissions", rbacHandler.RemoveRolePagePermissions) // 移除角色页面权限
|
||
rbac.GET("/roles/:id/page-permissions", rbacHandler.GetRolePagePermissions) // 获取角色页面权限
|
||
rbac.GET("/users/:id/permissions", rbacHandler.GetUserPermissions) // 获取用户权限
|
||
rbac.GET("/roles/:id/permissions", rbacHandler.GetRolePermissions) // 获取角色权限
|
||
rbac.GET("/check-page-permission", rbacHandler.CheckPagePermission) // 检查页面权限
|
||
rbac.GET("/users/:id/accessible-pages", rbacHandler.GetUserAccessiblePages) // 获取用户可访问页面
|
||
}
|
||
|
||
// 供应商模型价格配置
|
||
vendorPricing := admin.Group("/vendor-model-pricing")
|
||
{
|
||
vendorPricing.GET("", vendorPricingHandler.GetVendorModelPricing) // 获取价格配置列表
|
||
vendorPricing.PUT("/:id", vendorPricingHandler.UpdateModelPricing) // 更新模型价格
|
||
vendorPricing.GET("/providers", vendorPricingHandler.GetProviders) // 获取供应商列表
|
||
}
|
||
|
||
// 虚拟机价格配置
|
||
vmPricing := admin.Group("/vm-pricing")
|
||
{
|
||
vmPricing.GET("/specs", vmPricingHandler.GetVmSpecs) // 获取规格列表
|
||
vmPricing.POST("/specs", vmPricingHandler.CreateVmSpec) // 创建规格
|
||
vmPricing.PUT("/specs/:id", vmPricingHandler.UpdateVmSpec) // 更新规格价格
|
||
vmPricing.DELETE("/specs/:id", vmPricingHandler.DeleteVmSpec) // 删除规格
|
||
vmPricing.GET("/templates", vmPricingHandler.GetVmTemplates) // 获取模板列表
|
||
vmPricing.POST("/templates", vmPricingHandler.CreateVmTemplate) // 创建模板
|
||
vmPricing.DELETE("/templates/:id", vmPricingHandler.DeleteVmTemplate) // 删除模板
|
||
vmPricing.PUT("/templates/:id/default", vmPricingHandler.SetDefaultVmTemplate) // 设置默认模板
|
||
}
|
||
|
||
// MCP 价格配置(PostgreSQL)
|
||
mcpProviders := admin.Group("/mcp-providers")
|
||
{
|
||
mcpProviders.GET("", mcpProviderHandler.List)
|
||
mcpProviders.POST("", mcpProviderHandler.Create)
|
||
mcpProviders.GET(":id", mcpProviderHandler.GetByID)
|
||
mcpProviders.PUT(":id", mcpProviderHandler.Update)
|
||
mcpProviders.DELETE(":id", mcpProviderHandler.Delete)
|
||
mcpProviders.PATCH(":id/status", mcpProviderHandler.UpdateStatus)
|
||
mcpProviders.PATCH(":id/is-used", mcpProviderHandler.UpdateIsUsed)
|
||
}
|
||
|
||
// 用户等级配置管理
|
||
userLevelConfigs := admin.Group("/user-level-configs")
|
||
{
|
||
userLevelConfigs.GET("", userLevelConfigHandler.List) // 获取列表
|
||
userLevelConfigs.GET("/all", userLevelConfigHandler.GetAll) // 获取所有(不分页)
|
||
userLevelConfigs.POST("", userLevelConfigHandler.Create) // 创建
|
||
userLevelConfigs.GET("/:id", userLevelConfigHandler.GetByID) // 获取详情
|
||
userLevelConfigs.PUT("/:id", userLevelConfigHandler.Update) // 更新
|
||
userLevelConfigs.DELETE("/:id", userLevelConfigHandler.Delete) // 删除
|
||
userLevelConfigs.PUT("/:id/status", userLevelConfigHandler.UpdateStatus) // 更新状态
|
||
}
|
||
|
||
// 系统通用配置管理
|
||
systemConfigs := admin.Group("/system-configs")
|
||
{
|
||
systemConfigs.GET("", systemConfigHandler.List) // 获取列表
|
||
systemConfigs.GET("/all", systemConfigHandler.GetAll) // 获取所有(不分页)
|
||
systemConfigs.POST("", systemConfigHandler.Create) // 创建
|
||
systemConfigs.GET("/key/:key", systemConfigHandler.GetByKey) // 根据Key获取
|
||
systemConfigs.GET("/:id", systemConfigHandler.GetByID) // 获取详情
|
||
systemConfigs.PUT("/:id", systemConfigHandler.Update) // 更新
|
||
systemConfigs.DELETE("/:id", systemConfigHandler.Delete) // 删除
|
||
systemConfigs.PUT("/:id/status", systemConfigHandler.UpdateStatus) // 更新状态
|
||
}
|
||
|
||
// 邀请码管理(简化版)
|
||
inviteCodes := admin.Group("/invite-codes")
|
||
{
|
||
inviteCodes.GET("", inviteCodeHandler.GetInviteCodeList) // 获取邀请码列表
|
||
inviteCodes.POST("", inviteCodeHandler.CreateInviteCode) // 创建邀请码(支持设置过期时间)
|
||
inviteCodes.GET("/client-options", inviteCodeHandler.GetClientOptions) // 获取客户端选项
|
||
inviteCodes.GET("/statistics", inviteCodeHandler.GetInviteCodeStatistics) // 获取统计信息
|
||
inviteCodes.GET("/:id", inviteCodeHandler.GetInviteCodeDetail) // 获取邀请码详情
|
||
inviteCodes.PUT("/:id", inviteCodeHandler.UpdateInviteCode) // 更新邀请码(支持更新过期时间)
|
||
inviteCodes.DELETE("/:id", inviteCodeHandler.DeleteInviteCode) // 删除邀请码
|
||
inviteCodes.POST("/mark-used", inviteCodeHandler.MarkInviteCodeAsUsed) // 标记邀请码为已使用
|
||
inviteCodes.POST("/validate", inviteCodeHandler.ValidateInviteCode) // 验证邀请码是否有效
|
||
}
|
||
|
||
// 邀请码申请管理
|
||
inviteApplications := admin.Group("/invite-applications")
|
||
{
|
||
inviteApplications.GET("", inviteCodeApplicationHandler.GetApplicationList) // 获取申请列表
|
||
inviteApplications.GET("/statistics", inviteCodeApplicationHandler.GetStatistics) // 获取统计信息
|
||
inviteApplications.GET("/pending-count", inviteCodeApplicationHandler.GetPendingCount) // 获取待处理数量
|
||
inviteApplications.POST("/approve", inviteCodeApplicationHandler.ApproveApplication) // 审批通过申请
|
||
inviteApplications.POST("/reject", inviteCodeApplicationHandler.RejectApplication) // 审批拒绝申请
|
||
inviteApplications.POST("/batch-approve", inviteCodeApplicationHandler.BatchApproveApplications) // 批量审批通过
|
||
inviteApplications.POST("/batch-reject", inviteCodeApplicationHandler.BatchRejectApplications) // 批量审批拒绝
|
||
}
|
||
|
||
// 审计日志管理
|
||
auditLogs := admin.Group("/audit-logs")
|
||
{
|
||
auditLogs.GET("", auditLogHandler.List) // 获取审计日志列表
|
||
auditLogs.GET("/:id", auditLogHandler.GetByID) // 获取审计日志详情
|
||
}
|
||
}
|
||
|
||
// 财务数据(需要认证)
|
||
finance := api.Group("/finance")
|
||
finance.Use(authMiddleware.RequireAuth())
|
||
{
|
||
finance.GET("/sandbox-records", financeHandler.ListSandboxRecords)
|
||
finance.GET("/token-usages", financeHandler.ListTokenUsages)
|
||
finance.GET("/mcp-usages", financeHandler.ListMCPUsages)
|
||
finance.GET("/transaction-logs", financeHandler.ListTransactionLogs)
|
||
finance.GET("/payment-records", financeHandler.ListPaymentRecords)
|
||
finance.POST("/payment-records/refund", financeHandler.RefundPaymentRecord)
|
||
finance.GET("/mcp-account-recharge-records", financeHandler.ListMcpAccountRechargeRecords)
|
||
finance.POST("/mcp-account-recharge-records", financeHandler.CreateMcpAccountRechargeRecord)
|
||
finance.PUT("/mcp-account-recharge-records/:id", financeHandler.UpdateMcpAccountRechargeRecord)
|
||
finance.DELETE("/mcp-account-recharge-records/:id", financeHandler.DeleteMcpAccountRechargeRecord)
|
||
finance.GET("/mcp-provider-accounts", financeHandler.GetMcpProviderAccounts)
|
||
finance.GET("/mcp-account-balances", financeHandler.GetMcpAccountBalances)
|
||
finance.POST("/mcp-account-balances", financeHandler.CreateMcpAccountBalance)
|
||
finance.PUT("/mcp-account-balances/:provider_id", financeHandler.AdjustMcpAccountBalance)
|
||
finance.GET("/mcp-account-balances/:provider_id/history", financeHandler.GetMcpAccountBalanceHistory)
|
||
|
||
// 模型账号管理
|
||
finance.GET("/model-account-recharge-records", financeHandler.ListModelAccountRechargeRecords)
|
||
finance.POST("/model-account-recharge-records", financeHandler.CreateModelAccountRechargeRecord)
|
||
finance.PUT("/model-account-recharge-records/:id", financeHandler.UpdateModelAccountRechargeRecord)
|
||
finance.DELETE("/model-account-recharge-records/:id", financeHandler.DeleteModelAccountRechargeRecord)
|
||
finance.GET("/model-config-accounts", financeHandler.GetModelConfigAccounts)
|
||
finance.GET("/model-account-balances", financeHandler.GetModelAccountBalances)
|
||
finance.POST("/model-account-balances", financeHandler.CreateModelAccountBalance)
|
||
finance.PUT("/model-account-balances/:account", financeHandler.AdjustModelAccountBalance)
|
||
finance.GET("/model-account-balances/:account/history", financeHandler.GetModelAccountBalanceHistory)
|
||
}
|
||
|
||
// 配额相关路由(需要认证和动态权限检查)
|
||
quotas := api.Group("/quotas")
|
||
quotas.Use(authMiddleware.RequireAuth())
|
||
// 使用简化的页面权限检查
|
||
{
|
||
quotas.POST("/history", quotaHandler.GetQuotaHistory) // 获取配额历史
|
||
quotas.GET("/health", quotaHandler.HealthCheck) // 配额服务健康检查
|
||
quotas.GET("/rules", quotaHandler.GetQuotaRules) // 获取配额规则列表(转发网关)
|
||
quotas.POST("/rules", quotaHandler.CreateQuotaRule) // 创建规则(转发网关)
|
||
quotas.PUT("/rules/:id", quotaHandler.UpdateQuotaRule) // 更新规则(转发网关)
|
||
quotas.DELETE("/rules/:id", quotaHandler.DeleteQuotaRule) // 删除规则(转发网关)
|
||
|
||
// 用户项目配额 CRUD
|
||
userProject := quotas.Group("/user-project")
|
||
{
|
||
userProject.GET("", userProjectQuotaHandler.List)
|
||
userProject.POST("", userProjectQuotaHandler.Create)
|
||
userProject.GET(":id", userProjectQuotaHandler.GetByID)
|
||
userProject.PUT(":id", userProjectQuotaHandler.Update)
|
||
userProject.DELETE(":id", userProjectQuotaHandler.Delete)
|
||
}
|
||
}
|
||
}
|
||
|
||
return r
|
||
}
|