db.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/mssql" // mssql
  22. _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
  23. )
  24. var readDb *gorm.DB
  25. var writeDb *gorm.DB
  26. var readRemoteDb *gorm.DB
  27. var writeUserDb *gorm.DB
  28. var readUserDb *gorm.DB
  29. var readMiddleDb *gorm.DB
  30. var writeMiddleDb *gorm.DB
  31. var backupDb *gorm.DB
  32. var err error
  33. var blDb *gorm.DB
  34. func ConnectDB() {
  35. readHost := beego.AppConfig.String("readmysqlhost")
  36. readPort := beego.AppConfig.String("readmysqlport")
  37. readUser := beego.AppConfig.String("readmysqluser")
  38. readPass := beego.AppConfig.String("readmysqlpass")
  39. readName := beego.AppConfig.String("readmysqlname")
  40. writeHost := beego.AppConfig.String("writemysqlhost")
  41. writePort := beego.AppConfig.String("writemysqlport")
  42. writeUser := beego.AppConfig.String("writemysqluser")
  43. writePass := beego.AppConfig.String("writemysqlpass")
  44. writeName := beego.AppConfig.String("writemysqlname")
  45. readUserHost := beego.AppConfig.String("readuserhost")
  46. readUserPort := beego.AppConfig.String("readuserport")
  47. readUserUser := beego.AppConfig.String("readuseruser")
  48. readUserPass := beego.AppConfig.String("readuserpass")
  49. readUserName := beego.AppConfig.String("readusername")
  50. // readRemoteHost := beego.AppConfig.String("readremotehost")
  51. // readRemotePort := beego.AppConfig.String("readremoteport")
  52. // readRemoteUser := beego.AppConfig.String("readremoteuser")
  53. // readRemotePass := beego.AppConfig.String("readremotepass")
  54. // readRemoteName := beego.AppConfig.String("readremotename")
  55. writeUserHost := beego.AppConfig.String("writeuserhost")
  56. writeUserPort := beego.AppConfig.String("writeuserport")
  57. writeUserUser := beego.AppConfig.String("writeuseruser")
  58. writeUserPass := beego.AppConfig.String("writeuserpass")
  59. writeUserName := beego.AppConfig.String("writeusername")
  60. readMiddleHost := beego.AppConfig.String("readmiddlehost")
  61. readMiddlePort := beego.AppConfig.String("readmiddleport")
  62. readMiddleUser := beego.AppConfig.String("readmiddleuser")
  63. readMiddlePass := beego.AppConfig.String("readmiddlepass")
  64. readMiddleName := beego.AppConfig.String("readmiddlename")
  65. writeMiddleHost := beego.AppConfig.String("writemiddlehost")
  66. writeMiddlePort := beego.AppConfig.String("writemiddleport")
  67. writeMiddleUser := beego.AppConfig.String("writemiddleuser")
  68. writeMiddlePass := beego.AppConfig.String("writemiddlepass")
  69. writeMiddleName := beego.AppConfig.String("writemiddlename")
  70. // backupHost := beego.AppConfig.String("backuphost")
  71. // backupPort := beego.AppConfig.String("backupport")
  72. // backupUser := beego.AppConfig.String("backupuser")
  73. // backupPass := beego.AppConfig.String("backuppass")
  74. // backupName := beego.AppConfig.String("backupname")
  75. blHost := beego.AppConfig.String("blmysqlhost")
  76. blPort := beego.AppConfig.String("blmysqlport")
  77. blUser := beego.AppConfig.String("blmysqluser")
  78. blPass := beego.AppConfig.String("blmysqlpass")
  79. blName := beego.AppConfig.String("blmysqlname")
  80. rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
  81. wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
  82. // rudsn := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s", readRemoteHost, readRemotePort, readRemoteName, readRemoteUser, readRemotePass)
  83. wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  84. rmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readMiddleUser, readMiddlePass, readMiddleHost, readMiddlePort, readMiddleName)
  85. wmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeMiddleUser, writeMiddlePass, writeMiddleHost, writeMiddlePort, writeMiddleName)
  86. rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
  87. bldsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", blUser, blPass, blHost, blPort, blName)
  88. // bdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", backupUser, backupPass, backupHost, backupPort, backupName)
  89. readDb, err = gorm.Open("mysql", rdsn)
  90. if err != nil {
  91. //beego.Error(err)
  92. }
  93. readDb.DB().SetMaxIdleConns(10)
  94. readDb.DB().SetMaxOpenConns(100)
  95. readDb.LogMode(true)
  96. // backupDb, err = gorm.Open("mysql", bdsn)
  97. // if err != nil {
  98. // //beego.Error(err)
  99. // }
  100. // backupDb.DB().SetMaxIdleConns(10)
  101. // backupDb.DB().SetMaxOpenConns(100)
  102. // backupDb.LogMode(true)
  103. writeDb, err = gorm.Open("mysql", wdsn)
  104. if err != nil {
  105. //beego.Error(err)
  106. }
  107. writeDb.DB().SetMaxIdleConns(10)
  108. writeDb.DB().SetMaxOpenConns(100)
  109. writeDb.LogMode(true)
  110. // fmt.Println(rudsn)
  111. // readRemoteDb, err = gorm.Open("mssql", rudsn)
  112. // if err != nil {
  113. // panic(err)
  114. // }
  115. // readRemoteDb.DB().SetMaxIdleConns(10)
  116. // readRemoteDb.DB().SetMaxOpenConns(100)
  117. // readRemoteDb.LogMode(true)
  118. writeUserDb, err = gorm.Open("mysql", wudsn)
  119. if err != nil {
  120. //beego.Error(err)
  121. }
  122. writeUserDb.DB().SetMaxIdleConns(10)
  123. writeUserDb.DB().SetMaxOpenConns(100)
  124. writeUserDb.LogMode(true)
  125. readMiddleDb, err = gorm.Open("mysql", rmdsn)
  126. if err != nil {
  127. //beego.Error(err)
  128. }
  129. readMiddleDb.DB().SetMaxIdleConns(10)
  130. readMiddleDb.DB().SetMaxOpenConns(100)
  131. readMiddleDb.LogMode(true)
  132. writeMiddleDb, err = gorm.Open("mysql", wmdsn)
  133. if err != nil {
  134. //beego.Error(err)
  135. }
  136. writeMiddleDb.DB().SetMaxIdleConns(10)
  137. writeMiddleDb.DB().SetMaxOpenConns(100)
  138. writeMiddleDb.LogMode(true)
  139. readUserDb, err = gorm.Open("mysql", rudsn)
  140. if err != nil {
  141. //beego.Error(err)
  142. }
  143. readUserDb.DB().SetMaxIdleConns(10)
  144. readUserDb.DB().SetMaxOpenConns(100)
  145. readUserDb.LogMode(true)
  146. blDb, err = gorm.Open("mysql", bldsn)
  147. if err != nil {
  148. //beego.Error(err)
  149. }
  150. blDb.DB().SetMaxIdleConns(10)
  151. blDb.DB().SetMaxOpenConns(100)
  152. blDb.LogMode(true)
  153. }
  154. //func DisconnectDB() {
  155. // if err := readDb.Close(); nil != err {
  156. // beego.Error("Disconnect from database failed: " + err.Error())
  157. // }
  158. //}
  159. func XTReadDB() *gorm.DB {
  160. return readDb
  161. }
  162. func XTWriteDB() *gorm.DB {
  163. return writeDb
  164. }
  165. // func RemoteReadDB() *gorm.DB {
  166. // return readRemoteDb
  167. // }
  168. func UserWriteDB() *gorm.DB {
  169. return writeUserDb
  170. }
  171. func MiddleReadDB() *gorm.DB {
  172. return readMiddleDb
  173. }
  174. func MiddleWriteDB() *gorm.DB {
  175. return writeMiddleDb
  176. }