删除 fork 独有的实时日志相关功能(上游 Wei-Shaw/sub2api 不存在):
A. OpsLogBroadcaster + SSE 日志流(前端有用但用户不需要):
- backend/internal/service/ops_log_broadcaster{,_test}.go
- backend/internal/handler/ops_log_stream_middleware.go
- backend/internal/handler/admin/ops_log_stream_handler.go
- backend/internal/server/routes/admin.go: GET /admin/ops/logs/{stream,recent}
- backend/internal/server/routes/{gateway,windsurf_gateway}.go: opsLogStream middleware
- backend/internal/service/wire.go: ProvideOpsLogBroadcaster
- frontend/src/views/admin/ops/OpsLogStreamView.vue
- frontend/src/api/admin/ops.ts: subscribeOpsLogStream, getRecentOpsLogs,
OpsLogEntry/OpsLogFilter/OpsLogRecentResponse 类型
- frontend/src/router/index.ts: AdminOpsLogStream 路由
- frontend/src/components/layout/AppSidebar.vue: 侧边栏入口
- frontend/src/i18n/locales/{en,zh}.ts: nav.opsLogStream + admin.ops.logStream 全部文案
B. RequestEventBus + WS 请求事件流(前端零调用 dead code):
- backend/internal/service/request_event_bus{,_test}.go
- backend/internal/handler/admin/ops_ws_requests_handler.go
- backend/internal/server/routes/admin.go: GET /admin/ops/ws/requests
- backend/internal/handler/gateway_handler.go: RequestEventBus 字段/参数 +
reqStartTime + reqEventAccountID/reqEventStatus 跟踪 + defer Publish
- backend/internal/service/wire.go: NewRequestEventBus
- backend/internal/handler/admin/ops_handler.go: OpsHandler 中
requestEventBus + logBroadcaster 字段,简化 NewOpsHandler 签名
保留:
- /admin/ops/ws/qps (前端 QPS 监控仍在用)
- /admin/ops/realtime-traffic (前端在用)
- OpsErrorLoggerMiddleware (与本次无关)
签名变更:
- NewOpsHandler(opsService) — 移除 requestEventBus, logBroadcaster
- NewGatewayHandler(...): 移除 requestEventBus 末位参数
- ProvideRouter / SetupRouter / registerRoutes / RegisterGatewayRoutes /
RegisterWindsurfGatewayRoutes: 移除 opsLogBroadcaster 参数
- 同步更新 wire_gen.go + 测试调用点
验证:
- 后端 go build/vet 通过
- 前端 pnpm run build 通过 (9.48s)
- 测试: 2 个 baseline 既存失败 (TestProxyImportData...,
TestWindsurfTierAccessService_Snapshot_HappyPath) 与本次无关
226 lines
8.1 KiB
Go
226 lines
8.1 KiB
Go
package routes
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||
"github.com/Wei-Shaw/sub2api/internal/handler"
|
||
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// RegisterGatewayRoutes 注册 API 网关路由(Claude/OpenAI/Gemini 兼容)
|
||
func RegisterGatewayRoutes(
|
||
r *gin.Engine,
|
||
h *handler.Handlers,
|
||
apiKeyAuth middleware.APIKeyAuthMiddleware,
|
||
apiKeyService *service.APIKeyService,
|
||
subscriptionService *service.SubscriptionService,
|
||
opsService *service.OpsService,
|
||
settingService *service.SettingService,
|
||
cfg *config.Config,
|
||
) {
|
||
bodyLimit := middleware.RequestBodyLimit(cfg.Gateway.MaxBodySize)
|
||
clientRequestID := middleware.ClientRequestID()
|
||
opsErrorLogger := handler.OpsErrorLoggerMiddleware(opsService)
|
||
endpointNorm := handler.InboundEndpointMiddleware()
|
||
|
||
// 未分组 Key 拦截中间件(按协议格式区分错误响应)
|
||
requireGroupAnthropic := middleware.RequireGroupAssignment(settingService, middleware.AnthropicErrorWriter)
|
||
requireGroupGoogle := middleware.RequireGroupAssignment(settingService, middleware.GoogleErrorWriter)
|
||
|
||
// API网关(Claude API兼容)
|
||
gateway := r.Group("/v1")
|
||
gateway.Use(bodyLimit)
|
||
gateway.Use(clientRequestID)
|
||
gateway.Use(opsErrorLogger)
|
||
gateway.Use(endpointNorm)
|
||
gateway.Use(gin.HandlerFunc(apiKeyAuth))
|
||
gateway.Use(requireGroupAnthropic)
|
||
{
|
||
// /v1/messages: auto-route based on group platform
|
||
gateway.POST("/messages", func(c *gin.Context) {
|
||
if getGroupPlatform(c) == service.PlatformOpenAI {
|
||
h.OpenAIGateway.Messages(c)
|
||
return
|
||
}
|
||
h.Gateway.Messages(c)
|
||
})
|
||
// /v1/messages/count_tokens: OpenAI groups get 404
|
||
gateway.POST("/messages/count_tokens", func(c *gin.Context) {
|
||
if getGroupPlatform(c) == service.PlatformOpenAI {
|
||
c.JSON(http.StatusNotFound, gin.H{
|
||
"type": "error",
|
||
"error": gin.H{
|
||
"type": "not_found_error",
|
||
"message": "Token counting is not supported for this platform",
|
||
},
|
||
})
|
||
return
|
||
}
|
||
h.Gateway.CountTokens(c)
|
||
})
|
||
gateway.GET("/models", h.Gateway.Models)
|
||
gateway.GET("/usage", h.Gateway.Usage)
|
||
// OpenAI Responses API: auto-route based on group platform
|
||
gateway.POST("/responses", func(c *gin.Context) {
|
||
if getGroupPlatform(c) == service.PlatformOpenAI {
|
||
h.OpenAIGateway.Responses(c)
|
||
return
|
||
}
|
||
h.Gateway.Responses(c)
|
||
})
|
||
gateway.POST("/responses/*subpath", func(c *gin.Context) {
|
||
if getGroupPlatform(c) == service.PlatformOpenAI {
|
||
h.OpenAIGateway.Responses(c)
|
||
return
|
||
}
|
||
h.Gateway.Responses(c)
|
||
})
|
||
gateway.GET("/responses", h.OpenAIGateway.ResponsesWebSocket)
|
||
// OpenAI Chat Completions API: auto-route based on group platform
|
||
gateway.POST("/chat/completions", func(c *gin.Context) {
|
||
if getGroupPlatform(c) == service.PlatformOpenAI {
|
||
h.OpenAIGateway.ChatCompletions(c)
|
||
return
|
||
}
|
||
h.Gateway.ChatCompletions(c)
|
||
})
|
||
gateway.POST("/images/generations", func(c *gin.Context) {
|
||
if getGroupPlatform(c) != service.PlatformOpenAI {
|
||
c.JSON(http.StatusNotFound, gin.H{
|
||
"error": gin.H{
|
||
"type": "not_found_error",
|
||
"message": "Images API is not supported for this platform",
|
||
},
|
||
})
|
||
return
|
||
}
|
||
h.OpenAIGateway.Images(c)
|
||
})
|
||
gateway.POST("/images/edits", func(c *gin.Context) {
|
||
if getGroupPlatform(c) != service.PlatformOpenAI {
|
||
c.JSON(http.StatusNotFound, gin.H{
|
||
"error": gin.H{
|
||
"type": "not_found_error",
|
||
"message": "Images API is not supported for this platform",
|
||
},
|
||
})
|
||
return
|
||
}
|
||
h.OpenAIGateway.Images(c)
|
||
})
|
||
}
|
||
|
||
// Gemini 原生 API 兼容层(Gemini SDK/CLI 直连)
|
||
gemini := r.Group("/v1beta")
|
||
gemini.Use(bodyLimit)
|
||
gemini.Use(clientRequestID)
|
||
gemini.Use(opsErrorLogger)
|
||
gemini.Use(endpointNorm)
|
||
gemini.Use(middleware.APIKeyAuthWithSubscriptionGoogle(apiKeyService, subscriptionService, cfg))
|
||
gemini.Use(requireGroupGoogle)
|
||
{
|
||
gemini.GET("/models", h.Gateway.GeminiV1BetaListModels)
|
||
gemini.GET("/models/:model", h.Gateway.GeminiV1BetaGetModel)
|
||
// Gin treats ":" as a param marker, but Gemini uses "{model}:{action}" in the same segment.
|
||
gemini.POST("/models/*modelAction", h.Gateway.GeminiV1BetaModels)
|
||
}
|
||
|
||
// OpenAI Responses API(不带v1前缀的别名)— auto-route based on group platform
|
||
responsesHandler := func(c *gin.Context) {
|
||
if getGroupPlatform(c) == service.PlatformOpenAI {
|
||
h.OpenAIGateway.Responses(c)
|
||
return
|
||
}
|
||
h.Gateway.Responses(c)
|
||
}
|
||
r.POST("/responses", bodyLimit, clientRequestID, opsErrorLogger, endpointNorm, gin.HandlerFunc(apiKeyAuth), requireGroupAnthropic, responsesHandler)
|
||
r.POST("/responses/*subpath", bodyLimit, clientRequestID, opsErrorLogger, endpointNorm, gin.HandlerFunc(apiKeyAuth), requireGroupAnthropic, responsesHandler)
|
||
r.GET("/responses", bodyLimit, clientRequestID, opsErrorLogger, endpointNorm, gin.HandlerFunc(apiKeyAuth), requireGroupAnthropic, h.OpenAIGateway.ResponsesWebSocket)
|
||
codexDirect := r.Group("/backend-api/codex")
|
||
codexDirect.Use(bodyLimit, clientRequestID, opsErrorLogger, endpointNorm, gin.HandlerFunc(apiKeyAuth), requireGroupAnthropic)
|
||
{
|
||
codexDirect.POST("/responses", responsesHandler)
|
||
codexDirect.POST("/responses/*subpath", responsesHandler)
|
||
codexDirect.GET("/responses", h.OpenAIGateway.ResponsesWebSocket)
|
||
}
|
||
// OpenAI Chat Completions API(不带v1前缀的别名)— auto-route based on group platform
|
||
r.POST("/chat/completions", bodyLimit, clientRequestID, opsErrorLogger, endpointNorm, gin.HandlerFunc(apiKeyAuth), requireGroupAnthropic, func(c *gin.Context) {
|
||
if getGroupPlatform(c) == service.PlatformOpenAI {
|
||
h.OpenAIGateway.ChatCompletions(c)
|
||
return
|
||
}
|
||
h.Gateway.ChatCompletions(c)
|
||
})
|
||
r.POST("/images/generations", bodyLimit, clientRequestID, opsErrorLogger, endpointNorm, gin.HandlerFunc(apiKeyAuth), requireGroupAnthropic, func(c *gin.Context) {
|
||
if getGroupPlatform(c) != service.PlatformOpenAI {
|
||
c.JSON(http.StatusNotFound, gin.H{
|
||
"error": gin.H{
|
||
"type": "not_found_error",
|
||
"message": "Images API is not supported for this platform",
|
||
},
|
||
})
|
||
return
|
||
}
|
||
h.OpenAIGateway.Images(c)
|
||
})
|
||
r.POST("/images/edits", bodyLimit, clientRequestID, opsErrorLogger, endpointNorm, gin.HandlerFunc(apiKeyAuth), requireGroupAnthropic, func(c *gin.Context) {
|
||
if getGroupPlatform(c) != service.PlatformOpenAI {
|
||
c.JSON(http.StatusNotFound, gin.H{
|
||
"error": gin.H{
|
||
"type": "not_found_error",
|
||
"message": "Images API is not supported for this platform",
|
||
},
|
||
})
|
||
return
|
||
}
|
||
h.OpenAIGateway.Images(c)
|
||
})
|
||
|
||
// Antigravity 模型列表
|
||
r.GET("/antigravity/models", gin.HandlerFunc(apiKeyAuth), requireGroupAnthropic, h.Gateway.AntigravityModels)
|
||
|
||
// Antigravity 专用路由(仅使用 antigravity 账户,不混合调度)
|
||
antigravityV1 := r.Group("/antigravity/v1")
|
||
antigravityV1.Use(bodyLimit)
|
||
antigravityV1.Use(clientRequestID)
|
||
antigravityV1.Use(opsErrorLogger)
|
||
antigravityV1.Use(endpointNorm)
|
||
antigravityV1.Use(middleware.ForcePlatform(service.PlatformAntigravity))
|
||
antigravityV1.Use(gin.HandlerFunc(apiKeyAuth))
|
||
antigravityV1.Use(requireGroupAnthropic)
|
||
{
|
||
antigravityV1.POST("/messages", h.Gateway.Messages)
|
||
antigravityV1.POST("/messages/count_tokens", h.Gateway.CountTokens)
|
||
antigravityV1.GET("/models", h.Gateway.AntigravityModels)
|
||
antigravityV1.GET("/usage", h.Gateway.Usage)
|
||
}
|
||
|
||
antigravityV1Beta := r.Group("/antigravity/v1beta")
|
||
antigravityV1Beta.Use(bodyLimit)
|
||
antigravityV1Beta.Use(clientRequestID)
|
||
antigravityV1Beta.Use(opsErrorLogger)
|
||
antigravityV1Beta.Use(endpointNorm)
|
||
antigravityV1Beta.Use(middleware.ForcePlatform(service.PlatformAntigravity))
|
||
antigravityV1Beta.Use(middleware.APIKeyAuthWithSubscriptionGoogle(apiKeyService, subscriptionService, cfg))
|
||
antigravityV1Beta.Use(requireGroupGoogle)
|
||
{
|
||
antigravityV1Beta.GET("/models", h.Gateway.GeminiV1BetaListModels)
|
||
antigravityV1Beta.GET("/models/:model", h.Gateway.GeminiV1BetaGetModel)
|
||
antigravityV1Beta.POST("/models/*modelAction", h.Gateway.GeminiV1BetaModels)
|
||
}
|
||
|
||
}
|
||
|
||
// getGroupPlatform extracts the group platform from the API Key stored in context.
|
||
func getGroupPlatform(c *gin.Context) string {
|
||
apiKey, ok := middleware.GetAPIKeyFromContext(c)
|
||
if !ok || apiKey.Group == nil {
|
||
return ""
|
||
}
|
||
return apiKey.Group.Platform
|
||
}
|