12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package global
- import (
- "Cold_Logistic/internal/pkg/common/options"
- "github.com/go-redis/redis/v8"
- "github.com/nats-io/nats.go"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/database/myorm"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/database/myredis"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/log"
- "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/objectStore"
- "gorm.io/gorm"
- )
- var CommonConnectRepoInst = &CommonConnectsRepo{}
- // CommonConnectsRepo 公用连接资源管理维护
- type CommonConnectsRepo struct {
- opt *options.Options
- StoreDB *gorm.DB
- ObjectStore objectStore.ObjectStore
- Redis *redis.Client
- NatsConn *nats.Conn
- }
- func NewCommonConnectsRepo(opt *options.Options) *CommonConnectsRepo {
- return &CommonConnectsRepo{opt: opt}
- }
- func (c *CommonConnectsRepo) Run() {
- // mysql
- c.StoreDB = myorm.InitMysql(*c.opt.DB)
- //// 存储
- //objStore, err := objectStore.NewObjectStoreByStoreType(c.opt.Storage.StoreType, *c.opt.Storage)
- //if err != nil {
- // log.Panicf("初始化objectStore失败:%s", err)
- //}
- //c.ObjectStore = objStore
- // Redis
- if c.opt.Redis.Enable {
- c.Redis = myredis.InitRedis(c.opt.Redis)
- }
- // Nats
- natsConn := options.InitNats(*c.opt.Nats)
- c.NatsConn = natsConn
- CommonConnectRepoInst = c
- }
- func (c *CommonConnectsRepo) Close() {
- if c.StoreDB != nil {
- db, _ := c.StoreDB.DB()
- db.Close()
- log.Info("Mysql dbClient close...")
- }
- if c.Redis != nil {
- c.Redis.Close()
- log.Info("Redis client close...")
- }
- if c.NatsConn != nil {
- c.NatsConn.Close()
- log.Info("Nats client close...")
- }
- }
|