Files
backend/internal/wire/casbin.go
lafay 2f584c1f2c
All checks were successful
Build Backend / build (push) Successful in 5m10s
Build Backend / build-docker (push) Successful in 2m22s
This is a breaking change that renames the entire project from "Carrot BBS" to "WithYou".
The Go module name has been changed from `carrot_bbs` to `with_you`, which requires
all import paths to be updated throughout the codebase and by external consumers.

BREAKING CHANGE: Module name changed from carrot_bbs to with_you. All imports
and dependencies must be updated from carrot_bbs/* to with_you/*.
2026-04-22 16:01:59 +08:00

66 lines
1.6 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 (
"with_you/internal/cache"
"with_you/internal/config"
"with_you/internal/repository"
"with_you/internal/service"
"github.com/casbin/casbin/v3"
gormadapter "github.com/casbin/gorm-adapter/v3"
"github.com/google/wire"
"go.uber.org/zap"
"gorm.io/gorm"
)
// CasbinSet Casbin 相关 Provider Set
var CasbinSet = wire.NewSet(
ProvideCasbinEnforcer,
ProvideCasbinService,
ProvideRoleRepository,
)
// ProvideCasbinEnforcer 提供 Casbin Enforcer
func ProvideCasbinEnforcer(cfg *config.Config, db *gorm.DB) *casbin.Enforcer {
// 创建 GORM 适配器,连接数据库
adapter, err := gormadapter.NewAdapterByDB(db)
if err != nil {
zap.L().Fatal("Failed to create Casbin GORM adapter",
zap.Error(err),
)
}
// 创建 Casbin Enforcer使用数据库适配器
enforcer, err := casbin.NewEnforcer(cfg.Casbin.ModelPath, adapter)
if err != nil {
zap.L().Fatal("Failed to create Casbin enforcer",
zap.Error(err),
)
}
// 从数据库加载策略
if err := enforcer.LoadPolicy(); err != nil {
zap.L().Warn("Failed to load Casbin policy",
zap.Error(err),
)
}
zap.L().Info("Initialized Casbin enforcer with GORM adapter")
return enforcer
}
// ProvideCasbinService 提供 Casbin 服务
func ProvideCasbinService(
enforcer *casbin.Enforcer,
cache cache.Cache,
roleRepo repository.RoleRepository,
cfg *config.Config,
) service.CasbinService {
return service.NewCasbinService(enforcer, cache, roleRepo, &cfg.Casbin)
}
// ProvideRoleRepository 提供角色仓储
func ProvideRoleRepository(db *gorm.DB) repository.RoleRepository {
return repository.NewRoleRepository(db)
}