2025-12-02 17:40:39 +08:00
|
|
|
|
package container
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"carrotskin/internal/repository"
|
2025-12-02 17:50:52 +08:00
|
|
|
|
"carrotskin/internal/service"
|
2025-12-02 17:40:39 +08:00
|
|
|
|
"carrotskin/pkg/auth"
|
2026-06-15 16:40:36 +08:00
|
|
|
|
"carrotskin/pkg/config"
|
2025-12-02 22:52:33 +08:00
|
|
|
|
"carrotskin/pkg/database"
|
|
|
|
|
|
"carrotskin/pkg/email"
|
2025-12-02 17:40:39 +08:00
|
|
|
|
"carrotskin/pkg/redis"
|
|
|
|
|
|
"carrotskin/pkg/storage"
|
2026-06-15 16:40:36 +08:00
|
|
|
|
"context"
|
2025-12-02 22:52:33 +08:00
|
|
|
|
"time"
|
2025-12-02 17:40:39 +08:00
|
|
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Container 依赖注入容器
|
|
|
|
|
|
// 集中管理所有依赖,便于测试和维护
|
|
|
|
|
|
type Container struct {
|
2026-06-15 16:40:36 +08:00
|
|
|
|
// 配置
|
|
|
|
|
|
Config *config.Config
|
|
|
|
|
|
|
2025-12-02 17:40:39 +08:00
|
|
|
|
// 基础设施依赖
|
2025-12-02 22:52:33 +08:00
|
|
|
|
DB *gorm.DB
|
|
|
|
|
|
Redis *redis.Client
|
|
|
|
|
|
Logger *zap.Logger
|
|
|
|
|
|
JWT *auth.JWTService
|
2025-12-26 01:15:17 +08:00
|
|
|
|
Casbin *auth.CasbinService
|
2025-12-02 22:52:33 +08:00
|
|
|
|
Storage *storage.StorageClient
|
|
|
|
|
|
CacheManager *database.CacheManager
|
2025-12-02 17:40:39 +08:00
|
|
|
|
|
|
|
|
|
|
// Repository层
|
2025-12-02 22:52:33 +08:00
|
|
|
|
UserRepo repository.UserRepository
|
|
|
|
|
|
ProfileRepo repository.ProfileRepository
|
|
|
|
|
|
TextureRepo repository.TextureRepository
|
2025-12-03 14:43:38 +08:00
|
|
|
|
ClientRepo repository.ClientRepository
|
2025-12-02 22:52:33 +08:00
|
|
|
|
YggdrasilRepo repository.YggdrasilRepository
|
2025-12-02 17:50:52 +08:00
|
|
|
|
|
|
|
|
|
|
// Service层
|
2025-12-02 22:52:33 +08:00
|
|
|
|
UserService service.UserService
|
|
|
|
|
|
ProfileService service.ProfileService
|
|
|
|
|
|
TextureService service.TextureService
|
|
|
|
|
|
TokenService service.TokenService
|
|
|
|
|
|
YggdrasilService service.YggdrasilService
|
|
|
|
|
|
VerificationService service.VerificationService
|
|
|
|
|
|
CaptchaService service.CaptchaService
|
2025-12-03 15:27:12 +08:00
|
|
|
|
SignatureService *service.SignatureService
|
2025-12-02 17:40:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewContainer 创建依赖容器
|
|
|
|
|
|
func NewContainer(
|
2026-06-15 16:40:36 +08:00
|
|
|
|
cfg *config.Config,
|
2025-12-02 17:40:39 +08:00
|
|
|
|
db *gorm.DB,
|
|
|
|
|
|
redisClient *redis.Client,
|
|
|
|
|
|
logger *zap.Logger,
|
|
|
|
|
|
jwtService *auth.JWTService,
|
2025-12-26 01:15:17 +08:00
|
|
|
|
casbinService *auth.CasbinService,
|
2025-12-02 17:40:39 +08:00
|
|
|
|
storageClient *storage.StorageClient,
|
2026-06-15 16:40:36 +08:00
|
|
|
|
emailService *email.Service,
|
2025-12-02 17:40:39 +08:00
|
|
|
|
) *Container {
|
2025-12-02 22:52:33 +08:00
|
|
|
|
// 创建缓存管理器
|
|
|
|
|
|
cacheManager := database.NewCacheManager(redisClient, database.CacheConfig{
|
|
|
|
|
|
Prefix: "carrotskin:",
|
|
|
|
|
|
Expiration: 5 * time.Minute,
|
|
|
|
|
|
Enabled: true,
|
2025-12-24 16:03:46 +08:00
|
|
|
|
Policy: database.CachePolicy{
|
|
|
|
|
|
UserTTL: 5 * time.Minute,
|
|
|
|
|
|
UserEmailTTL: 5 * time.Minute,
|
|
|
|
|
|
ProfileTTL: 5 * time.Minute,
|
|
|
|
|
|
ProfileListTTL: 3 * time.Minute,
|
|
|
|
|
|
TextureTTL: 5 * time.Minute,
|
|
|
|
|
|
TextureListTTL: 2 * time.Minute,
|
|
|
|
|
|
},
|
2025-12-02 22:52:33 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
2025-12-02 17:40:39 +08:00
|
|
|
|
c := &Container{
|
2026-06-15 16:40:36 +08:00
|
|
|
|
Config: cfg,
|
2025-12-02 22:52:33 +08:00
|
|
|
|
DB: db,
|
|
|
|
|
|
Redis: redisClient,
|
|
|
|
|
|
Logger: logger,
|
|
|
|
|
|
JWT: jwtService,
|
2025-12-26 01:15:17 +08:00
|
|
|
|
Casbin: casbinService,
|
2025-12-02 22:52:33 +08:00
|
|
|
|
Storage: storageClient,
|
|
|
|
|
|
CacheManager: cacheManager,
|
2025-12-02 17:40:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化Repository
|
|
|
|
|
|
c.UserRepo = repository.NewUserRepository(db)
|
|
|
|
|
|
c.ProfileRepo = repository.NewProfileRepository(db)
|
|
|
|
|
|
c.TextureRepo = repository.NewTextureRepository(db)
|
2025-12-03 14:43:38 +08:00
|
|
|
|
c.ClientRepo = repository.NewClientRepository(db)
|
2025-12-02 22:52:33 +08:00
|
|
|
|
c.YggdrasilRepo = repository.NewYggdrasilRepository(db)
|
2025-12-02 17:40:39 +08:00
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
|
// 初始化SignatureService(作为依赖注入,避免在容器中创建并立即调用)
|
|
|
|
|
|
// 将SignatureService添加到容器中,供其他服务使用
|
|
|
|
|
|
c.SignatureService = service.NewSignatureService(c.ProfileRepo, redisClient, logger)
|
2025-12-03 14:43:38 +08:00
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
|
// 初始化Service(注入缓存管理器)
|
2026-06-15 16:40:36 +08:00
|
|
|
|
c.UserService = service.NewUserService(cfg, c.UserRepo, jwtService, redisClient, cacheManager, storageClient, logger)
|
2025-12-02 22:52:33 +08:00
|
|
|
|
c.ProfileService = service.NewProfileService(c.ProfileRepo, c.UserRepo, cacheManager, logger)
|
2026-06-15 16:40:36 +08:00
|
|
|
|
c.TextureService = service.NewTextureService(c.TextureRepo, c.UserRepo, storageClient, cacheManager, logger, db)
|
2025-12-03 15:27:12 +08:00
|
|
|
|
|
|
|
|
|
|
// 获取Yggdrasil私钥并创建JWT服务(TokenService需要)
|
|
|
|
|
|
// 注意:这里仍然需要预先初始化,因为TokenService在创建时需要YggdrasilJWT
|
|
|
|
|
|
// 但SignatureService已经作为依赖注入,降低了耦合度
|
2026-06-15 16:40:36 +08:00
|
|
|
|
// 使用后台 ctx(启动阶段不依赖请求 ctx)
|
|
|
|
|
|
initCtx, cancelInit := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
|
|
_, privateKey, err := c.SignatureService.GetOrCreateYggdrasilKeyPair(initCtx)
|
|
|
|
|
|
cancelInit()
|
2025-12-03 15:27:12 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
logger.Fatal("获取Yggdrasil私钥失败", zap.Error(err))
|
|
|
|
|
|
}
|
|
|
|
|
|
yggdrasilJWT := auth.NewYggdrasilJWTService(privateKey, "carrotskin")
|
2025-12-24 16:03:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 创建Redis Token存储(必须使用Redis,包括miniredis回退)
|
|
|
|
|
|
if redisClient == nil {
|
|
|
|
|
|
logger.Fatal("Redis客户端未初始化,无法创建Token服务")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tokenStore := auth.NewTokenStoreRedis(
|
|
|
|
|
|
redisClient,
|
|
|
|
|
|
logger,
|
|
|
|
|
|
auth.WithKeyPrefix("token:"),
|
|
|
|
|
|
auth.WithDefaultTTL(24*time.Hour),
|
|
|
|
|
|
auth.WithStaleTTL(30*24*time.Hour),
|
|
|
|
|
|
auth.WithMaxTokensPerUser(10),
|
|
|
|
|
|
)
|
|
|
|
|
|
c.TokenService = service.NewTokenServiceRedis(tokenStore, c.ClientRepo, c.ProfileRepo, yggdrasilJWT, logger)
|
2025-12-02 17:50:52 +08:00
|
|
|
|
|
2025-12-03 10:58:39 +08:00
|
|
|
|
// 使用组合服务(内部包含认证、会话、序列化、证书服务)
|
2026-06-15 16:40:36 +08:00
|
|
|
|
c.YggdrasilService = service.NewYggdrasilServiceComposite(c.UserRepo, c.ProfileRepo, c.YggdrasilRepo, c.TextureRepo, c.SignatureService, redisClient, logger, c.TokenService)
|
2025-12-02 22:52:33 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化其他服务
|
2026-06-15 16:40:36 +08:00
|
|
|
|
c.CaptchaService = service.NewCaptchaService(cfg, redisClient, logger)
|
2025-12-02 22:52:33 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化VerificationService(需要email.Service)
|
|
|
|
|
|
if emailService != nil {
|
2026-06-15 16:40:36 +08:00
|
|
|
|
c.VerificationService = service.NewVerificationService(cfg, redisClient, emailService)
|
2025-12-02 22:52:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 17:40:39 +08:00
|
|
|
|
return c
|
|
|
|
|
|
}
|