This repository has been archived on 2026-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
Files
MetaLab/internal/router/deps_extra.go
Victor_Jay ca21ae7216 fix: 修复头像审核旧图裂开问题 + CSP 配置修复
- 头像提交审核不再删除旧文件,审核通过后才删除旧头像文件
- 头像审核被拒时删除已上传的新头像文件,保留旧头像
- CSP style-src 移除 nonce 以启用 unsafe-inline(JS 动态样式需要)
- CSP script-src 移除 nonce 改用 unsafe-inline + unsafe-eval(Vditor 编辑器和动态样式需要)
2026-06-02 22:26:01 +08:00

133 lines
5.4 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 router
import (
"time"
"metazone.cc/metalab/internal/cache"
"metazone.cc/metalab/internal/config"
"metazone.cc/metalab/internal/controller"
adminCtrl "metazone.cc/metalab/internal/controller/admin"
"metazone.cc/metalab/internal/middleware"
"metazone.cc/metalab/internal/repository"
"metazone.cc/metalab/internal/service"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
func buildDeps(db *gorm.DB, cfg *config.Config, siteSettings *config.SiteSettings, redisClient *redis.Client) *dependencies {
userRepo, sessionMgr, authService, rateLimiter, authCtrl, avatarSvc, authMdw := buildDepsCore(db, cfg, siteSettings, redisClient)
auditRepo := repository.NewAuditRepo(db)
notificationRepo := repository.NewNotificationRepo(db)
notificationService := service.NewNotificationService(notificationRepo)
// 域能系统
energyRepo := repository.NewEnergyRepo(db)
fundRepo := repository.NewFundRepo(db)
energyRepo.SetFundStore(fundRepo) // 为跨仓库 Transaction 注入 FundStore
energyService := service.NewEnergyService(energyRepo, nil, notificationService)
energyService.SetFundRepo(fundRepo) // 只读查询
// 等级/签到/任务系统(链式注入域能服务)
_, levelSvc, levelCtrl := buildDepsLevel(db, notificationService, authMdw)
levelSvc.WithEnergyService(energyService)
// EnergyService 也需要 LevelService用于赋能获得经验通过 setter 注入
energyService.SetExpAdder(levelSvc)
auditService := service.NewAuditService(auditRepo, userRepo, siteSettings, cfg.Audit, notificationService, levelSvc)
auditService.WithEnergyRefundService(energyService)
auditService.WithAvatarFileCleaner(avatarSvc)
auditController := adminCtrl.NewAuditController(auditService, userRepo)
messageController := controller.NewMessageController(notificationService)
settingsCtrl := controller.NewSettingsController(authService, avatarSvc, auditService, sessionMgr, cfg, levelSvc)
settingsCtrl.WithEnergyService(energyService)
settingsCtrl.SetNotifyPrefReader(authService)
siteSettingController := adminCtrl.NewSiteSettingController(
service.NewSiteSettingService(siteSettings),
&service.AuditDefaults{
Enabled: cfg.Audit.Enabled, UsernameAudit: cfg.Audit.UsernameAudit,
AvatarAudit: cfg.Audit.AvatarAudit, BioAudit: cfg.Audit.BioAudit,
},
)
postRepo := repository.NewPostRepo(db)
postService := service.NewPostService(postRepo, siteSettings, notificationService, userRepo)
shortcodeSvc := service.NewShortcodeService()
postCtrl := controller.NewPostController(postService, shortcodeSvc)
adminPostCtrl := adminCtrl.NewAdminPostController(postService)
adminController := adminCtrl.NewAdminController(service.NewAdminService(userRepo, postRepo, sessionMgr))
studioCtrl := controller.NewStudioController(postService)
studioCtrl.WithEnergyService(energyService)
// 域能控制器
energyCtrl := controller.NewEnergyController(energyService)
adminEnergyCtrl := adminCtrl.NewAdminEnergyController(energyService)
// 评论系统
commentRepo := repository.NewCommentRepo(db)
commentService := service.NewCommentService(commentRepo, notificationService)
commentCtrl := controller.NewCommentController(commentService)
// 后台评论管理
adminCommentCtrl := adminCtrl.NewAdminCommentController(commentService)
// 赞/踩系统
reactionRepo := repository.NewReactionRepo(db)
reactionService := service.NewReactionService(reactionRepo)
reactionCtrl := controller.NewReactionController(reactionService)
// 关注系统
followRepo := repository.NewFollowRepo(db)
followService := service.NewFollowService(followRepo)
followService.SetNotifier(notificationService) // 关注通知
followCtrl := controller.NewFollowController(followService)
// 收藏夹系统(需在 SpaceController 之前初始化)
favoriteRepo := repository.NewFavoriteRepo(db)
favoriteService := service.NewFavoriteService(favoriteRepo)
favoriteCtrl := controller.NewFavoriteController(favoriteService)
// 用户空间
spaceService := service.NewSpaceService(userRepo, postRepo)
spaceCtrl := controller.NewSpaceController(spaceService)
spaceCtrl.WithFollowService(followService)
spaceCtrl.WithFavoriteService(favoriteService)
// Studio 趋势图
trendsService := service.NewTrendsService(energyRepo, commentRepo, reactionRepo, favoriteRepo)
studioCtrl.WithTrendsService(trendsService)
maintenanceMdw := middleware.NewMaintenanceMiddleware(cfg, siteSettings, sessionMgr)
// 首页内存缓存30s TTL文章变更时失效
homeCache := cache.NewHomePageCache(30 * time.Second)
postService.WithHomePageInvalidator(func() { homeCache.Invalidate() })
return &dependencies{
authCtrl: authCtrl, authMdw: authMdw, rateLimiter: rateLimiter, maintenanceMdw: maintenanceMdw,
settingsCtrl: settingsCtrl,
messageCtrl: messageController, postCtrl: postCtrl, postService: postService, spaceCtrl: spaceCtrl,
studioCtrl: studioCtrl,
adminPostCtrl: adminPostCtrl,
adminCtrl: adminController, auditCtrl: auditController,
siteSettingCtrl: siteSettingController,
levelCtrl: levelCtrl,
energyCtrl: energyCtrl,
adminEnergyCtrl: adminEnergyCtrl,
energyService: energyService,
commentCtrl: commentCtrl,
commentService: commentService,
adminCommentCtrl: adminCommentCtrl,
reactionCtrl: reactionCtrl,
followCtrl: followCtrl,
favoriteCtrl: favoriteCtrl,
favoriteService: favoriteService,
trendsService: trendsService,
homeCache: homeCache,
}
}