service_context.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package svc
  2. import (
  3. "github.com/bwmarrin/snowflake"
  4. "github.com/redis/go-redis/v9"
  5. "github.com/zeromicro/go-zero/core/collection"
  6. "gorm.io/gorm"
  7. "time"
  8. "wechat-api/database"
  9. "wechat-api/database/dao/wechat/query"
  10. "wechat-api/internal/config"
  11. "wechat-api/internal/middleware"
  12. "wechat-api/internal/pkg/wechat_ws"
  13. "github.com/suyuan32/simple-admin-core/rpc/coreclient"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. "wechat-api/ent"
  16. _ "wechat-api/ent/runtime"
  17. "github.com/casbin/casbin/v2"
  18. "github.com/zeromicro/go-zero/rest"
  19. "github.com/zeromicro/go-zero/zrpc"
  20. )
  21. type ServiceContext struct {
  22. Config config.Config
  23. Casbin *casbin.Enforcer
  24. Authority rest.Middleware
  25. Miniprogram rest.Middleware
  26. DB *ent.Client //大家都不爱用,后边可以慢慢过渡到gorm的库
  27. WechatDB *gorm.DB
  28. WechatQ *query.Query
  29. CoreRpc coreclient.Core
  30. Rds redis.UniversalClient
  31. WechatWs map[string]*wechat_ws.WechatWsClient
  32. Cache *collection.Cache
  33. NodeID *snowflake.Node
  34. }
  35. func NewServiceContext(c config.Config) *ServiceContext {
  36. rds := c.RedisConf.MustNewUniversalRedis()
  37. cbn := c.CasbinConf.MustNewCasbinWithOriginalRedisWatcher(c.CasbinDatabaseConf.Type, c.CasbinDatabaseConf.GetDSN(), c.RedisConf)
  38. db := ent.NewClient(
  39. ent.Log(logx.Info), // logger
  40. ent.Driver(c.DatabaseConf.NewNoCacheDriver()),
  41. ent.Debug(), // debug mode
  42. )
  43. // gorm 数据库连接
  44. wechatDb, err := database.InitWechatDB(c.DatabaseConf, c.Mode)
  45. if err != nil {
  46. logx.Error(err)
  47. panic("gorm 数据库连接失败")
  48. }
  49. wechatQ := query.Use(wechatDb)
  50. coreRpc := coreclient.NewCore(zrpc.NewClientIfEnable(c.CoreRpc))
  51. wechatWs := make(map[string]*wechat_ws.WechatWsClient)
  52. for _, ws := range c.WebSocket {
  53. if ws.Type == "wechat" {
  54. client, err := wechat_ws.NewWechatWsClient(ws.Url, ws.Name, ws.Type)
  55. if err != nil {
  56. logx.Error(err)
  57. } else {
  58. logx.Info("建立ws client成功~", ws.Name)
  59. go client.ReadPump()
  60. go client.WritePump()
  61. wechatWs[ws.Name] = client
  62. }
  63. }
  64. }
  65. cache, err := collection.NewCache(time.Hour * 24 * 365)
  66. if err != nil {
  67. logx.Error(err)
  68. panic("本地缓存实例失败")
  69. }
  70. node, err := snowflake.NewNode(1)
  71. if err != nil {
  72. logx.Error(err)
  73. panic("雪花算法实例失败")
  74. }
  75. return &ServiceContext{
  76. Config: c,
  77. Authority: middleware.NewAuthorityMiddleware(cbn, rds, coreRpc).Handle,
  78. Miniprogram: middleware.NewMiniprogramMiddleware(cbn, rds, coreRpc, c).Handle,
  79. DB: db,
  80. WechatDB: wechatDb,
  81. WechatQ: wechatQ,
  82. CoreRpc: coreRpc,
  83. Rds: rds,
  84. WechatWs: wechatWs,
  85. Cache: cache,
  86. NodeID: node,
  87. }
  88. }