dev 分支暂存

This commit is contained in:
2026-05-16 08:26:56 +08:00
parent 58c8caa570
commit eb4129176c
71 changed files with 8474 additions and 214 deletions
+83
View File
@@ -0,0 +1,83 @@
package router
import (
"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()
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)
}
// 记忆管理
memory := protected.Group("/memory")
{
memory.GET("/search", memoryHandler.Query)
memory.GET("", memoryHandler.List)
memory.POST("", memoryHandler.Add)
}
}
// ========== 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")
})
}
}