Files
backend/internal/wire/service.go
lan c561a0bb0c feat(grpc): add gRPC server infrastructure and schedule sync service
Add gRPC server support with configurable TLS, environment-based settings, and Wire injection. Implement schedule synchronization service with task runner integration for external course data fetching.

- Add gRPC configuration with env var overrides (APP_GRPC_ENABLED, APP_GRPC_PORT, etc.)
- Create gRPC server infrastructure with runner task management
- Implement ScheduleSyncService for course data synchronization
- Add sync endpoint to schedule handler for external system integration
- Update repository with context-aware batch delete operation
- Wire all dependencies including zap logger for structured logging
2026-03-13 20:40:20 +08:00

202 lines
5.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package wire
import (
"carrot_bbs/internal/cache"
"carrot_bbs/internal/config"
"carrot_bbs/internal/grpc/runner"
"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"
"go.uber.org/zap"
"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,
ProvideScheduleSyncService,
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)
}
// ProvideScheduleSyncService 提供课表同步服务
func ProvideScheduleSyncService(
taskManager *runner.TaskManager,
scheduleRepo repository.ScheduleRepository,
logger *zap.Logger,
) service.ScheduleSyncService {
return service.NewScheduleSyncService(taskManager, scheduleRepo, logger)
}
// 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)
}