db.go 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Pipe - A small and beautiful blogging platform written in golang.
  2. // Copyright (C) 2017-2018, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. package service
  17. import (
  18. "fmt"
  19. "github.com/astaxie/beego"
  20. "github.com/jinzhu/gorm"
  21. _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
  22. )
  23. var readDb *gorm.DB
  24. var writeDb *gorm.DB
  25. var readUserDb *gorm.DB
  26. var writeUserDb *gorm.DB
  27. var readChargeDb *gorm.DB
  28. var writeChargeDb *gorm.DB
  29. var err error
  30. func ConnectDB() {
  31. readHost := beego.AppConfig.String("readmysqlhost")
  32. readPort := beego.AppConfig.String("readmysqlport")
  33. readUser := beego.AppConfig.String("readmysqluser")
  34. readPass := beego.AppConfig.String("readmysqlpass")
  35. readName := beego.AppConfig.String("readmysqlname")
  36. writeHost := beego.AppConfig.String("writemysqlhost")
  37. writePort := beego.AppConfig.String("writemysqlport")
  38. writeUser := beego.AppConfig.String("writemysqluser")
  39. writePass := beego.AppConfig.String("writemysqlpass")
  40. writeName := beego.AppConfig.String("writemysqlname")
  41. readUserHost := beego.AppConfig.String("readuserhost")
  42. readUserPort := beego.AppConfig.String("readuserport")
  43. readUserUser := beego.AppConfig.String("readuseruser")
  44. readUserPass := beego.AppConfig.String("readuserpass")
  45. readUserName := beego.AppConfig.String("readusername")
  46. writeUserHost := beego.AppConfig.String("writeuserhost")
  47. writeUserPort := beego.AppConfig.String("writeuserport")
  48. writeUserUser := beego.AppConfig.String("writeuseruser")
  49. writeUserPass := beego.AppConfig.String("writeuserpass")
  50. writeUserName := beego.AppConfig.String("writeusername")
  51. readChargeHost := beego.AppConfig.String("readchargehost")
  52. readChargePort := beego.AppConfig.String("readchargeport")
  53. readChargeUser := beego.AppConfig.String("readchargeuser")
  54. readChargePass := beego.AppConfig.String("readchargepass")
  55. readChargeName := beego.AppConfig.String("readchargename")
  56. writeChargeHost := beego.AppConfig.String("writechargehost")
  57. writeChargePort := beego.AppConfig.String("writechargeport")
  58. writeChargeUser := beego.AppConfig.String("writechargeuser")
  59. writeChargePass := beego.AppConfig.String("writechargepass")
  60. writeChargeName := beego.AppConfig.String("writechargename")
  61. rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
  62. wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
  63. rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
  64. wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  65. rcdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readChargeUser, readChargePass, readChargeHost, readChargePort, readChargeName)
  66. wcdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeChargeUser, writeChargePass, writeChargeHost, writeChargePort, writeChargeName)
  67. readDb, err = gorm.Open("mysql", rdsn)
  68. //defer readDb.Close()
  69. if err != nil {
  70. //beego.Error(err)
  71. }
  72. readDb.DB().SetMaxIdleConns(5)
  73. readDb.DB().SetMaxOpenConns(20)
  74. readDb.LogMode(true)
  75. writeDb, err = gorm.Open("mysql", wdsn)
  76. if err != nil {
  77. //beego.Error(err)
  78. }
  79. //defer writeDb.Close()
  80. writeDb.DB().SetMaxIdleConns(5)
  81. writeDb.DB().SetMaxOpenConns(20)
  82. writeDb.LogMode(true)
  83. readUserDb, err = gorm.Open("mysql", rudsn)
  84. if err != nil {
  85. //beego.Error(err)
  86. }
  87. //defer readUserDb.Close()
  88. readUserDb.DB().SetMaxIdleConns(5)
  89. readUserDb.DB().SetMaxOpenConns(20)
  90. readUserDb.LogMode(true)
  91. writeUserDb, err = gorm.Open("mysql", wudsn)
  92. if err != nil {
  93. //beego.Error(err)
  94. }
  95. //defer writeUserDb.Close()
  96. writeUserDb.DB().SetMaxIdleConns(5)
  97. writeUserDb.DB().SetMaxOpenConns(20)
  98. writeUserDb.LogMode(true)
  99. readChargeDb, err = gorm.Open("mysql", rcdsn)
  100. if err != nil {
  101. //beego.Error(err)
  102. }
  103. readChargeDb.DB().SetMaxIdleConns(5)
  104. readChargeDb.DB().SetMaxOpenConns(20)
  105. readChargeDb.LogMode(true)
  106. writeChargeDb, err = gorm.Open("mysql", wcdsn)
  107. if err != nil {
  108. //beego.Error(err)
  109. }
  110. //defer writeUserDb.Close()
  111. writeChargeDb.DB().SetMaxIdleConns(5)
  112. writeChargeDb.DB().SetMaxOpenConns(20)
  113. writeChargeDb.LogMode(true)
  114. }
  115. func XTReadDB() *gorm.DB {
  116. return readDb
  117. }
  118. func XTWriteDB() *gorm.DB {
  119. return writeDb
  120. }
  121. func UserReadDB() *gorm.DB {
  122. return readUserDb
  123. }
  124. func UserWriteDB() *gorm.DB {
  125. return writeUserDb
  126. }
  127. func ChargeReadDB() *gorm.DB {
  128. return readChargeDb
  129. }
  130. func ChargeWriteDB() *gorm.DB {
  131. return writeChargeDb
  132. }