connect.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package global
  2. import (
  3. "Cold_Logistic/internal/pkg/common/options"
  4. "github.com/go-redis/redis/v8"
  5. "github.com/nats-io/nats.go"
  6. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/database/myorm"
  7. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/database/myredis"
  8. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/log"
  9. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/objectStore"
  10. "gorm.io/gorm"
  11. )
  12. var CommonConnectRepoInst = &CommonConnectsRepo{}
  13. // CommonConnectsRepo 公用连接资源管理维护
  14. type CommonConnectsRepo struct {
  15. opt *options.Options
  16. StoreDB *gorm.DB
  17. ObjectStore objectStore.ObjectStore
  18. Redis *redis.Client
  19. NatsConn *nats.Conn
  20. }
  21. func NewCommonConnectsRepo(opt *options.Options) *CommonConnectsRepo {
  22. return &CommonConnectsRepo{opt: opt}
  23. }
  24. func (c *CommonConnectsRepo) Run() {
  25. // mysql
  26. c.StoreDB = myorm.InitMysql(*c.opt.DB)
  27. //// 存储
  28. //objStore, err := objectStore.NewObjectStoreByStoreType(c.opt.Storage.StoreType, *c.opt.Storage)
  29. //if err != nil {
  30. // log.Panicf("初始化objectStore失败:%s", err)
  31. //}
  32. //c.ObjectStore = objStore
  33. // Redis
  34. if c.opt.Redis.Enable {
  35. c.Redis = myredis.InitRedis(c.opt.Redis)
  36. }
  37. // Nats
  38. natsConn := options.InitNats(*c.opt.Nats)
  39. c.NatsConn = natsConn
  40. CommonConnectRepoInst = c
  41. }
  42. func (c *CommonConnectsRepo) Close() {
  43. if c.StoreDB != nil {
  44. db, _ := c.StoreDB.DB()
  45. db.Close()
  46. log.Info("Mysql dbClient close...")
  47. }
  48. if c.Redis != nil {
  49. c.Redis.Close()
  50. log.Info("Redis client close...")
  51. }
  52. if c.NatsConn != nil {
  53. c.NatsConn.Close()
  54. log.Info("Nats client close...")
  55. }
  56. }