service_context.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package svc
  2. import (
  3. "github.com/redis/go-redis/v9"
  4. "wechat-api/internal/config"
  5. "wechat-api/internal/middleware"
  6. "github.com/suyuan32/simple-admin-core/rpc/coreclient"
  7. "github.com/zeromicro/go-zero/core/logx"
  8. "wechat-api/ent"
  9. _ "wechat-api/ent/runtime"
  10. "github.com/casbin/casbin/v2"
  11. "github.com/zeromicro/go-zero/rest"
  12. "github.com/zeromicro/go-zero/zrpc"
  13. )
  14. type ServiceContext struct {
  15. Config config.Config
  16. Casbin *casbin.Enforcer
  17. Authority rest.Middleware
  18. DB *ent.Client
  19. CoreRpc coreclient.Core
  20. Rds redis.UniversalClient
  21. }
  22. func NewServiceContext(c config.Config) *ServiceContext {
  23. rds := c.RedisConf.MustNewUniversalRedis()
  24. cbn := c.CasbinConf.MustNewCasbinWithOriginalRedisWatcher(c.CasbinDatabaseConf.Type, c.CasbinDatabaseConf.GetDSN(), c.RedisConf)
  25. db := ent.NewClient(
  26. ent.Log(logx.Info), // logger
  27. ent.Driver(c.DatabaseConf.NewNoCacheDriver()),
  28. ent.Debug(), // debug mode
  29. )
  30. coreRpc := coreclient.NewCore(zrpc.NewClientIfEnable(c.CoreRpc))
  31. return &ServiceContext{
  32. Config: c,
  33. Authority: middleware.NewAuthorityMiddleware(cbn, rds, coreRpc).Handle,
  34. DB: db,
  35. CoreRpc: coreRpc,
  36. Rds: rds,
  37. }
  38. }