db.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 readDbs *gorm.DB
  28. var writeDbs *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. readHosts := beego.AppConfig.String("readmysqlhosts")
  52. readPorts := beego.AppConfig.String("readmysqlports")
  53. readUsers := beego.AppConfig.String("readmysqlusers")
  54. readPasss := beego.AppConfig.String("readmysqlpasss")
  55. readNames := beego.AppConfig.String("readmysqlnames")
  56. writeHosts := beego.AppConfig.String("writemysqlhosts")
  57. writePorts := beego.AppConfig.String("writemysqlports")
  58. writeUsers := beego.AppConfig.String("writemysqlusers")
  59. writePasss := beego.AppConfig.String("writemysqlpasss")
  60. writeNames := beego.AppConfig.String("writemysqlnames")
  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. rdsns := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUsers, readPasss, readHosts, readPorts, readNames)
  66. wdsns := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUsers, writePasss, writeHosts, writePorts, writeNames)
  67. readDb, err = gorm.Open("mysql", rdsn)
  68. if err != nil {
  69. //beego.Error(err)
  70. }
  71. readDb.DB().SetMaxIdleConns(10)
  72. readDb.DB().SetMaxOpenConns(100)
  73. readDb.LogMode(true)
  74. writeDb, err = gorm.Open("mysql", wdsn)
  75. if err != nil {
  76. //beego.Error(err)
  77. }
  78. writeDb.DB().SetMaxIdleConns(10)
  79. writeDb.DB().SetMaxOpenConns(100)
  80. writeDb.LogMode(true)
  81. readUserDb, err = gorm.Open("mysql", rudsn)
  82. if err != nil {
  83. //beego.Error(err)
  84. }
  85. readUserDb.DB().SetMaxIdleConns(10)
  86. readUserDb.DB().SetMaxOpenConns(100)
  87. readUserDb.LogMode(true)
  88. writeUserDb, err = gorm.Open("mysql", wudsn)
  89. if err != nil {
  90. //beego.Error(err)
  91. }
  92. writeUserDb.DB().SetMaxIdleConns(10)
  93. writeUserDb.DB().SetMaxOpenConns(100)
  94. writeUserDb.LogMode(true)
  95. readDbs, err = gorm.Open("mysql", rdsns)
  96. fmt.Println("错误", err)
  97. if err != nil {
  98. //beego.Error(err)
  99. }
  100. readDbs.DB().SetMaxIdleConns(10)
  101. readDbs.DB().SetMaxOpenConns(100)
  102. readDbs.LogMode(true)
  103. writeDbs, err = gorm.Open("mysql", wdsns)
  104. fmt.Println("错误", err)
  105. if err != nil {
  106. //beego.Error(err)
  107. }
  108. writeDbs.DB().SetMaxIdleConns(10)
  109. writeDbs.DB().SetMaxOpenConns(100)
  110. writeDbs.LogMode(true)
  111. }
  112. func PatientReadDB() *gorm.DB {
  113. return readDb
  114. }
  115. func PatientWriteDB() *gorm.DB {
  116. return writeDb
  117. }
  118. func UserReadDB() *gorm.DB {
  119. return readUserDb
  120. }
  121. func UserWriteDB() *gorm.DB {
  122. return writeUserDb
  123. }
  124. func XTReadDB() *gorm.DB {
  125. return readDbs
  126. }
  127. func XTWriteDB() *gorm.DB {
  128. return writeDbs
  129. }