附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 1 месец
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package db
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "xorm.io/xorm"
  7. "xorm.io/xorm/log"
  8. "applet/app/cfg"
  9. "applet/app/db/model"
  10. "applet/app/utils/logx"
  11. )
  12. var DBs map[string]*xorm.Engine
  13. // 每个站长都要有自己的syscfg 缓存, 键是站长id,值是缓存名
  14. // var SysCfgMapKey map[string]string
  15. func NewDB(c *cfg.DBCfg) (*xorm.Engine, error) {
  16. db, err := xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4", c.User, c.Psw, c.Host, c.Name))
  17. if err != nil {
  18. return nil, err
  19. }
  20. db.SetConnMaxLifetime(c.MaxLifetime * time.Second)
  21. db.SetMaxOpenConns(c.MaxOpenConns)
  22. db.SetMaxIdleConns(c.MaxIdleConns)
  23. err = db.Ping()
  24. if err != nil {
  25. return nil, err
  26. }
  27. if c.ShowLog {
  28. db.ShowSQL(true)
  29. db.Logger().SetLevel(log.LOG_DEBUG)
  30. f, err := os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
  31. if err != nil {
  32. os.RemoveAll(c.Path)
  33. if f, err = os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777); err != nil {
  34. return nil, err
  35. }
  36. }
  37. logger := log.NewSimpleLogger(f)
  38. logger.ShowSQL(true)
  39. db.SetLogger(logger)
  40. }
  41. return db, nil
  42. }
  43. // InitDBs is 初始化多数据库
  44. func InitDBs(ch chan int) {
  45. // 初始化多数据库
  46. var tables *[]model.DbMapping
  47. InitMapDbs(cfg.DB, cfg.Prd)
  48. ch <- 1
  49. // 每10s 查询一次模板数据库的db mapping 表,如果有新增数据库记录,则添加到 DBs中
  50. ticker := time.NewTicker(time.Duration(time.Second * 120))
  51. for range ticker.C {
  52. if cfg.Prd {
  53. tables = GetAllDatabasePrd() //默认获取全部
  54. } else {
  55. tables = GetAllDatabaseDev() //默认获取全部
  56. }
  57. if tables == nil {
  58. logx.Warn("no database tables data")
  59. continue
  60. }
  61. for _, item := range *tables {
  62. _, ok := DBs[item.DbMasterId]
  63. if !ok {
  64. // 不在db.DBs 则添加进去
  65. dbCfg := cfg.DBCfg{
  66. Name: item.DbName,
  67. ShowLog: cfg.DB.ShowLog,
  68. MaxLifetime: cfg.DB.MaxLifetime,
  69. MaxOpenConns: cfg.DB.MaxOpenConns,
  70. MaxIdleConns: cfg.DB.MaxIdleConns,
  71. Path: fmt.Sprintf(cfg.DB.Path, item.DbName),
  72. }
  73. if item.DbHost != "" && item.DbUsername != "" && item.DbPassword != "" {
  74. dbCfg.Host = item.DbHost
  75. dbCfg.User = item.DbUsername
  76. dbCfg.Psw = item.DbPassword
  77. } else {
  78. dbCfg.Host = cfg.DB.Host
  79. dbCfg.User = cfg.DB.User
  80. dbCfg.Psw = cfg.DB.Psw
  81. }
  82. e, err := NewDB(&dbCfg)
  83. if err != nil || e == nil {
  84. logx.Warnf("db engine can't create, please check config, params[host:%s, user:%s, psw: %s, name: %s], err: %v", dbCfg.Host, dbCfg.User, dbCfg.Psw, dbCfg.Name, err)
  85. } else {
  86. DBs[item.DbMasterId] = e
  87. }
  88. }
  89. // 如果 被禁用则删除
  90. if item.DeletedAt == 1 {
  91. logx.Infof("%s have been removed", item.DbMasterId)
  92. delete(DBs, item.DbMasterId)
  93. }
  94. }
  95. }
  96. }