refactor: introduce Wire dependency injection and interface-based architecture
- Add Google Wire for compile-time dependency injection - Replace concrete service types with interfaces across handlers - Remove global state (DB, cache) in favor of constructor injection - Split monolithic files into focused modules: - config: separate files for each config domain - dto: converters split by domain (user, post, message, group) - cache: separate metrics.go and redis_cache.go - Introduce unified apperrors package with string-based error codes - Add transaction support with context-aware repository methods - Upgrade golang.org/x/crypto from 0.17.0 to 0.26.0
This commit is contained in:
189
internal/wire/service.go
Normal file
189
internal/wire/service.go
Normal file
@@ -0,0 +1,189 @@
|
||||
package wire
|
||||
|
||||
import (
|
||||
"carrot_bbs/internal/cache"
|
||||
"carrot_bbs/internal/config"
|
||||
"carrot_bbs/internal/pkg/email"
|
||||
"carrot_bbs/internal/pkg/gorse"
|
||||
"carrot_bbs/internal/pkg/openai"
|
||||
"carrot_bbs/internal/pkg/s3"
|
||||
"carrot_bbs/internal/pkg/sse"
|
||||
"carrot_bbs/internal/repository"
|
||||
"carrot_bbs/internal/service"
|
||||
|
||||
"github.com/google/wire"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ServiceSet Service 层 Provider Set
|
||||
var ServiceSet = wire.NewSet(
|
||||
// JWT 服务(需要配置参数)
|
||||
ProvideJWTService,
|
||||
|
||||
// 基础服务
|
||||
ProvideEmailService,
|
||||
ProvidePostAIService,
|
||||
|
||||
// 核心服务(按依赖顺序)
|
||||
ProvidePushService,
|
||||
ProvideSystemMessageService,
|
||||
ProvidePostService,
|
||||
ProvideCommentService,
|
||||
ProvideMessageService,
|
||||
ProvideNotificationService,
|
||||
ProvideUserService,
|
||||
ProvideStickerService,
|
||||
ProvideVoteService,
|
||||
ProvideChatService,
|
||||
ProvideScheduleService,
|
||||
ProvideGroupService,
|
||||
ProvideUploadService,
|
||||
)
|
||||
|
||||
// ProvideJWTService 提供 JWT 服务
|
||||
func ProvideJWTService(cfg *config.Config) *service.JWTService {
|
||||
return service.NewJWTService(
|
||||
cfg.JWT.Secret,
|
||||
int64(cfg.JWT.AccessTokenExpire.Seconds()),
|
||||
int64(cfg.JWT.RefreshTokenExpire.Seconds()),
|
||||
)
|
||||
}
|
||||
|
||||
// ProvideEmailService 提供邮件服务
|
||||
func ProvideEmailService(client email.Client) service.EmailService {
|
||||
return service.NewEmailService(client)
|
||||
}
|
||||
|
||||
// ProvidePostAIService 提供 AI 服务
|
||||
func ProvidePostAIService(client openai.Client) *service.PostAIService {
|
||||
return service.NewPostAIService(client)
|
||||
}
|
||||
|
||||
// ProvidePushService 提供推送服务
|
||||
func ProvidePushService(
|
||||
pushRepo *repository.PushRecordRepository,
|
||||
deviceTokenRepo *repository.DeviceTokenRepository,
|
||||
messageRepo *repository.MessageRepository,
|
||||
sseHub *sse.Hub,
|
||||
) service.PushService {
|
||||
return service.NewPushService(pushRepo, deviceTokenRepo, messageRepo, sseHub)
|
||||
}
|
||||
|
||||
// ProvideSystemMessageService 提供系统消息服务
|
||||
func ProvideSystemMessageService(
|
||||
notifyRepo *repository.SystemNotificationRepository,
|
||||
pushService service.PushService,
|
||||
userRepo *repository.UserRepository,
|
||||
postRepo *repository.PostRepository,
|
||||
cacheBackend cache.Cache,
|
||||
) service.SystemMessageService {
|
||||
return service.NewSystemMessageService(notifyRepo, pushService, userRepo, postRepo, cacheBackend)
|
||||
}
|
||||
|
||||
// ProvidePostService 提供帖子服务
|
||||
func ProvidePostService(
|
||||
postRepo *repository.PostRepository,
|
||||
systemMessageService service.SystemMessageService,
|
||||
gorseClient gorse.Client,
|
||||
postAIService *service.PostAIService,
|
||||
cacheBackend cache.Cache,
|
||||
txManager repository.TransactionManager,
|
||||
) service.PostService {
|
||||
return service.NewPostService(postRepo, systemMessageService, gorseClient, postAIService, cacheBackend, txManager)
|
||||
}
|
||||
|
||||
// ProvideCommentService 提供评论服务
|
||||
func ProvideCommentService(
|
||||
commentRepo *repository.CommentRepository,
|
||||
postRepo *repository.PostRepository,
|
||||
systemMessageService service.SystemMessageService,
|
||||
gorseClient gorse.Client,
|
||||
postAIService *service.PostAIService,
|
||||
cacheBackend cache.Cache,
|
||||
) *service.CommentService {
|
||||
return service.NewCommentService(commentRepo, postRepo, systemMessageService, gorseClient, postAIService, cacheBackend)
|
||||
}
|
||||
|
||||
// ProvideMessageService 提供消息服务
|
||||
func ProvideMessageService(
|
||||
db *gorm.DB,
|
||||
messageRepo *repository.MessageRepository,
|
||||
cacheBackend cache.Cache,
|
||||
) *service.MessageService {
|
||||
return service.NewMessageService(db, messageRepo, cacheBackend)
|
||||
}
|
||||
|
||||
// ProvideNotificationService 提供通知服务
|
||||
func ProvideNotificationService(
|
||||
notificationRepo *repository.NotificationRepository,
|
||||
cacheBackend cache.Cache,
|
||||
) *service.NotificationService {
|
||||
return service.NewNotificationService(notificationRepo, cacheBackend)
|
||||
}
|
||||
|
||||
// ProvideUserService 提供用户服务
|
||||
func ProvideUserService(
|
||||
userRepo *repository.UserRepository,
|
||||
systemMessageService service.SystemMessageService,
|
||||
emailService service.EmailService,
|
||||
cacheBackend cache.Cache,
|
||||
) service.UserService {
|
||||
return service.NewUserService(userRepo, systemMessageService, emailService, cacheBackend)
|
||||
}
|
||||
|
||||
// ProvideStickerService 提供表情服务
|
||||
func ProvideStickerService(
|
||||
stickerRepo repository.StickerRepository,
|
||||
) service.StickerService {
|
||||
return service.NewStickerService(stickerRepo)
|
||||
}
|
||||
|
||||
// ProvideVoteService 提供投票服务
|
||||
func ProvideVoteService(
|
||||
voteRepo *repository.VoteRepository,
|
||||
postRepo *repository.PostRepository,
|
||||
cache cache.Cache,
|
||||
postAIService *service.PostAIService,
|
||||
systemMessageService service.SystemMessageService,
|
||||
) *service.VoteService {
|
||||
return service.NewVoteService(voteRepo, postRepo, cache, postAIService, systemMessageService)
|
||||
}
|
||||
|
||||
// ProvideChatService 提供聊天服务
|
||||
// Note: sensitiveService 传 nil,与 main.go 保持一致
|
||||
func ProvideChatService(
|
||||
db *gorm.DB,
|
||||
messageRepo *repository.MessageRepository,
|
||||
userRepo *repository.UserRepository,
|
||||
sseHub *sse.Hub,
|
||||
cacheBackend cache.Cache,
|
||||
) service.ChatService {
|
||||
return service.NewChatService(db, messageRepo, userRepo, nil, sseHub, cacheBackend)
|
||||
}
|
||||
|
||||
// ProvideScheduleService 提供日程服务
|
||||
func ProvideScheduleService(
|
||||
scheduleRepo repository.ScheduleRepository,
|
||||
) service.ScheduleService {
|
||||
return service.NewScheduleService(scheduleRepo)
|
||||
}
|
||||
|
||||
// ProvideGroupService 提供群组服务
|
||||
func ProvideGroupService(
|
||||
db *gorm.DB,
|
||||
groupRepo repository.GroupRepository,
|
||||
userRepo *repository.UserRepository,
|
||||
messageRepo *repository.MessageRepository,
|
||||
sseHub *sse.Hub,
|
||||
cacheBackend cache.Cache,
|
||||
) service.GroupService {
|
||||
return service.NewGroupService(db, groupRepo, userRepo, messageRepo, sseHub, cacheBackend)
|
||||
}
|
||||
|
||||
// ProvideUploadService 提供上传服务
|
||||
func ProvideUploadService(
|
||||
s3Client *s3.Client,
|
||||
userService service.UserService,
|
||||
) *service.UploadService {
|
||||
return service.NewUploadService(s3Client, userService)
|
||||
}
|
||||
Reference in New Issue
Block a user