|
|
|
|
@@ -19,6 +19,7 @@ import (
|
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@@ -47,7 +48,8 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|
|
|
|
redisClient := repository.ProvideRedis(configConfig)
|
|
|
|
|
refreshTokenCache := repository.NewRefreshTokenCache(redisClient)
|
|
|
|
|
settingRepository := repository.NewSettingRepository(client)
|
|
|
|
|
settingService := service.NewSettingService(settingRepository, configConfig)
|
|
|
|
|
groupRepository := repository.NewGroupRepository(client, db)
|
|
|
|
|
settingService := service.ProvideSettingService(settingRepository, groupRepository, configConfig)
|
|
|
|
|
emailCache := repository.NewEmailCache(redisClient)
|
|
|
|
|
emailService := service.NewEmailService(settingRepository, emailCache)
|
|
|
|
|
turnstileVerifier := repository.NewTurnstileVerifier()
|
|
|
|
|
@@ -58,15 +60,14 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|
|
|
|
userSubscriptionRepository := repository.NewUserSubscriptionRepository(client)
|
|
|
|
|
billingCacheService := service.NewBillingCacheService(billingCache, userRepository, userSubscriptionRepository, configConfig)
|
|
|
|
|
apiKeyRepository := repository.NewAPIKeyRepository(client)
|
|
|
|
|
groupRepository := repository.NewGroupRepository(client, db)
|
|
|
|
|
userGroupRateRepository := repository.NewUserGroupRateRepository(db)
|
|
|
|
|
apiKeyCache := repository.NewAPIKeyCache(redisClient)
|
|
|
|
|
apiKeyService := service.NewAPIKeyService(apiKeyRepository, userRepository, groupRepository, userSubscriptionRepository, userGroupRateRepository, apiKeyCache, configConfig)
|
|
|
|
|
apiKeyAuthCacheInvalidator := service.ProvideAPIKeyAuthCacheInvalidator(apiKeyService)
|
|
|
|
|
promoService := service.NewPromoService(promoCodeRepository, userRepository, billingCacheService, client, apiKeyAuthCacheInvalidator)
|
|
|
|
|
authService := service.NewAuthService(userRepository, redeemCodeRepository, refreshTokenCache, configConfig, settingService, emailService, turnstileService, emailQueueService, promoService)
|
|
|
|
|
userService := service.NewUserService(userRepository, apiKeyAuthCacheInvalidator, billingCache)
|
|
|
|
|
subscriptionService := service.NewSubscriptionService(groupRepository, userSubscriptionRepository, billingCacheService, client, configConfig)
|
|
|
|
|
authService := service.NewAuthService(userRepository, redeemCodeRepository, refreshTokenCache, configConfig, settingService, emailService, turnstileService, emailQueueService, promoService, subscriptionService)
|
|
|
|
|
userService := service.NewUserService(userRepository, apiKeyAuthCacheInvalidator, billingCache)
|
|
|
|
|
redeemCache := repository.NewRedeemCache(redisClient)
|
|
|
|
|
redeemService := service.NewRedeemService(redeemCodeRepository, userRepository, subscriptionService, redeemCache, billingCacheService, client, apiKeyAuthCacheInvalidator)
|
|
|
|
|
secretEncryptor, err := repository.NewAESEncryptor(configConfig)
|
|
|
|
|
@@ -102,7 +103,7 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|
|
|
|
proxyRepository := repository.NewProxyRepository(client, db)
|
|
|
|
|
proxyExitInfoProber := repository.NewProxyExitInfoProber(configConfig)
|
|
|
|
|
proxyLatencyCache := repository.NewProxyLatencyCache(redisClient)
|
|
|
|
|
adminService := service.NewAdminService(userRepository, groupRepository, accountRepository, soraAccountRepository, proxyRepository, apiKeyRepository, redeemCodeRepository, userGroupRateRepository, billingCacheService, proxyExitInfoProber, proxyLatencyCache, apiKeyAuthCacheInvalidator)
|
|
|
|
|
adminService := service.NewAdminService(userRepository, groupRepository, accountRepository, soraAccountRepository, proxyRepository, apiKeyRepository, redeemCodeRepository, userGroupRateRepository, billingCacheService, proxyExitInfoProber, proxyLatencyCache, apiKeyAuthCacheInvalidator, client, settingService, subscriptionService)
|
|
|
|
|
concurrencyCache := repository.ProvideConcurrencyCache(redisClient, configConfig)
|
|
|
|
|
concurrencyService := service.ProvideConcurrencyService(concurrencyCache, accountRepository, configConfig)
|
|
|
|
|
adminUserHandler := admin.NewUserHandler(adminService, concurrencyService)
|
|
|
|
|
@@ -113,7 +114,8 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|
|
|
|
openAIOAuthService := service.NewOpenAIOAuthService(proxyRepository, openAIOAuthClient)
|
|
|
|
|
geminiOAuthClient := repository.NewGeminiOAuthClient(configConfig)
|
|
|
|
|
geminiCliCodeAssistClient := repository.NewGeminiCliCodeAssistClient()
|
|
|
|
|
geminiOAuthService := service.NewGeminiOAuthService(proxyRepository, geminiOAuthClient, geminiCliCodeAssistClient, configConfig)
|
|
|
|
|
driveClient := repository.NewGeminiDriveClient()
|
|
|
|
|
geminiOAuthService := service.NewGeminiOAuthService(proxyRepository, geminiOAuthClient, geminiCliCodeAssistClient, driveClient, configConfig)
|
|
|
|
|
antigravityOAuthService := service.NewAntigravityOAuthService(proxyRepository)
|
|
|
|
|
geminiQuotaService := service.NewGeminiQuotaService(configConfig, settingRepository)
|
|
|
|
|
tempUnschedCache := repository.NewTempUnschedCache(redisClient)
|
|
|
|
|
@@ -136,14 +138,17 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|
|
|
|
accountTestService := service.NewAccountTestService(accountRepository, geminiTokenProvider, antigravityGatewayService, httpUpstream, configConfig)
|
|
|
|
|
crsSyncService := service.NewCRSSyncService(accountRepository, proxyRepository, oAuthService, openAIOAuthService, geminiOAuthService, configConfig)
|
|
|
|
|
sessionLimitCache := repository.ProvideSessionLimitCache(redisClient, configConfig)
|
|
|
|
|
accountHandler := admin.NewAccountHandler(adminService, oAuthService, openAIOAuthService, geminiOAuthService, antigravityOAuthService, rateLimitService, accountUsageService, accountTestService, concurrencyService, crsSyncService, sessionLimitCache, compositeTokenCacheInvalidator)
|
|
|
|
|
rpmCache := repository.NewRPMCache(redisClient)
|
|
|
|
|
accountHandler := admin.NewAccountHandler(adminService, oAuthService, openAIOAuthService, geminiOAuthService, antigravityOAuthService, rateLimitService, accountUsageService, accountTestService, concurrencyService, crsSyncService, sessionLimitCache, rpmCache, compositeTokenCacheInvalidator)
|
|
|
|
|
adminAnnouncementHandler := admin.NewAnnouncementHandler(announcementService)
|
|
|
|
|
dataManagementService := service.NewDataManagementService()
|
|
|
|
|
dataManagementHandler := admin.NewDataManagementHandler(dataManagementService)
|
|
|
|
|
oAuthHandler := admin.NewOAuthHandler(oAuthService)
|
|
|
|
|
openAIOAuthHandler := admin.NewOpenAIOAuthHandler(openAIOAuthService, adminService)
|
|
|
|
|
geminiOAuthHandler := admin.NewGeminiOAuthHandler(geminiOAuthService)
|
|
|
|
|
antigravityOAuthHandler := admin.NewAntigravityOAuthHandler(antigravityOAuthService)
|
|
|
|
|
proxyHandler := admin.NewProxyHandler(adminService)
|
|
|
|
|
adminRedeemHandler := admin.NewRedeemHandler(adminService)
|
|
|
|
|
adminRedeemHandler := admin.NewRedeemHandler(adminService, redeemService)
|
|
|
|
|
promoHandler := admin.NewPromoHandler(promoService)
|
|
|
|
|
opsRepository := repository.NewOpsRepository(db)
|
|
|
|
|
pricingRemoteClient := repository.ProvidePricingRemoteClient(configConfig)
|
|
|
|
|
@@ -156,13 +161,18 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|
|
|
|
deferredService := service.ProvideDeferredService(accountRepository, timingWheelService)
|
|
|
|
|
claudeTokenProvider := service.NewClaudeTokenProvider(accountRepository, geminiTokenCache, oAuthService)
|
|
|
|
|
digestSessionStore := service.NewDigestSessionStore()
|
|
|
|
|
gatewayService := service.NewGatewayService(accountRepository, groupRepository, usageLogRepository, userRepository, userSubscriptionRepository, userGroupRateRepository, gatewayCache, configConfig, schedulerSnapshotService, concurrencyService, billingService, rateLimitService, billingCacheService, identityService, httpUpstream, deferredService, claudeTokenProvider, sessionLimitCache, digestSessionStore)
|
|
|
|
|
gatewayService := service.NewGatewayService(accountRepository, groupRepository, usageLogRepository, userRepository, userSubscriptionRepository, userGroupRateRepository, gatewayCache, configConfig, schedulerSnapshotService, concurrencyService, billingService, rateLimitService, billingCacheService, identityService, httpUpstream, deferredService, claudeTokenProvider, sessionLimitCache, rpmCache, digestSessionStore)
|
|
|
|
|
openAITokenProvider := service.NewOpenAITokenProvider(accountRepository, geminiTokenCache, openAIOAuthService)
|
|
|
|
|
openAIGatewayService := service.NewOpenAIGatewayService(accountRepository, usageLogRepository, userRepository, userSubscriptionRepository, gatewayCache, configConfig, schedulerSnapshotService, concurrencyService, billingService, rateLimitService, billingCacheService, httpUpstream, deferredService, openAITokenProvider)
|
|
|
|
|
geminiMessagesCompatService := service.NewGeminiMessagesCompatService(accountRepository, groupRepository, gatewayCache, schedulerSnapshotService, geminiTokenProvider, rateLimitService, httpUpstream, antigravityGatewayService, configConfig)
|
|
|
|
|
opsSystemLogSink := service.ProvideOpsSystemLogSink(opsRepository)
|
|
|
|
|
opsService := service.NewOpsService(opsRepository, settingRepository, configConfig, accountRepository, userRepository, concurrencyService, gatewayService, openAIGatewayService, geminiMessagesCompatService, antigravityGatewayService, opsSystemLogSink)
|
|
|
|
|
settingHandler := admin.NewSettingHandler(settingService, emailService, turnstileService, opsService)
|
|
|
|
|
soraS3Storage := service.NewSoraS3Storage(settingService)
|
|
|
|
|
settingService.SetOnS3UpdateCallback(soraS3Storage.RefreshClient)
|
|
|
|
|
soraGenerationRepository := repository.NewSoraGenerationRepository(db)
|
|
|
|
|
soraQuotaService := service.NewSoraQuotaService(userRepository, groupRepository, settingService)
|
|
|
|
|
soraGenerationService := service.NewSoraGenerationService(soraGenerationRepository, soraS3Storage, soraQuotaService)
|
|
|
|
|
settingHandler := admin.NewSettingHandler(settingService, emailService, turnstileService, opsService, soraS3Storage)
|
|
|
|
|
opsHandler := admin.NewOpsHandler(opsService)
|
|
|
|
|
updateCache := repository.NewUpdateCache(redisClient)
|
|
|
|
|
gitHubReleaseClient := repository.ProvideGitHubReleaseClient(configConfig)
|
|
|
|
|
@@ -183,19 +193,21 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|
|
|
|
errorPassthroughCache := repository.NewErrorPassthroughCache(redisClient)
|
|
|
|
|
errorPassthroughService := service.NewErrorPassthroughService(errorPassthroughRepository, errorPassthroughCache)
|
|
|
|
|
errorPassthroughHandler := admin.NewErrorPassthroughHandler(errorPassthroughService)
|
|
|
|
|
adminHandlers := handler.ProvideAdminHandlers(dashboardHandler, adminUserHandler, groupHandler, accountHandler, adminAnnouncementHandler, oAuthHandler, openAIOAuthHandler, geminiOAuthHandler, antigravityOAuthHandler, proxyHandler, adminRedeemHandler, promoHandler, settingHandler, opsHandler, systemHandler, adminSubscriptionHandler, adminUsageHandler, userAttributeHandler, errorPassthroughHandler)
|
|
|
|
|
adminAPIKeyHandler := admin.NewAdminAPIKeyHandler(adminService)
|
|
|
|
|
adminHandlers := handler.ProvideAdminHandlers(dashboardHandler, adminUserHandler, groupHandler, accountHandler, adminAnnouncementHandler, dataManagementHandler, oAuthHandler, openAIOAuthHandler, geminiOAuthHandler, antigravityOAuthHandler, proxyHandler, adminRedeemHandler, promoHandler, settingHandler, opsHandler, systemHandler, adminSubscriptionHandler, adminUsageHandler, userAttributeHandler, errorPassthroughHandler, adminAPIKeyHandler)
|
|
|
|
|
usageRecordWorkerPool := service.NewUsageRecordWorkerPool(configConfig)
|
|
|
|
|
gatewayHandler := handler.NewGatewayHandler(gatewayService, geminiMessagesCompatService, antigravityGatewayService, userService, concurrencyService, billingCacheService, usageService, apiKeyService, usageRecordWorkerPool, errorPassthroughService, configConfig)
|
|
|
|
|
gatewayHandler := handler.NewGatewayHandler(gatewayService, geminiMessagesCompatService, antigravityGatewayService, userService, concurrencyService, billingCacheService, usageService, apiKeyService, usageRecordWorkerPool, errorPassthroughService, configConfig, settingService)
|
|
|
|
|
openAIGatewayHandler := handler.NewOpenAIGatewayHandler(openAIGatewayService, concurrencyService, billingCacheService, apiKeyService, usageRecordWorkerPool, errorPassthroughService, configConfig)
|
|
|
|
|
soraSDKClient := service.ProvideSoraSDKClient(configConfig, httpUpstream, openAITokenProvider, accountRepository, soraAccountRepository)
|
|
|
|
|
soraMediaStorage := service.ProvideSoraMediaStorage(configConfig)
|
|
|
|
|
soraGatewayService := service.NewSoraGatewayService(soraSDKClient, soraMediaStorage, rateLimitService, configConfig)
|
|
|
|
|
soraGatewayService := service.NewSoraGatewayService(soraSDKClient, rateLimitService, httpUpstream, configConfig)
|
|
|
|
|
soraClientHandler := handler.NewSoraClientHandler(soraGenerationService, soraQuotaService, soraS3Storage, soraGatewayService, gatewayService, soraMediaStorage, apiKeyService)
|
|
|
|
|
soraGatewayHandler := handler.NewSoraGatewayHandler(gatewayService, soraGatewayService, concurrencyService, billingCacheService, usageRecordWorkerPool, configConfig)
|
|
|
|
|
handlerSettingHandler := handler.ProvideSettingHandler(settingService, buildInfo)
|
|
|
|
|
totpHandler := handler.NewTotpHandler(totpService)
|
|
|
|
|
idempotencyCoordinator := service.ProvideIdempotencyCoordinator(idempotencyRepository, configConfig)
|
|
|
|
|
idempotencyCleanupService := service.ProvideIdempotencyCleanupService(idempotencyRepository, configConfig)
|
|
|
|
|
handlers := handler.ProvideHandlers(authHandler, userHandler, apiKeyHandler, usageHandler, redeemHandler, subscriptionHandler, announcementHandler, adminHandlers, gatewayHandler, openAIGatewayHandler, soraGatewayHandler, handlerSettingHandler, totpHandler, idempotencyCoordinator, idempotencyCleanupService)
|
|
|
|
|
handlers := handler.ProvideHandlers(authHandler, userHandler, apiKeyHandler, usageHandler, redeemHandler, subscriptionHandler, announcementHandler, adminHandlers, gatewayHandler, openAIGatewayHandler, soraGatewayHandler, soraClientHandler, handlerSettingHandler, totpHandler, idempotencyCoordinator, idempotencyCleanupService)
|
|
|
|
|
jwtAuthMiddleware := middleware.NewJWTAuthMiddleware(authService, userService)
|
|
|
|
|
adminAuthMiddleware := middleware.NewAdminAuthMiddleware(authService, userService, settingService)
|
|
|
|
|
apiKeyAuthMiddleware := middleware.NewAPIKeyAuthMiddleware(apiKeyService, subscriptionService, configConfig)
|
|
|
|
|
@@ -210,7 +222,7 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|
|
|
|
tokenRefreshService := service.ProvideTokenRefreshService(accountRepository, soraAccountRepository, oAuthService, openAIOAuthService, geminiOAuthService, antigravityOAuthService, compositeTokenCacheInvalidator, schedulerCache, configConfig)
|
|
|
|
|
accountExpiryService := service.ProvideAccountExpiryService(accountRepository)
|
|
|
|
|
subscriptionExpiryService := service.ProvideSubscriptionExpiryService(userSubscriptionRepository)
|
|
|
|
|
v := provideCleanup(client, redisClient, opsMetricsCollector, opsAggregationService, opsAlertEvaluatorService, opsCleanupService, opsScheduledReportService, opsSystemLogSink, soraMediaCleanupService, schedulerSnapshotService, tokenRefreshService, accountExpiryService, subscriptionExpiryService, usageCleanupService, idempotencyCleanupService, pricingService, emailQueueService, billingCacheService, usageRecordWorkerPool, subscriptionService, oAuthService, openAIOAuthService, geminiOAuthService, antigravityOAuthService)
|
|
|
|
|
v := provideCleanup(client, redisClient, opsMetricsCollector, opsAggregationService, opsAlertEvaluatorService, opsCleanupService, opsScheduledReportService, opsSystemLogSink, soraMediaCleanupService, schedulerSnapshotService, tokenRefreshService, accountExpiryService, subscriptionExpiryService, usageCleanupService, idempotencyCleanupService, pricingService, emailQueueService, billingCacheService, usageRecordWorkerPool, subscriptionService, oAuthService, openAIOAuthService, geminiOAuthService, antigravityOAuthService, openAIGatewayService)
|
|
|
|
|
application := &Application{
|
|
|
|
|
Server: httpServer,
|
|
|
|
|
Cleanup: v,
|
|
|
|
|
@@ -257,15 +269,18 @@ func provideCleanup(
|
|
|
|
|
openaiOAuth *service.OpenAIOAuthService,
|
|
|
|
|
geminiOAuth *service.GeminiOAuthService,
|
|
|
|
|
antigravityOAuth *service.AntigravityOAuthService,
|
|
|
|
|
openAIGateway *service.OpenAIGatewayService,
|
|
|
|
|
) func() {
|
|
|
|
|
return func() {
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
cleanupSteps := []struct {
|
|
|
|
|
type cleanupStep struct {
|
|
|
|
|
name string
|
|
|
|
|
fn func() error
|
|
|
|
|
}{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parallelSteps := []cleanupStep{
|
|
|
|
|
{"OpsScheduledReportService", func() error {
|
|
|
|
|
if opsScheduledReport != nil {
|
|
|
|
|
opsScheduledReport.Stop()
|
|
|
|
|
@@ -378,23 +393,60 @@ func provideCleanup(
|
|
|
|
|
antigravityOAuth.Stop()
|
|
|
|
|
return nil
|
|
|
|
|
}},
|
|
|
|
|
{"OpenAIWSPool", func() error {
|
|
|
|
|
if openAIGateway != nil {
|
|
|
|
|
openAIGateway.CloseOpenAIWSPool()
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
infraSteps := []cleanupStep{
|
|
|
|
|
{"Redis", func() error {
|
|
|
|
|
if rdb == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return rdb.Close()
|
|
|
|
|
}},
|
|
|
|
|
{"Ent", func() error {
|
|
|
|
|
if entClient == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return entClient.Close()
|
|
|
|
|
}},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, step := range cleanupSteps {
|
|
|
|
|
if err := step.fn(); err != nil {
|
|
|
|
|
log.Printf("[Cleanup] %s failed: %v", step.name, err)
|
|
|
|
|
runParallel := func(steps []cleanupStep) {
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
for i := range steps {
|
|
|
|
|
step := steps[i]
|
|
|
|
|
wg.Add(1)
|
|
|
|
|
go func() {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
if err := step.fn(); err != nil {
|
|
|
|
|
log.Printf("[Cleanup] %s failed: %v", step.name, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
log.Printf("[Cleanup] %s succeeded", step.name)
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
wg.Wait()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
runSequential := func(steps []cleanupStep) {
|
|
|
|
|
for i := range steps {
|
|
|
|
|
step := steps[i]
|
|
|
|
|
if err := step.fn(); err != nil {
|
|
|
|
|
log.Printf("[Cleanup] %s failed: %v", step.name, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
log.Printf("[Cleanup] %s succeeded", step.name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runParallel(parallelSteps)
|
|
|
|
|
runSequential(infraSteps)
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
log.Printf("[Cleanup] Warning: cleanup timed out after 10 seconds")
|
|
|
|
|
|