From eb55947ec4134f24ec79e1af899923739158bd34 Mon Sep 17 00:00:00 2001 From: shaw Date: Tue, 23 Dec 2025 10:25:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dgolangci-lint=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除OpenAIGatewayHandler中未使用的userService字段 - 将账号类型判断的if-else链改为switch语句 --- backend/cmd/server/wire_gen.go | 2 +- .../internal/handler/openai_gateway_handler.go | 3 --- .../internal/service/openai_gateway_service.go | 15 +++++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/backend/cmd/server/wire_gen.go b/backend/cmd/server/wire_gen.go index 8759839e..e7097442 100644 --- a/backend/cmd/server/wire_gen.go +++ b/backend/cmd/server/wire_gen.go @@ -111,7 +111,7 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) { concurrencyService := service.NewConcurrencyService(concurrencyCache) gatewayHandler := handler.NewGatewayHandler(gatewayService, userService, concurrencyService, billingCacheService) openAIGatewayService := service.NewOpenAIGatewayService(accountRepository, usageLogRepository, userRepository, userSubscriptionRepository, gatewayCache, configConfig, billingService, rateLimitService, billingCacheService, httpUpstream) - openAIGatewayHandler := handler.NewOpenAIGatewayHandler(openAIGatewayService, userService, concurrencyService, billingCacheService) + openAIGatewayHandler := handler.NewOpenAIGatewayHandler(openAIGatewayService, concurrencyService, billingCacheService) handlerSettingHandler := handler.ProvideSettingHandler(settingService, buildInfo) handlers := handler.ProvideHandlers(authHandler, userHandler, apiKeyHandler, usageHandler, redeemHandler, subscriptionHandler, adminHandlers, gatewayHandler, openAIGatewayHandler, handlerSettingHandler) groupService := service.NewGroupService(groupRepository) diff --git a/backend/internal/handler/openai_gateway_handler.go b/backend/internal/handler/openai_gateway_handler.go index ce34a5cf..9b011a14 100644 --- a/backend/internal/handler/openai_gateway_handler.go +++ b/backend/internal/handler/openai_gateway_handler.go @@ -19,7 +19,6 @@ import ( // OpenAIGatewayHandler handles OpenAI API gateway requests type OpenAIGatewayHandler struct { gatewayService *service.OpenAIGatewayService - userService *service.UserService billingCacheService *service.BillingCacheService concurrencyHelper *ConcurrencyHelper } @@ -27,13 +26,11 @@ type OpenAIGatewayHandler struct { // NewOpenAIGatewayHandler creates a new OpenAIGatewayHandler func NewOpenAIGatewayHandler( gatewayService *service.OpenAIGatewayService, - userService *service.UserService, concurrencyService *service.ConcurrencyService, billingCacheService *service.BillingCacheService, ) *OpenAIGatewayHandler { return &OpenAIGatewayHandler{ gatewayService: gatewayService, - userService: userService, billingCacheService: billingCacheService, concurrencyHelper: NewConcurrencyHelper(concurrencyService, SSEPingFormatNone), } diff --git a/backend/internal/service/openai_gateway_service.go b/backend/internal/service/openai_gateway_service.go index 45f3b235..08ce2593 100644 --- a/backend/internal/service/openai_gateway_service.go +++ b/backend/internal/service/openai_gateway_service.go @@ -179,20 +179,22 @@ func (s *OpenAIGatewayService) SelectAccountForModel(ctx context.Context, groupI // GetAccessToken gets the access token for an OpenAI account func (s *OpenAIGatewayService) GetAccessToken(ctx context.Context, account *model.Account) (string, string, error) { - if account.Type == model.AccountTypeOAuth { + switch account.Type { + case model.AccountTypeOAuth: accessToken := account.GetOpenAIAccessToken() if accessToken == "" { return "", "", errors.New("access_token not found in credentials") } return accessToken, "oauth", nil - } else if account.Type == model.AccountTypeApiKey { + case model.AccountTypeApiKey: apiKey := account.GetOpenAIApiKey() if apiKey == "" { return "", "", errors.New("api_key not found in credentials") } return apiKey, "apikey", nil + default: + return "", "", fmt.Errorf("unsupported account type: %s", account.Type) } - return "", "", fmt.Errorf("unsupported account type: %s", account.Type) } // Forward forwards request to OpenAI API @@ -295,10 +297,11 @@ func (s *OpenAIGatewayService) Forward(ctx context.Context, c *gin.Context, acco func (s *OpenAIGatewayService) buildUpstreamRequest(ctx context.Context, c *gin.Context, account *model.Account, body []byte, token string, isStream bool) (*http.Request, error) { // Determine target URL based on account type var targetURL string - if account.Type == model.AccountTypeOAuth { + switch account.Type { + case model.AccountTypeOAuth: // OAuth accounts use ChatGPT internal API targetURL = chatgptCodexURL - } else if account.Type == model.AccountTypeApiKey { + case model.AccountTypeApiKey: // API Key accounts use Platform API or custom base URL baseURL := account.GetOpenAIBaseURL() if baseURL != "" { @@ -306,7 +309,7 @@ func (s *OpenAIGatewayService) buildUpstreamRequest(ctx context.Context, c *gin. } else { targetURL = openaiPlatformAPIURL } - } else { + default: targetURL = openaiPlatformAPIURL }