package api import ( "net/http" "github.com/gin-gonic/gin" "github.com/openteam/server/internal/api/middleware" "github.com/openteam/server/internal/app" "github.com/openteam/server/internal/proxy" ) // NewRouter 装配所有路由: // - /v1/* 代理端点(Bearer API Key,OpenAI 格式错误体) // - /api/v1/* 管理 API(会话 JWT) func NewRouter(a *app.App, gw *proxy.Gateway) *gin.Engine { if a.Cfg.Env == "production" { gin.SetMode(gin.ReleaseMode) } r := gin.New() r.Use(gin.Logger(), gin.Recovery(), middleware.CORS()) h := NewHandler(a) // --- 代理端点(对外)--- proxyGroup := r.Group("/v1") { proxyGroup.Any("/chat/completions", gw.Auth, gw.Handle) proxyGroup.Any("/responses", gw.Auth, gw.Handle) proxyGroup.Any("/models", gw.Auth, gw.Handle) } // 未匹配的 /v1/* 返回 OpenAI 风格 404(需先认证) r.NoRoute(func(c *gin.Context) { if len(c.Request.URL.Path) >= 3 && c.Request.URL.Path[:3] == "/v1" { gw.Auth(c) if !c.IsAborted() { gw.Handle(c) } return } c.JSON(http.StatusNotFound, gin.H{"error": "not found"}) }) // --- 管理 API --- api := r.Group("/api/v1") { auth := api.Group("/auth") { auth.POST("/register", h.Register) auth.POST("/login", h.Login) auth.POST("/refresh", h.Refresh) auth.POST("/logout", h.Logout) auth.GET("/me", middleware.SessionAuth(a), h.Me) } user := api.Group("", middleware.SessionAuth(a)) { user.GET("/user/profile", h.UserProfile) user.GET("/user/balance", h.UserBalance) user.GET("/user/models", h.UserModels) user.GET("/usage/summary", h.UsageSummary) user.GET("/usage/stats", h.UsageStats) user.GET("/usage/logs", h.UsageLogs) user.POST("/keys", h.CreateKey) user.GET("/keys", h.ListKeys) user.PATCH("/keys/:id", h.PatchKey) user.DELETE("/keys/:id", h.DeleteKey) } admin := api.Group("/admin", middleware.SessionAuth(a), middleware.AdminOnly) { admin.GET("/users", h.AdminUsers) admin.PATCH("/users/:id", h.AdminPatchUser) admin.POST("/users/:id/balance", h.AdminAdjustBalance) admin.GET("/config", h.AdminConfig) admin.PUT("/config", h.AdminPutConfig) // 渠道/模型/用量管理(M4);充值审核(M5 预留) } } r.GET("/healthz", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"status": "ok"}) }) return r }