db.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
  30. readPort := beego.AppConfig.String("readmysqlport")
  31. readUser := "kuyicloud"
  32. readPass := "yHXAcb7dTJyvMiX3vK2iOvTAHeaowy5P"
  33. readName := beego.AppConfig.String("readmysqlname")
  34. writeHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
  35. writePort := beego.AppConfig.String("writemysqlport")
  36. writeUser := "kuyicloud"
  37. writePass := "yHXAcb7dTJyvMiX3vK2iOvTAHeaowy5P"
  38. writeName := beego.AppConfig.String("writemysqlname")
  39. readUserHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
  40. readUserPort := beego.AppConfig.String("readuserport")
  41. readUserUser := "kuyicloud"
  42. readUserPass := "yHXAcb7dTJyvMiX3vK2iOvTAHeaowy5P"
  43. readUserName := beego.AppConfig.String("readusername")
  44. writeUserHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
  45. writeUserPort := beego.AppConfig.String("writeuserport")
  46. writeUserUser := "kuyicloud"
  47. writeUserPass := "yHXAcb7dTJyvMiX3vK2iOvTAHeaowy5P"
  48. writeUserName := beego.AppConfig.String("readusername")
  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 DisconnectDB() {
  83. // if err := readDb.Close(); nil != err {
  84. // beego.Error("Disconnect from database failed: " + err.Error())
  85. // }
  86. //}
  87. func XTReadDB() *gorm.DB {
  88. return readDb
  89. }
  90. func XTWriteDB() *gorm.DB {
  91. return writeDb
  92. }
  93. func UserReadDB() *gorm.DB {
  94. return readUserDb
  95. }
  96. func UserWriteDB() *gorm.DB {
  97. return writeUserDb
  98. }