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"
|
|
|
|
|
|
"carrotskin/pkg/redis"
|
|
|
|
|
|
"carrotskin/pkg/storage"
|
|
|
|
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Container 依赖注入容器
|
|
|
|
|
|
// 集中管理所有依赖,便于测试和维护
|
|
|
|
|
|
type Container struct {
|
|
|
|
|
|
// 基础设施依赖
|
|
|
|
|
|
DB *gorm.DB
|
|
|
|
|
|
Redis *redis.Client
|
|
|
|
|
|
Logger *zap.Logger
|
|
|
|
|
|
JWT *auth.JWTService
|
|
|
|
|
|
Storage *storage.StorageClient
|
|
|
|
|
|
|
|
|
|
|
|
// Repository层
|
2025-12-02 17:46:00 +08:00
|
|
|
|
UserRepo repository.UserRepository
|
|
|
|
|
|
ProfileRepo repository.ProfileRepository
|
|
|
|
|
|
TextureRepo repository.TextureRepository
|
|
|
|
|
|
TokenRepo repository.TokenRepository
|
|
|
|
|
|
ConfigRepo repository.SystemConfigRepository
|
2025-12-02 17:50:52 +08:00
|
|
|
|
|
|
|
|
|
|
// Service层
|
|
|
|
|
|
UserService service.UserService
|
|
|
|
|
|
ProfileService service.ProfileService
|
|
|
|
|
|
TextureService service.TextureService
|
|
|
|
|
|
TokenService service.TokenService
|
2025-12-02 17:40:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewContainer 创建依赖容器
|
|
|
|
|
|
func NewContainer(
|
|
|
|
|
|
db *gorm.DB,
|
|
|
|
|
|
redisClient *redis.Client,
|
|
|
|
|
|
logger *zap.Logger,
|
|
|
|
|
|
jwtService *auth.JWTService,
|
|
|
|
|
|
storageClient *storage.StorageClient,
|
|
|
|
|
|
) *Container {
|
|
|
|
|
|
c := &Container{
|
|
|
|
|
|
DB: db,
|
|
|
|
|
|
Redis: redisClient,
|
|
|
|
|
|
Logger: logger,
|
|
|
|
|
|
JWT: jwtService,
|
|
|
|
|
|
Storage: storageClient,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化Repository
|
|
|
|
|
|
c.UserRepo = repository.NewUserRepository(db)
|
|
|
|
|
|
c.ProfileRepo = repository.NewProfileRepository(db)
|
|
|
|
|
|
c.TextureRepo = repository.NewTextureRepository(db)
|
|
|
|
|
|
c.TokenRepo = repository.NewTokenRepository(db)
|
|
|
|
|
|
c.ConfigRepo = repository.NewSystemConfigRepository(db)
|
|
|
|
|
|
|
2025-12-02 17:50:52 +08:00
|
|
|
|
// 初始化Service
|
|
|
|
|
|
c.UserService = service.NewUserService(c.UserRepo, c.ConfigRepo, jwtService, redisClient, logger)
|
|
|
|
|
|
c.ProfileService = service.NewProfileService(c.ProfileRepo, c.UserRepo, logger)
|
|
|
|
|
|
c.TextureService = service.NewTextureService(c.TextureRepo, c.UserRepo, logger)
|
|
|
|
|
|
c.TokenService = service.NewTokenService(c.TokenRepo, c.ProfileRepo, logger)
|
|
|
|
|
|
|
2025-12-02 17:40:39 +08:00
|
|
|
|
return c
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewTestContainer 创建测试用容器(可注入mock依赖)
|
|
|
|
|
|
func NewTestContainer(opts ...Option) *Container {
|
|
|
|
|
|
c := &Container{}
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
|
opt(c)
|
|
|
|
|
|
}
|
|
|
|
|
|
return c
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Option 容器配置选项
|
|
|
|
|
|
type Option func(*Container)
|
|
|
|
|
|
|
|
|
|
|
|
// WithDB 设置数据库连接
|
|
|
|
|
|
func WithDB(db *gorm.DB) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.DB = db
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithRedis 设置Redis客户端
|
|
|
|
|
|
func WithRedis(redis *redis.Client) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.Redis = redis
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithLogger 设置日志
|
|
|
|
|
|
func WithLogger(logger *zap.Logger) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.Logger = logger
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithJWT 设置JWT服务
|
|
|
|
|
|
func WithJWT(jwt *auth.JWTService) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.JWT = jwt
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithStorage 设置存储客户端
|
|
|
|
|
|
func WithStorage(storage *storage.StorageClient) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.Storage = storage
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithUserRepo 设置用户仓储
|
|
|
|
|
|
func WithUserRepo(repo repository.UserRepository) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.UserRepo = repo
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithProfileRepo 设置档案仓储
|
|
|
|
|
|
func WithProfileRepo(repo repository.ProfileRepository) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.ProfileRepo = repo
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithTextureRepo 设置材质仓储
|
|
|
|
|
|
func WithTextureRepo(repo repository.TextureRepository) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.TextureRepo = repo
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithTokenRepo 设置令牌仓储
|
|
|
|
|
|
func WithTokenRepo(repo repository.TokenRepository) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.TokenRepo = repo
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithConfigRepo 设置系统配置仓储
|
|
|
|
|
|
func WithConfigRepo(repo repository.SystemConfigRepository) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.ConfigRepo = repo
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-02 17:50:52 +08:00
|
|
|
|
|
|
|
|
|
|
// WithUserService 设置用户服务
|
|
|
|
|
|
func WithUserService(svc service.UserService) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.UserService = svc
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithProfileService 设置档案服务
|
|
|
|
|
|
func WithProfileService(svc service.ProfileService) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.ProfileService = svc
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithTextureService 设置材质服务
|
|
|
|
|
|
func WithTextureService(svc service.TextureService) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.TextureService = svc
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithTokenService 设置令牌服务
|
|
|
|
|
|
func WithTokenService(svc service.TokenService) Option {
|
|
|
|
|
|
return func(c *Container) {
|
|
|
|
|
|
c.TokenService = svc
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|