2025-12-26 10:42:08 +08:00
|
|
|
|
package routes
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-12-29 03:17:25 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
2025-12-26 10:42:08 +08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/handler"
|
|
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
2025-12-26 00:17:55 -08:00
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
2025-12-26 10:42:08 +08:00
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-26 00:17:55 -08:00
|
|
|
|
// RegisterGatewayRoutes 注册 API 网关路由(Claude/OpenAI/Gemini 兼容)
|
2025-12-26 10:42:08 +08:00
|
|
|
|
func RegisterGatewayRoutes(
|
|
|
|
|
|
r *gin.Engine,
|
|
|
|
|
|
h *handler.Handlers,
|
2026-01-04 19:27:53 +08:00
|
|
|
|
apiKeyAuth middleware.APIKeyAuthMiddleware,
|
|
|
|
|
|
apiKeyService *service.APIKeyService,
|
2025-12-26 00:17:55 -08:00
|
|
|
|
subscriptionService *service.SubscriptionService,
|
2026-01-09 20:55:12 +08:00
|
|
|
|
opsService *service.OpsService,
|
2025-12-29 03:17:25 +08:00
|
|
|
|
cfg *config.Config,
|
2025-12-26 10:42:08 +08:00
|
|
|
|
) {
|
2025-12-31 08:50:12 +08:00
|
|
|
|
bodyLimit := middleware.RequestBodyLimit(cfg.Gateway.MaxBodySize)
|
2026-01-09 20:55:12 +08:00
|
|
|
|
clientRequestID := middleware.ClientRequestID()
|
|
|
|
|
|
opsErrorLogger := handler.OpsErrorLoggerMiddleware(opsService)
|
2025-12-31 08:50:12 +08:00
|
|
|
|
|
2025-12-26 10:42:08 +08:00
|
|
|
|
// API网关(Claude API兼容)
|
|
|
|
|
|
gateway := r.Group("/v1")
|
2025-12-31 08:50:12 +08:00
|
|
|
|
gateway.Use(bodyLimit)
|
2026-01-09 20:55:12 +08:00
|
|
|
|
gateway.Use(clientRequestID)
|
|
|
|
|
|
gateway.Use(opsErrorLogger)
|
2025-12-26 10:42:08 +08:00
|
|
|
|
gateway.Use(gin.HandlerFunc(apiKeyAuth))
|
|
|
|
|
|
{
|
|
|
|
|
|
gateway.POST("/messages", h.Gateway.Messages)
|
|
|
|
|
|
gateway.POST("/messages/count_tokens", h.Gateway.CountTokens)
|
|
|
|
|
|
gateway.GET("/models", h.Gateway.Models)
|
|
|
|
|
|
gateway.GET("/usage", h.Gateway.Usage)
|
|
|
|
|
|
// OpenAI Responses API
|
|
|
|
|
|
gateway.POST("/responses", h.OpenAIGateway.Responses)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 00:17:55 -08:00
|
|
|
|
// Gemini 原生 API 兼容层(Gemini SDK/CLI 直连)
|
|
|
|
|
|
gemini := r.Group("/v1beta")
|
2025-12-31 08:50:12 +08:00
|
|
|
|
gemini.Use(bodyLimit)
|
2026-01-09 20:55:12 +08:00
|
|
|
|
gemini.Use(clientRequestID)
|
|
|
|
|
|
gemini.Use(opsErrorLogger)
|
2026-01-04 19:27:53 +08:00
|
|
|
|
gemini.Use(middleware.APIKeyAuthWithSubscriptionGoogle(apiKeyService, subscriptionService, cfg))
|
2025-12-26 00:17:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-26 10:42:08 +08:00
|
|
|
|
// OpenAI Responses API(不带v1前缀的别名)
|
2026-01-09 20:55:12 +08:00
|
|
|
|
r.POST("/responses", bodyLimit, clientRequestID, opsErrorLogger, gin.HandlerFunc(apiKeyAuth), h.OpenAIGateway.Responses)
|
2025-12-29 16:52:55 +08:00
|
|
|
|
|
2026-01-03 06:29:02 -08:00
|
|
|
|
// Antigravity 模型列表
|
|
|
|
|
|
r.GET("/antigravity/models", gin.HandlerFunc(apiKeyAuth), h.Gateway.AntigravityModels)
|
|
|
|
|
|
|
2025-12-29 16:52:55 +08:00
|
|
|
|
// Antigravity 专用路由(仅使用 antigravity 账户,不混合调度)
|
|
|
|
|
|
antigravityV1 := r.Group("/antigravity/v1")
|
2025-12-31 08:50:12 +08:00
|
|
|
|
antigravityV1.Use(bodyLimit)
|
2026-01-09 20:55:12 +08:00
|
|
|
|
antigravityV1.Use(clientRequestID)
|
|
|
|
|
|
antigravityV1.Use(opsErrorLogger)
|
2025-12-29 16:52:55 +08:00
|
|
|
|
antigravityV1.Use(middleware.ForcePlatform(service.PlatformAntigravity))
|
|
|
|
|
|
antigravityV1.Use(gin.HandlerFunc(apiKeyAuth))
|
|
|
|
|
|
{
|
|
|
|
|
|
antigravityV1.POST("/messages", h.Gateway.Messages)
|
|
|
|
|
|
antigravityV1.POST("/messages/count_tokens", h.Gateway.CountTokens)
|
2026-01-03 06:29:02 -08:00
|
|
|
|
antigravityV1.GET("/models", h.Gateway.AntigravityModels)
|
2025-12-29 16:52:55 +08:00
|
|
|
|
antigravityV1.GET("/usage", h.Gateway.Usage)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
antigravityV1Beta := r.Group("/antigravity/v1beta")
|
2025-12-31 08:50:12 +08:00
|
|
|
|
antigravityV1Beta.Use(bodyLimit)
|
2026-01-09 20:55:12 +08:00
|
|
|
|
antigravityV1Beta.Use(clientRequestID)
|
|
|
|
|
|
antigravityV1Beta.Use(opsErrorLogger)
|
2025-12-29 16:52:55 +08:00
|
|
|
|
antigravityV1Beta.Use(middleware.ForcePlatform(service.PlatformAntigravity))
|
2026-01-04 19:27:53 +08:00
|
|
|
|
antigravityV1Beta.Use(middleware.APIKeyAuthWithSubscriptionGoogle(apiKeyService, subscriptionService, cfg))
|
2025-12-29 16:52:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
antigravityV1Beta.GET("/models", h.Gateway.GeminiV1BetaListModels)
|
|
|
|
|
|
antigravityV1Beta.GET("/models/:model", h.Gateway.GeminiV1BetaGetModel)
|
|
|
|
|
|
antigravityV1Beta.POST("/models/*modelAction", h.Gateway.GeminiV1BetaModels)
|
|
|
|
|
|
}
|
2025-12-26 10:42:08 +08:00
|
|
|
|
}
|