新增功能: - 新增 Sora 账号管理和 OAuth 认证 - 新增 Sora 视频/图片生成 API 网关 - 新增 Sora 任务调度和缓存机制 - 新增 Sora 使用统计和计费支持 - 前端增加 Sora 平台配置界面 安全修复(代码审核): - [SEC-001] 限制媒体下载响应体大小(图片 20MB、视频 200MB),防止 DoS 攻击 - [SEC-002] 限制 SDK API 响应大小(1MB),防止内存耗尽 - [SEC-003] 修复 SSRF 风险,添加 URL 验证并强制使用代理配置 BUG 修复(代码审核): - [BUG-001] 修复 for 循环内 defer 累积导致的资源泄漏 - [BUG-002] 修复图片并发槽位获取失败时已持有锁未释放的永久泄漏 性能优化(代码审核): - [PERF-001] 添加 Sentinel Token 缓存(3 分钟有效期),减少 PoW 计算开销 技术细节: - 使用 io.LimitReader 限制所有外部输入的大小 - 添加 urlvalidator 验证防止 SSRF 攻击 - 使用 sync.Map 实现线程安全的包级缓存 - 优化并发槽位管理,添加 releaseAll 模式防止泄漏 影响范围: - 后端:新增 Sora 相关数据模型、服务、网关和管理接口 - 前端:新增 Sora 平台配置、账号管理和监控界面 - 配置:新增 Sora 相关配置项和环境变量 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
209 lines
4.9 KiB
Go
209 lines
4.9 KiB
Go
//go:build wireinject
|
|
// +build wireinject
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/ent"
|
|
"github.com/Wei-Shaw/sub2api/internal/config"
|
|
"github.com/Wei-Shaw/sub2api/internal/handler"
|
|
"github.com/Wei-Shaw/sub2api/internal/repository"
|
|
"github.com/Wei-Shaw/sub2api/internal/server"
|
|
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
|
|
"github.com/google/wire"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type Application struct {
|
|
Server *http.Server
|
|
Cleanup func()
|
|
}
|
|
|
|
func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
|
wire.Build(
|
|
// Infrastructure layer ProviderSets
|
|
config.ProviderSet,
|
|
|
|
// Business layer ProviderSets
|
|
repository.ProviderSet,
|
|
service.ProviderSet,
|
|
middleware.ProviderSet,
|
|
handler.ProviderSet,
|
|
|
|
// Server layer ProviderSet
|
|
server.ProviderSet,
|
|
|
|
// BuildInfo provider
|
|
provideServiceBuildInfo,
|
|
|
|
// Cleanup function provider
|
|
provideCleanup,
|
|
|
|
// Application struct
|
|
wire.Struct(new(Application), "Server", "Cleanup"),
|
|
)
|
|
return nil, nil
|
|
}
|
|
|
|
func provideServiceBuildInfo(buildInfo handler.BuildInfo) service.BuildInfo {
|
|
return service.BuildInfo{
|
|
Version: buildInfo.Version,
|
|
BuildType: buildInfo.BuildType,
|
|
}
|
|
}
|
|
|
|
func provideCleanup(
|
|
entClient *ent.Client,
|
|
rdb *redis.Client,
|
|
opsMetricsCollector *service.OpsMetricsCollector,
|
|
opsAggregation *service.OpsAggregationService,
|
|
opsAlertEvaluator *service.OpsAlertEvaluatorService,
|
|
opsCleanup *service.OpsCleanupService,
|
|
opsScheduledReport *service.OpsScheduledReportService,
|
|
schedulerSnapshot *service.SchedulerSnapshotService,
|
|
tokenRefresh *service.TokenRefreshService,
|
|
soraTokenRefresh *service.SoraTokenRefreshService,
|
|
soraCacheCleanup *service.SoraCacheCleanupService,
|
|
accountExpiry *service.AccountExpiryService,
|
|
usageCleanup *service.UsageCleanupService,
|
|
pricing *service.PricingService,
|
|
emailQueue *service.EmailQueueService,
|
|
billingCache *service.BillingCacheService,
|
|
oauth *service.OAuthService,
|
|
openaiOAuth *service.OpenAIOAuthService,
|
|
geminiOAuth *service.GeminiOAuthService,
|
|
antigravityOAuth *service.AntigravityOAuthService,
|
|
) func() {
|
|
return func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
// Cleanup steps in reverse dependency order
|
|
cleanupSteps := []struct {
|
|
name string
|
|
fn func() error
|
|
}{
|
|
{"OpsScheduledReportService", func() error {
|
|
if opsScheduledReport != nil {
|
|
opsScheduledReport.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"OpsCleanupService", func() error {
|
|
if opsCleanup != nil {
|
|
opsCleanup.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"OpsAlertEvaluatorService", func() error {
|
|
if opsAlertEvaluator != nil {
|
|
opsAlertEvaluator.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"OpsAggregationService", func() error {
|
|
if opsAggregation != nil {
|
|
opsAggregation.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"OpsMetricsCollector", func() error {
|
|
if opsMetricsCollector != nil {
|
|
opsMetricsCollector.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"SchedulerSnapshotService", func() error {
|
|
if schedulerSnapshot != nil {
|
|
schedulerSnapshot.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"UsageCleanupService", func() error {
|
|
if usageCleanup != nil {
|
|
usageCleanup.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"TokenRefreshService", func() error {
|
|
tokenRefresh.Stop()
|
|
return nil
|
|
}},
|
|
{"SoraTokenRefreshService", func() error {
|
|
if soraTokenRefresh != nil {
|
|
soraTokenRefresh.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"SoraCacheCleanupService", func() error {
|
|
if soraCacheCleanup != nil {
|
|
soraCacheCleanup.Stop()
|
|
}
|
|
return nil
|
|
}},
|
|
{"AccountExpiryService", func() error {
|
|
accountExpiry.Stop()
|
|
return nil
|
|
}},
|
|
{"PricingService", func() error {
|
|
pricing.Stop()
|
|
return nil
|
|
}},
|
|
{"EmailQueueService", func() error {
|
|
emailQueue.Stop()
|
|
return nil
|
|
}},
|
|
{"BillingCacheService", func() error {
|
|
billingCache.Stop()
|
|
return nil
|
|
}},
|
|
{"OAuthService", func() error {
|
|
oauth.Stop()
|
|
return nil
|
|
}},
|
|
{"OpenAIOAuthService", func() error {
|
|
openaiOAuth.Stop()
|
|
return nil
|
|
}},
|
|
{"GeminiOAuthService", func() error {
|
|
geminiOAuth.Stop()
|
|
return nil
|
|
}},
|
|
{"AntigravityOAuthService", func() error {
|
|
antigravityOAuth.Stop()
|
|
return nil
|
|
}},
|
|
{"Redis", func() error {
|
|
return rdb.Close()
|
|
}},
|
|
{"Ent", func() error {
|
|
return entClient.Close()
|
|
}},
|
|
}
|
|
|
|
for _, step := range cleanupSteps {
|
|
if err := step.fn(); err != nil {
|
|
log.Printf("[Cleanup] %s failed: %v", step.name, err)
|
|
// Continue with remaining cleanup steps even if one fails
|
|
} else {
|
|
log.Printf("[Cleanup] %s succeeded", step.name)
|
|
}
|
|
}
|
|
|
|
// Check if context timed out
|
|
select {
|
|
case <-ctx.Done():
|
|
log.Printf("[Cleanup] Warning: cleanup timed out after 10 seconds")
|
|
default:
|
|
log.Printf("[Cleanup] All cleanup steps completed")
|
|
}
|
|
}
|
|
}
|