db.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. if err != nil {
  55. //beego.Error(err)
  56. }
  57. readDb.DB().SetMaxIdleConns(10)
  58. readDb.DB().SetMaxOpenConns(100)
  59. readDb.LogMode(true)
  60. writeDb, err = gorm.Open("mysql", wdsn)
  61. if err != nil {
  62. //beego.Error(err)
  63. }
  64. writeDb.DB().SetMaxIdleConns(10)
  65. writeDb.DB().SetMaxOpenConns(100)
  66. writeDb.LogMode(true)
  67. readUserDb, err = gorm.Open("mysql", rudsn)
  68. if err != nil {
  69. //beego.Error(err)
  70. }
  71. readUserDb.DB().SetMaxIdleConns(10)
  72. readUserDb.DB().SetMaxOpenConns(100)
  73. readUserDb.LogMode(true)
  74. writeUserDb, err = gorm.Open("mysql", wudsn)
  75. if err != nil {
  76. //beego.Error(err)
  77. }
  78. writeUserDb.DB().SetMaxIdleConns(10)
  79. writeUserDb.DB().SetMaxOpenConns(100)
  80. writeUserDb.LogMode(true)
  81. }
  82. func PatientReadDB() *gorm.DB {
  83. return readDb
  84. }
  85. func PatientWriteDB() *gorm.DB {
  86. return writeDb
  87. }
  88. func UserReadDB() *gorm.DB {
  89. return readUserDb
  90. }
  91. func UserWriteDB() *gorm.DB {
  92. return writeUserDb
  93. }