db.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 err error
  28. func ConnectDB() {
  29. readHost := beego.AppConfig.String("readmysqlhost")
  30. readPort := beego.AppConfig.String("readmysqlport")
  31. readUser := beego.AppConfig.String("readmysqluser")
  32. readPass := beego.AppConfig.String("readmysqlpass")
  33. readName := beego.AppConfig.String("readmysqlname")
  34. writeHost := beego.AppConfig.String("writemysqlhost")
  35. writePort := beego.AppConfig.String("writemysqlport")
  36. writeUser := beego.AppConfig.String("writemysqluser")
  37. writePass := beego.AppConfig.String("writemysqlpass")
  38. writeName := beego.AppConfig.String("writemysqlname")
  39. readUserHost := beego.AppConfig.String("readuserhost")
  40. readUserPort := beego.AppConfig.String("readuserport")
  41. readUserUser := beego.AppConfig.String("readuseruser")
  42. readUserPass := beego.AppConfig.String("readuserpass")
  43. readUserName := beego.AppConfig.String("readusername")
  44. writeUserHost := beego.AppConfig.String("writeuserhost")
  45. writeUserPort := beego.AppConfig.String("writeuserport")
  46. writeUserUser := beego.AppConfig.String("writeuseruser")
  47. writeUserPass := beego.AppConfig.String("writeuserpass")
  48. writeUserName := beego.AppConfig.String("writeusername")
  49. rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
  50. wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
  51. rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
  52. wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  53. readDb, err = gorm.Open("mysql", rdsn)
  54. //defer readDb.Close()
  55. if err != nil {
  56. //beego.Error(err)
  57. }
  58. readDb.DB().SetMaxIdleConns(5)
  59. readDb.DB().SetMaxOpenConns(20)
  60. readDb.LogMode(true)
  61. writeDb, err = gorm.Open("mysql", wdsn)
  62. if err != nil {
  63. //beego.Error(err)
  64. }
  65. //defer writeDb.Close()
  66. writeDb.DB().SetMaxIdleConns(5)
  67. writeDb.DB().SetMaxOpenConns(20)
  68. writeDb.LogMode(true)
  69. readUserDb, err = gorm.Open("mysql", rudsn)
  70. if err != nil {
  71. //beego.Error(err)
  72. }
  73. //defer readUserDb.Close()
  74. readUserDb.DB().SetMaxIdleConns(5)
  75. readUserDb.DB().SetMaxOpenConns(20)
  76. readUserDb.LogMode(true)
  77. writeUserDb, err = gorm.Open("mysql", wudsn)
  78. if err != nil {
  79. //beego.Error(err)
  80. }
  81. //defer writeUserDb.Close()
  82. writeUserDb.DB().SetMaxIdleConns(5)
  83. writeUserDb.DB().SetMaxOpenConns(20)
  84. writeUserDb.LogMode(true)
  85. }
  86. func XTReadDB() *gorm.DB {
  87. return readDb
  88. }
  89. func XTWriteDB() *gorm.DB {
  90. return writeDb
  91. }
  92. func UserReadDB() *gorm.DB {
  93. return readUserDb
  94. }
  95. func UserWriteDB() *gorm.DB {
  96. return writeUserDb
  97. }