4af9414646
- memory_handler: Query/List/Add 支持管理员通过 user_id 参数跨用户查询 - router: sessions/active 移到 admin 路由组 (需要管理员权限) - devtools: sessions 代理路径更新为 /api/v1/admin/sessions/active
111 lines
3.0 KiB
Go
111 lines
3.0 KiB
Go
package router
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/yourname/cyrene-ai/gateway/internal/config"
|
|
"github.com/yourname/cyrene-ai/gateway/internal/handler"
|
|
"github.com/yourname/cyrene-ai/gateway/internal/middleware"
|
|
"github.com/yourname/cyrene-ai/gateway/internal/ws"
|
|
)
|
|
|
|
// Setup 注册所有路由
|
|
func Setup(r *gin.Engine, hub *ws.Hub, cfg *config.Config) {
|
|
// 限流器
|
|
rateLimiter := middleware.NewRateLimiter(10, 20) // 每秒10个请求,突发20
|
|
|
|
// 初始化处理器
|
|
authHandler := handler.NewAuthHandler(cfg)
|
|
sessionHandler := handler.NewSessionHandler(hub)
|
|
memoryHandler := handler.NewMemoryHandler(cfg.AICoreURL)
|
|
chatHandler := handler.NewChatHandler(cfg, hub)
|
|
|
|
// ========== 公开路由 ==========
|
|
api := r.Group("/api/v1")
|
|
|
|
// 健康检查
|
|
api.GET("/health", func(c *gin.Context) {
|
|
c.JSON(200, gin.H{
|
|
"status": "ok",
|
|
"service": "cyrene-gateway",
|
|
"ws_connections": hub.ClientCount(),
|
|
})
|
|
})
|
|
|
|
// 认证 (无需JWT)
|
|
auth := api.Group("/auth")
|
|
{
|
|
auth.POST("/register", authHandler.Register)
|
|
auth.POST("/login", authHandler.Login)
|
|
}
|
|
|
|
// ========== 需要认证的路由 ==========
|
|
protected := api.Group("")
|
|
protected.Use(middleware.JWTAuth(cfg))
|
|
protected.Use(rateLimiter.Handler())
|
|
{
|
|
// Token刷新
|
|
protected.POST("/auth/refresh", authHandler.RefreshToken)
|
|
|
|
// 会话管理
|
|
sessions := protected.Group("/sessions")
|
|
{
|
|
sessions.POST("", sessionHandler.Create)
|
|
sessions.GET("", sessionHandler.List)
|
|
sessions.GET("/:id", sessionHandler.Get)
|
|
sessions.DELETE("/:id", sessionHandler.Delete)
|
|
sessions.GET("/:id/messages", sessionHandler.GetMessages)
|
|
}
|
|
|
|
// 记忆管理
|
|
memory := protected.Group("/memory")
|
|
{
|
|
memory.GET("/search", memoryHandler.Query)
|
|
memory.GET("", memoryHandler.List)
|
|
memory.POST("", memoryHandler.Add)
|
|
memory.DELETE("", memoryHandler.Delete)
|
|
}
|
|
|
|
// Admin 路由 (需要管理员权限)
|
|
admin := protected.Group("/admin")
|
|
admin.Use(adminAuth())
|
|
{
|
|
admin.GET("/sessions", sessionHandler.ListActiveSessions)
|
|
admin.GET("/sessions/active", sessionHandler.GetActiveSessions)
|
|
admin.GET("/sessions/:id", sessionHandler.GetSession)
|
|
}
|
|
}
|
|
|
|
// ========== WebSocket路由 ==========
|
|
// WebSocket升级在HTTP层,token通过query参数或Header传递
|
|
wsGroup := r.Group("/ws")
|
|
{
|
|
wsGroup.GET("/chat", chatHandler.HandleWebSocket)
|
|
}
|
|
|
|
// ========== 静态文件服务 (生产环境) ==========
|
|
if cfg.Env == "production" {
|
|
r.Static("/assets", "./public/assets")
|
|
r.StaticFile("/", "./public/index.html")
|
|
r.NoRoute(func(c *gin.Context) {
|
|
c.File("./public/index.html")
|
|
})
|
|
}
|
|
}
|
|
|
|
// adminAuth 管理员权限中间件 (检查 userID 是否以 "admin_" 开头)
|
|
func adminAuth() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
if userID == "" || !strings.HasPrefix(userID, "admin_") {
|
|
c.JSON(http.StatusForbidden, gin.H{"error": "需要管理员权限"})
|
|
c.Abort()
|
|
return
|
|
}
|
|
c.Next()
|
|
}
|
|
}
|