golang 的 rabbitmq 消费项目
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

213 linhas
5.3 KiB

  1. package db
  2. import (
  3. "errors"
  4. "fmt"
  5. "xorm.io/xorm"
  6. "applet/app/cfg"
  7. "applet/app/db/model"
  8. "applet/app/utils/logx"
  9. )
  10. func MapBaseExists() (bool, error) {
  11. return Db.IsTableExist("db_mapping")
  12. }
  13. func InitMapDbs(c *cfg.DBCfg, prd bool) {
  14. var tables *[]model.DbMapping
  15. exists, err := MapBaseExists()
  16. if !exists || err != nil {
  17. logx.Fatalf("db_mapping not exists : %v", err)
  18. }
  19. // tables := MapAllDatabases(debug)
  20. if cfg.Prd {
  21. tables = GetAllDatabasePrd() //debug 获取生产
  22. } else {
  23. tables = GetAllDatabaseDev() //debug 获取开发
  24. }
  25. if tables == nil {
  26. logx.Fatal("no database tables data")
  27. }
  28. var e *xorm.Engine
  29. DBs = map[string]*xorm.Engine{}
  30. for _, v := range *tables {
  31. if v.DbName != "" && v.DeletedAt == 0 && v.DbName != c.Name {
  32. dbCfg := cfg.DBCfg{
  33. Name: v.DbName,
  34. ShowLog: c.ShowLog,
  35. MaxLifetime: c.MaxLifetime,
  36. MaxOpenConns: c.MaxOpenConns,
  37. MaxIdleConns: c.MaxIdleConns,
  38. Path: fmt.Sprintf(c.Path, v.DbName),
  39. }
  40. if v.DbHost != "" && v.DbUsername != "" && v.DbPassword != "" {
  41. dbCfg.Host = v.DbHost
  42. dbCfg.User = v.DbUsername
  43. dbCfg.Psw = v.DbPassword
  44. } else {
  45. dbCfg.Host = c.Host
  46. dbCfg.User = c.User
  47. dbCfg.Psw = c.Psw
  48. }
  49. e, err = NewDB(&dbCfg)
  50. if err != nil || e == nil {
  51. 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)
  52. } else {
  53. DBs[v.DbMasterId] = e
  54. }
  55. }
  56. }
  57. }
  58. func MapAllDatabases(debug bool) *[]model.DbMapping {
  59. sql := "`db_name` != ?"
  60. if debug {
  61. sql = "`db_name` = ?"
  62. }
  63. var m []model.DbMapping
  64. if err := Db.Where(sql, cfg.DB.Name).Find(&m); err != nil || len(m) == 0 {
  65. logx.Warn(err)
  66. return nil
  67. }
  68. return &m
  69. }
  70. // GetAllDatabasePrd is 获取生成库 所有db 除了 deleted_at = 1 的
  71. func GetAllDatabasePrd() *[]model.DbMapping {
  72. var m []model.DbMapping
  73. if err := Db.Where("deleted_at != ? AND is_dev = '0' ", 1).Find(&m); err != nil || len(m) == 0 {
  74. logx.Warn(err)
  75. return nil
  76. }
  77. var mm []model.SupplyDbMapping
  78. if err := Db.Where("deleted_at != ? AND is_dev = '0' ", 1).Find(&mm); err != nil || len(mm) == 0 {
  79. logx.Warn(err)
  80. return nil
  81. }
  82. for _, v := range mm {
  83. m = append(m, model.DbMapping{
  84. DbMasterId: v.DbMasterId,
  85. DbHost: v.DbHost,
  86. DbUsername: v.DbUsername,
  87. DbPassword: v.DbPassword,
  88. DbName: v.DbName,
  89. ExternalMysql: v.ExternalMysql,
  90. IsDev: v.IsDev,
  91. CreatedAt: v.CreatedAt,
  92. UpdatedAt: v.UpdatedAt,
  93. DeletedAt: v.DeletedAt,
  94. })
  95. }
  96. return &m
  97. }
  98. // GetAllDatabaseDev is 获取开发库 所有db 除了 deleted_at = 1 的
  99. func GetAllDatabaseDev() *[]model.DbMapping {
  100. var m []model.DbMapping
  101. var err error
  102. fmt.Println("cfg.Local is: ", cfg.Local)
  103. if cfg.Local { // 本地调试 加快速度
  104. fmt.Println("notice:LOCAL TEST, only masterId:** 99813608 ** available!")
  105. err = Db.Where("deleted_at != ? AND db_master_id=?", 1, 31585332).Find(&m)
  106. } else {
  107. err = Db.Where("deleted_at != ? AND is_dev = '1' ", 1).Find(&m)
  108. }
  109. //err := Db.Where("deleted_at != ? AND is_dev = '1' and db_master_id='123456'", 1).Find(&m)
  110. if err != nil || len(m) == 0 {
  111. logx.Warn(err)
  112. return nil
  113. }
  114. return &m
  115. }
  116. //GetDatabaseByMasterID is 根据站长id 获取对应的的数据库信息
  117. func GetDatabaseByMasterID(Db *xorm.Engine, id string) (*model.DbMapping, error) {
  118. var m model.DbMapping
  119. has, err := Db.Where("db_master_id=?", id).Get(&m)
  120. if !has {
  121. return nil, errors.New("Not Found DB data by " + id)
  122. }
  123. if err != nil {
  124. return nil, err
  125. }
  126. if m.DbHost == "" {
  127. m.DbHost = cfg.DB.Host
  128. m.DbUsername = cfg.DB.User
  129. m.DbPassword = cfg.DB.Psw
  130. }
  131. return &m, nil
  132. }
  133. //SessionGetDatabaseByMasterID is 根据站长id 获取对应的的数据库信息
  134. func SessionGetDatabaseByMasterID(Db *xorm.Session, id string) (*model.DbMapping, error) {
  135. var m model.DbMapping
  136. has, err := Db.Where("db_master_id=?", id).Get(&m)
  137. if !has {
  138. return nil, errors.New("Not Found DB data by " + id)
  139. }
  140. if err != nil {
  141. return nil, err
  142. }
  143. if m.DbHost == "" {
  144. m.DbHost = cfg.DB.Host
  145. m.DbName = cfg.DB.Name
  146. m.DbUsername = cfg.DB.User
  147. m.DbPassword = cfg.DB.Psw
  148. }
  149. return &m, nil
  150. }
  151. // 获取自动任务队列
  152. func MapCrontabCfg(eg *xorm.Engine) *[]model.SysCfg {
  153. var c []model.SysCfg
  154. // 数据库查询如果有下划线会认为是一个任意字符
  155. if err := eg.Where("`key` LIKE 'mall_cron\\_%' AND val != ''").Cols("`key`,`val`").Find(&c); err != nil || len(c) == 0 {
  156. logx.Warn(err)
  157. return nil
  158. }
  159. return &c
  160. }
  161. // 获取官方域名
  162. func GetOfficialDomainInfoByType(Db *xorm.Engine, masterId, key string) (string, error) {
  163. type SysCfg struct {
  164. K string
  165. V string
  166. Memo string
  167. }
  168. var domainBase SysCfg
  169. has, err := Db.Where("k=?", "domain_base").Get(&domainBase)
  170. if err != nil {
  171. return "", err
  172. }
  173. if has == false {
  174. return "", errors.New("can not find key by : domain_base")
  175. }
  176. if key == "wap" {
  177. return "h5." + masterId + "." + domainBase.V, nil
  178. }
  179. if key == "api" {
  180. var apiDomain SysCfg
  181. has, err = Db.Where("k=?", "domain_api_base").Get(&apiDomain)
  182. if err != nil {
  183. return "", err
  184. }
  185. if has == false {
  186. return "", errors.New("can not find key by : domain_api_base")
  187. }
  188. return apiDomain.V, nil
  189. }
  190. if key == "admin" {
  191. return "admin." + masterId + "." + domainBase.V, nil
  192. }
  193. // 默认返回H5的
  194. return "h5." + masterId + "." + domainBase.V, nil
  195. }