refactor(backend): 拆分 Wire ProviderSet
This commit is contained in:
@@ -92,9 +92,9 @@ func createTestPayload() map[string]interface{} {
|
||||
"metadata": map[string]string{
|
||||
"user_id": generateSessionString(),
|
||||
},
|
||||
"max_tokens": 1024,
|
||||
"max_tokens": 1024,
|
||||
"temperature": 1,
|
||||
"stream": true,
|
||||
"stream": true,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -310,5 +310,5 @@ func (s *AccountTestService) sendEvent(c *gin.Context, event TestEvent) {
|
||||
func (s *AccountTestService) sendErrorAndEnd(c *gin.Context, errorMsg string) error {
|
||||
log.Printf("Account test error: %s", errorMsg)
|
||||
s.sendEvent(c, TestEvent{Type: "error", Error: errorMsg})
|
||||
return fmt.Errorf(errorMsg)
|
||||
return fmt.Errorf("%s", errorMsg)
|
||||
}
|
||||
|
||||
@@ -191,24 +191,17 @@ type adminServiceImpl struct {
|
||||
}
|
||||
|
||||
// NewAdminService creates a new AdminService
|
||||
func NewAdminService(repos *repository.Repositories) AdminService {
|
||||
func NewAdminService(repos *repository.Repositories, billingCacheService *BillingCacheService) AdminService {
|
||||
return &adminServiceImpl{
|
||||
userRepo: repos.User,
|
||||
groupRepo: repos.Group,
|
||||
accountRepo: repos.Account,
|
||||
proxyRepo: repos.Proxy,
|
||||
apiKeyRepo: repos.ApiKey,
|
||||
redeemCodeRepo: repos.RedeemCode,
|
||||
usageLogRepo: repos.UsageLog,
|
||||
userSubRepo: repos.UserSubscription,
|
||||
}
|
||||
}
|
||||
|
||||
// SetBillingCacheService 设置计费缓存服务(用于缓存失效)
|
||||
// 注意:AdminService是接口,需要类型断言
|
||||
func SetAdminServiceBillingCache(adminService AdminService, billingCacheService *BillingCacheService) {
|
||||
if impl, ok := adminService.(*adminServiceImpl); ok {
|
||||
impl.billingCacheService = billingCacheService
|
||||
userRepo: repos.User,
|
||||
groupRepo: repos.Group,
|
||||
accountRepo: repos.Account,
|
||||
proxyRepo: repos.Proxy,
|
||||
apiKeyRepo: repos.ApiKey,
|
||||
redeemCodeRepo: repos.RedeemCode,
|
||||
usageLogRepo: repos.UsageLog,
|
||||
userSubRepo: repos.UserSubscription,
|
||||
billingCacheService: billingCacheService,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,13 +16,13 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidCredentials = errors.New("invalid email or password")
|
||||
ErrUserNotActive = errors.New("user is not active")
|
||||
ErrEmailExists = errors.New("email already exists")
|
||||
ErrInvalidToken = errors.New("invalid token")
|
||||
ErrTokenExpired = errors.New("token has expired")
|
||||
ErrEmailVerifyRequired = errors.New("email verification is required")
|
||||
ErrRegDisabled = errors.New("registration is currently disabled")
|
||||
ErrInvalidCredentials = errors.New("invalid email or password")
|
||||
ErrUserNotActive = errors.New("user is not active")
|
||||
ErrEmailExists = errors.New("email already exists")
|
||||
ErrInvalidToken = errors.New("invalid token")
|
||||
ErrTokenExpired = errors.New("token has expired")
|
||||
ErrEmailVerifyRequired = errors.New("email verification is required")
|
||||
ErrRegDisabled = errors.New("registration is currently disabled")
|
||||
)
|
||||
|
||||
// JWTClaims JWT载荷数据
|
||||
@@ -44,33 +44,24 @@ type AuthService struct {
|
||||
}
|
||||
|
||||
// NewAuthService 创建认证服务实例
|
||||
func NewAuthService(userRepo *repository.UserRepository, cfg *config.Config) *AuthService {
|
||||
func NewAuthService(
|
||||
userRepo *repository.UserRepository,
|
||||
cfg *config.Config,
|
||||
settingService *SettingService,
|
||||
emailService *EmailService,
|
||||
turnstileService *TurnstileService,
|
||||
emailQueueService *EmailQueueService,
|
||||
) *AuthService {
|
||||
return &AuthService{
|
||||
userRepo: userRepo,
|
||||
cfg: cfg,
|
||||
userRepo: userRepo,
|
||||
cfg: cfg,
|
||||
settingService: settingService,
|
||||
emailService: emailService,
|
||||
turnstileService: turnstileService,
|
||||
emailQueueService: emailQueueService,
|
||||
}
|
||||
}
|
||||
|
||||
// SetSettingService 设置系统设置服务(用于检查注册开关和邮件验证)
|
||||
func (s *AuthService) SetSettingService(settingService *SettingService) {
|
||||
s.settingService = settingService
|
||||
}
|
||||
|
||||
// SetEmailService 设置邮件服务(用于邮件验证)
|
||||
func (s *AuthService) SetEmailService(emailService *EmailService) {
|
||||
s.emailService = emailService
|
||||
}
|
||||
|
||||
// SetTurnstileService 设置Turnstile服务(用于验证码校验)
|
||||
func (s *AuthService) SetTurnstileService(turnstileService *TurnstileService) {
|
||||
s.turnstileService = turnstileService
|
||||
}
|
||||
|
||||
// SetEmailQueueService 设置邮件队列服务(用于异步发送邮件)
|
||||
func (s *AuthService) SetEmailQueueService(emailQueueService *EmailQueueService) {
|
||||
s.emailQueueService = emailQueueService
|
||||
}
|
||||
|
||||
// Register 用户注册,返回token和用户
|
||||
func (s *AuthService) Register(ctx context.Context, email, password string) (string, *model.User, error) {
|
||||
return s.RegisterWithVerification(ctx, email, password, "")
|
||||
|
||||
@@ -57,20 +57,22 @@ type RedeemService struct {
|
||||
}
|
||||
|
||||
// NewRedeemService 创建兑换码服务实例
|
||||
func NewRedeemService(redeemRepo *repository.RedeemCodeRepository, userRepo *repository.UserRepository, subscriptionService *SubscriptionService, rdb *redis.Client) *RedeemService {
|
||||
func NewRedeemService(
|
||||
redeemRepo *repository.RedeemCodeRepository,
|
||||
userRepo *repository.UserRepository,
|
||||
subscriptionService *SubscriptionService,
|
||||
rdb *redis.Client,
|
||||
billingCacheService *BillingCacheService,
|
||||
) *RedeemService {
|
||||
return &RedeemService{
|
||||
redeemRepo: redeemRepo,
|
||||
userRepo: userRepo,
|
||||
subscriptionService: subscriptionService,
|
||||
rdb: rdb,
|
||||
billingCacheService: billingCacheService,
|
||||
}
|
||||
}
|
||||
|
||||
// SetBillingCacheService 设置计费缓存服务(用于缓存失效)
|
||||
func (s *RedeemService) SetBillingCacheService(billingCacheService *BillingCacheService) {
|
||||
s.billingCacheService = billingCacheService
|
||||
}
|
||||
|
||||
// GenerateRandomCode 生成随机兑换码
|
||||
func (s *RedeemService) GenerateRandomCode() (string, error) {
|
||||
// 生成16字节随机数据
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"sub2api/internal/config"
|
||||
"sub2api/internal/repository"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// Services 服务集合容器
|
||||
type Services struct {
|
||||
Auth *AuthService
|
||||
@@ -34,106 +27,3 @@ type Services struct {
|
||||
Concurrency *ConcurrencyService
|
||||
Identity *IdentityService
|
||||
}
|
||||
|
||||
// NewServices 创建所有服务实例
|
||||
func NewServices(repos *repository.Repositories, rdb *redis.Client, cfg *config.Config) *Services {
|
||||
// 初始化价格服务
|
||||
pricingService := NewPricingService(cfg)
|
||||
if err := pricingService.Initialize(); err != nil {
|
||||
// 价格服务初始化失败不应阻止启动,使用回退价格
|
||||
println("[Service] Warning: Pricing service initialization failed:", err.Error())
|
||||
}
|
||||
|
||||
// 初始化计费服务(依赖价格服务)
|
||||
billingService := NewBillingService(cfg, pricingService)
|
||||
|
||||
// 初始化其他服务
|
||||
authService := NewAuthService(repos.User, cfg)
|
||||
userService := NewUserService(repos.User, cfg)
|
||||
apiKeyService := NewApiKeyService(repos.ApiKey, repos.User, repos.Group, repos.UserSubscription, rdb, cfg)
|
||||
groupService := NewGroupService(repos.Group)
|
||||
accountService := NewAccountService(repos.Account, repos.Group)
|
||||
proxyService := NewProxyService(repos.Proxy)
|
||||
usageService := NewUsageService(repos.UsageLog, repos.User)
|
||||
|
||||
// 初始化订阅服务 (RedeemService 依赖)
|
||||
subscriptionService := NewSubscriptionService(repos)
|
||||
|
||||
// 初始化兑换服务 (依赖订阅服务)
|
||||
redeemService := NewRedeemService(repos.RedeemCode, repos.User, subscriptionService, rdb)
|
||||
|
||||
// 初始化Admin服务
|
||||
adminService := NewAdminService(repos)
|
||||
|
||||
// 初始化OAuth服务(GatewayService依赖)
|
||||
oauthService := NewOAuthService(repos.Proxy)
|
||||
|
||||
// 初始化限流服务
|
||||
rateLimitService := NewRateLimitService(repos, cfg)
|
||||
|
||||
// 初始化计费缓存服务
|
||||
billingCacheService := NewBillingCacheService(rdb, repos.User, repos.UserSubscription)
|
||||
|
||||
// 初始化账号使用量服务
|
||||
accountUsageService := NewAccountUsageService(repos, oauthService)
|
||||
|
||||
// 初始化账号测试服务
|
||||
accountTestService := NewAccountTestService(repos, oauthService)
|
||||
|
||||
// 初始化身份指纹服务
|
||||
identityService := NewIdentityService(rdb)
|
||||
|
||||
// 初始化Gateway服务
|
||||
gatewayService := NewGatewayService(repos, rdb, cfg, oauthService, billingService, rateLimitService, billingCacheService, identityService)
|
||||
|
||||
// 初始化设置服务
|
||||
settingService := NewSettingService(repos.Setting, cfg)
|
||||
emailService := NewEmailService(repos.Setting, rdb)
|
||||
|
||||
// 初始化邮件队列服务
|
||||
emailQueueService := NewEmailQueueService(emailService, 3)
|
||||
|
||||
// 初始化Turnstile服务
|
||||
turnstileService := NewTurnstileService(settingService)
|
||||
|
||||
// 设置Auth服务的依赖(用于注册开关和邮件验证)
|
||||
authService.SetSettingService(settingService)
|
||||
authService.SetEmailService(emailService)
|
||||
authService.SetTurnstileService(turnstileService)
|
||||
authService.SetEmailQueueService(emailQueueService)
|
||||
|
||||
// 初始化并发控制服务
|
||||
concurrencyService := NewConcurrencyService(rdb)
|
||||
|
||||
// 注入计费缓存服务到需要失效缓存的服务
|
||||
redeemService.SetBillingCacheService(billingCacheService)
|
||||
subscriptionService.SetBillingCacheService(billingCacheService)
|
||||
SetAdminServiceBillingCache(adminService, billingCacheService)
|
||||
|
||||
return &Services{
|
||||
Auth: authService,
|
||||
User: userService,
|
||||
ApiKey: apiKeyService,
|
||||
Group: groupService,
|
||||
Account: accountService,
|
||||
Proxy: proxyService,
|
||||
Redeem: redeemService,
|
||||
Usage: usageService,
|
||||
Pricing: pricingService,
|
||||
Billing: billingService,
|
||||
BillingCache: billingCacheService,
|
||||
Admin: adminService,
|
||||
Gateway: gatewayService,
|
||||
OAuth: oauthService,
|
||||
RateLimit: rateLimitService,
|
||||
AccountUsage: accountUsageService,
|
||||
AccountTest: accountTestService,
|
||||
Setting: settingService,
|
||||
Email: emailService,
|
||||
EmailQueue: emailQueueService,
|
||||
Turnstile: turnstileService,
|
||||
Subscription: subscriptionService,
|
||||
Concurrency: concurrencyService,
|
||||
Identity: identityService,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,13 +28,11 @@ type SubscriptionService struct {
|
||||
}
|
||||
|
||||
// NewSubscriptionService 创建订阅服务
|
||||
func NewSubscriptionService(repos *repository.Repositories) *SubscriptionService {
|
||||
return &SubscriptionService{repos: repos}
|
||||
}
|
||||
|
||||
// SetBillingCacheService 设置计费缓存服务(用于缓存失效)
|
||||
func (s *SubscriptionService) SetBillingCacheService(billingCacheService *BillingCacheService) {
|
||||
s.billingCacheService = billingCacheService
|
||||
func NewSubscriptionService(repos *repository.Repositories, billingCacheService *BillingCacheService) *SubscriptionService {
|
||||
return &SubscriptionService{
|
||||
repos: repos,
|
||||
billingCacheService: billingCacheService,
|
||||
}
|
||||
}
|
||||
|
||||
// AssignSubscriptionInput 分配订阅输入
|
||||
@@ -88,6 +86,7 @@ func (s *SubscriptionService) AssignSubscription(ctx context.Context, input *Ass
|
||||
// 如果用户已有同分组的订阅:
|
||||
// - 未过期:从当前过期时间累加天数
|
||||
// - 已过期:从当前时间开始计算新的过期时间,并激活订阅
|
||||
//
|
||||
// 如果没有订阅:创建新订阅
|
||||
func (s *SubscriptionService) AssignOrExtendSubscription(ctx context.Context, input *AssignSubscriptionInput) (*model.UserSubscription, bool, error) {
|
||||
// 检查分组是否存在且为订阅类型
|
||||
@@ -191,15 +190,15 @@ func (s *SubscriptionService) createSubscription(ctx context.Context, input *Ass
|
||||
|
||||
now := time.Now()
|
||||
sub := &model.UserSubscription{
|
||||
UserID: input.UserID,
|
||||
GroupID: input.GroupID,
|
||||
StartsAt: now,
|
||||
ExpiresAt: now.AddDate(0, 0, validityDays),
|
||||
Status: model.SubscriptionStatusActive,
|
||||
UserID: input.UserID,
|
||||
GroupID: input.GroupID,
|
||||
StartsAt: now,
|
||||
ExpiresAt: now.AddDate(0, 0, validityDays),
|
||||
Status: model.SubscriptionStatusActive,
|
||||
AssignedAt: now,
|
||||
Notes: input.Notes,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
Notes: input.Notes,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
// 只有当 AssignedBy > 0 时才设置(0 表示系统分配,如兑换码)
|
||||
if input.AssignedBy > 0 {
|
||||
@@ -225,17 +224,17 @@ type BulkAssignSubscriptionInput struct {
|
||||
|
||||
// BulkAssignResult 批量分配结果
|
||||
type BulkAssignResult struct {
|
||||
SuccessCount int
|
||||
FailedCount int
|
||||
SuccessCount int
|
||||
FailedCount int
|
||||
Subscriptions []model.UserSubscription
|
||||
Errors []string
|
||||
Errors []string
|
||||
}
|
||||
|
||||
// BulkAssignSubscription 批量分配订阅
|
||||
func (s *SubscriptionService) BulkAssignSubscription(ctx context.Context, input *BulkAssignSubscriptionInput) (*BulkAssignResult, error) {
|
||||
result := &BulkAssignResult{
|
||||
Subscriptions: make([]model.UserSubscription, 0),
|
||||
Errors: make([]string, 0),
|
||||
Errors: make([]string, 0),
|
||||
}
|
||||
|
||||
for _, userID := range input.UserIDs {
|
||||
@@ -417,10 +416,10 @@ func (s *SubscriptionService) RecordUsage(ctx context.Context, subscriptionID in
|
||||
|
||||
// SubscriptionProgress 订阅进度
|
||||
type SubscriptionProgress struct {
|
||||
ID int64 `json:"id"`
|
||||
GroupName string `json:"group_name"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
ExpiresInDays int `json:"expires_in_days"`
|
||||
ID int64 `json:"id"`
|
||||
GroupName string `json:"group_name"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
ExpiresInDays int `json:"expires_in_days"`
|
||||
Daily *UsageWindowProgress `json:"daily,omitempty"`
|
||||
Weekly *UsageWindowProgress `json:"weekly,omitempty"`
|
||||
Monthly *UsageWindowProgress `json:"monthly,omitempty"`
|
||||
@@ -428,13 +427,13 @@ type SubscriptionProgress struct {
|
||||
|
||||
// UsageWindowProgress 使用窗口进度
|
||||
type UsageWindowProgress struct {
|
||||
LimitUSD float64 `json:"limit_usd"`
|
||||
UsedUSD float64 `json:"used_usd"`
|
||||
RemainingUSD float64 `json:"remaining_usd"`
|
||||
Percentage float64 `json:"percentage"`
|
||||
WindowStart time.Time `json:"window_start"`
|
||||
ResetsAt time.Time `json:"resets_at"`
|
||||
ResetsInSeconds int64 `json:"resets_in_seconds"`
|
||||
LimitUSD float64 `json:"limit_usd"`
|
||||
UsedUSD float64 `json:"used_usd"`
|
||||
RemainingUSD float64 `json:"remaining_usd"`
|
||||
Percentage float64 `json:"percentage"`
|
||||
WindowStart time.Time `json:"window_start"`
|
||||
ResetsAt time.Time `json:"resets_at"`
|
||||
ResetsInSeconds int64 `json:"resets_in_seconds"`
|
||||
}
|
||||
|
||||
// GetSubscriptionProgress 获取订阅使用进度
|
||||
@@ -464,12 +463,12 @@ func (s *SubscriptionService) GetSubscriptionProgress(ctx context.Context, subsc
|
||||
limit := *group.DailyLimitUSD
|
||||
resetsAt := sub.DailyWindowStart.Add(24 * time.Hour)
|
||||
progress.Daily = &UsageWindowProgress{
|
||||
LimitUSD: limit,
|
||||
UsedUSD: sub.DailyUsageUSD,
|
||||
RemainingUSD: limit - sub.DailyUsageUSD,
|
||||
Percentage: (sub.DailyUsageUSD / limit) * 100,
|
||||
WindowStart: *sub.DailyWindowStart,
|
||||
ResetsAt: resetsAt,
|
||||
LimitUSD: limit,
|
||||
UsedUSD: sub.DailyUsageUSD,
|
||||
RemainingUSD: limit - sub.DailyUsageUSD,
|
||||
Percentage: (sub.DailyUsageUSD / limit) * 100,
|
||||
WindowStart: *sub.DailyWindowStart,
|
||||
ResetsAt: resetsAt,
|
||||
ResetsInSeconds: int64(time.Until(resetsAt).Seconds()),
|
||||
}
|
||||
if progress.Daily.RemainingUSD < 0 {
|
||||
@@ -488,12 +487,12 @@ func (s *SubscriptionService) GetSubscriptionProgress(ctx context.Context, subsc
|
||||
limit := *group.WeeklyLimitUSD
|
||||
resetsAt := sub.WeeklyWindowStart.Add(7 * 24 * time.Hour)
|
||||
progress.Weekly = &UsageWindowProgress{
|
||||
LimitUSD: limit,
|
||||
UsedUSD: sub.WeeklyUsageUSD,
|
||||
RemainingUSD: limit - sub.WeeklyUsageUSD,
|
||||
Percentage: (sub.WeeklyUsageUSD / limit) * 100,
|
||||
WindowStart: *sub.WeeklyWindowStart,
|
||||
ResetsAt: resetsAt,
|
||||
LimitUSD: limit,
|
||||
UsedUSD: sub.WeeklyUsageUSD,
|
||||
RemainingUSD: limit - sub.WeeklyUsageUSD,
|
||||
Percentage: (sub.WeeklyUsageUSD / limit) * 100,
|
||||
WindowStart: *sub.WeeklyWindowStart,
|
||||
ResetsAt: resetsAt,
|
||||
ResetsInSeconds: int64(time.Until(resetsAt).Seconds()),
|
||||
}
|
||||
if progress.Weekly.RemainingUSD < 0 {
|
||||
@@ -512,12 +511,12 @@ func (s *SubscriptionService) GetSubscriptionProgress(ctx context.Context, subsc
|
||||
limit := *group.MonthlyLimitUSD
|
||||
resetsAt := sub.MonthlyWindowStart.Add(30 * 24 * time.Hour)
|
||||
progress.Monthly = &UsageWindowProgress{
|
||||
LimitUSD: limit,
|
||||
UsedUSD: sub.MonthlyUsageUSD,
|
||||
RemainingUSD: limit - sub.MonthlyUsageUSD,
|
||||
Percentage: (sub.MonthlyUsageUSD / limit) * 100,
|
||||
WindowStart: *sub.MonthlyWindowStart,
|
||||
ResetsAt: resetsAt,
|
||||
LimitUSD: limit,
|
||||
UsedUSD: sub.MonthlyUsageUSD,
|
||||
RemainingUSD: limit - sub.MonthlyUsageUSD,
|
||||
Percentage: (sub.MonthlyUsageUSD / limit) * 100,
|
||||
WindowStart: *sub.MonthlyWindowStart,
|
||||
ResetsAt: resetsAt,
|
||||
ResetsInSeconds: int64(time.Until(resetsAt).Seconds()),
|
||||
}
|
||||
if progress.Monthly.RemainingUSD < 0 {
|
||||
|
||||
54
backend/internal/service/wire.go
Normal file
54
backend/internal/service/wire.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"sub2api/internal/config"
|
||||
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
// ProvidePricingService creates and initializes PricingService
|
||||
func ProvidePricingService(cfg *config.Config) (*PricingService, error) {
|
||||
svc := NewPricingService(cfg)
|
||||
if err := svc.Initialize(); err != nil {
|
||||
// 价格服务初始化失败不应阻止启动,使用回退价格
|
||||
println("[Service] Warning: Pricing service initialization failed:", err.Error())
|
||||
}
|
||||
return svc, nil
|
||||
}
|
||||
|
||||
// ProvideEmailQueueService creates EmailQueueService with default worker count
|
||||
func ProvideEmailQueueService(emailService *EmailService) *EmailQueueService {
|
||||
return NewEmailQueueService(emailService, 3)
|
||||
}
|
||||
|
||||
// ProviderSet is the Wire provider set for all services
|
||||
var ProviderSet = wire.NewSet(
|
||||
// Core services
|
||||
NewAuthService,
|
||||
NewUserService,
|
||||
NewApiKeyService,
|
||||
NewGroupService,
|
||||
NewAccountService,
|
||||
NewProxyService,
|
||||
NewRedeemService,
|
||||
NewUsageService,
|
||||
ProvidePricingService,
|
||||
NewBillingService,
|
||||
NewBillingCacheService,
|
||||
NewAdminService,
|
||||
NewGatewayService,
|
||||
NewOAuthService,
|
||||
NewRateLimitService,
|
||||
NewAccountUsageService,
|
||||
NewAccountTestService,
|
||||
NewSettingService,
|
||||
NewEmailService,
|
||||
ProvideEmailQueueService,
|
||||
NewTurnstileService,
|
||||
NewSubscriptionService,
|
||||
NewConcurrencyService,
|
||||
NewIdentityService,
|
||||
|
||||
// Provide the Services container struct
|
||||
wire.Struct(new(Services), "*"),
|
||||
)
|
||||
Reference in New Issue
Block a user