sso

db.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 readXTDb *gorm.DB
  28. var writeXTDb *gorm.DB
  29. var err error
  30. // var gormDb *gorm.DB
  31. func ConnectDB() {
  32. readHost := beego.AppConfig.String("readmysqlhost")
  33. readPort := beego.AppConfig.String("readmysqlport")
  34. readUser := beego.AppConfig.String("readmysqluser")
  35. readPass := beego.AppConfig.String("readmysqlpass")
  36. readName := beego.AppConfig.String("readmysqlname")
  37. writeHost := beego.AppConfig.String("writemysqlhost")
  38. writePort := beego.AppConfig.String("writemysqlport")
  39. writeUser := beego.AppConfig.String("writemysqluser")
  40. writePass := beego.AppConfig.String("writemysqlpass")
  41. writeName := beego.AppConfig.String("writemysqlname")
  42. readUserHost := beego.AppConfig.String("readuserhost")
  43. readUserPort := beego.AppConfig.String("readuserport")
  44. readUserUser := beego.AppConfig.String("readuseruser")
  45. readUserPass := beego.AppConfig.String("readuserpass")
  46. readUserName := beego.AppConfig.String("readusername")
  47. writeUserHost := beego.AppConfig.String("writeuserhost")
  48. writeUserPort := beego.AppConfig.String("writeuserport")
  49. writeUserUser := beego.AppConfig.String("writeuseruser")
  50. writeUserPass := beego.AppConfig.String("writeuserpass")
  51. writeUserName := beego.AppConfig.String("writeusername")
  52. readXTHost := beego.AppConfig.String("readsqlhost")
  53. readXTPort := beego.AppConfig.String("readsqlport")
  54. readXTUser := beego.AppConfig.String("readsqluser")
  55. readXTPass := beego.AppConfig.String("readsqlpass")
  56. readXTName := beego.AppConfig.String("readsqlname")
  57. writeXTHost := beego.AppConfig.String("writesqlhost")
  58. writeXTPort := beego.AppConfig.String("writesqlport")
  59. writeXTUser := beego.AppConfig.String("writesqluser")
  60. writeXTPass := beego.AppConfig.String("writesqlpass")
  61. writeXTName := beego.AppConfig.String("writesqlname")
  62. rxdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readXTUser, readXTPass, readXTHost, readXTPort, readXTName)
  63. wxdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeXTUser, writeXTPass, writeXTHost, writeXTPort, writeXTName)
  64. rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
  65. wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
  66. rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
  67. wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
  68. readDb, err = gorm.Open("mysql", rdsn)
  69. if err != nil {
  70. beego.Error(err)
  71. }
  72. readDb.DB().SetMaxIdleConns(10)
  73. readDb.DB().SetMaxOpenConns(100)
  74. readDb.LogMode(true)
  75. writeDb, err = gorm.Open("mysql", wdsn)
  76. if err != nil {
  77. beego.Error(err)
  78. }
  79. writeDb.DB().SetMaxIdleConns(10)
  80. writeDb.DB().SetMaxOpenConns(100)
  81. writeDb.LogMode(true)
  82. readUserDb, err = gorm.Open("mysql", rudsn)
  83. if err != nil {
  84. beego.Error(err)
  85. }
  86. readUserDb.DB().SetMaxIdleConns(10)
  87. readUserDb.DB().SetMaxOpenConns(100)
  88. readUserDb.LogMode(true)
  89. writeUserDb, err = gorm.Open("mysql", wudsn)
  90. if err != nil {
  91. beego.Error(err)
  92. }
  93. writeUserDb.DB().SetMaxIdleConns(10)
  94. writeUserDb.DB().SetMaxOpenConns(100)
  95. writeUserDb.LogMode(true)
  96. readXTDb, err = gorm.Open("mysql", rxdsn)
  97. if err != nil {
  98. beego.Error(err)
  99. }
  100. readXTDb.DB().SetMaxIdleConns(10)
  101. readXTDb.DB().SetMaxOpenConns(100)
  102. readXTDb.LogMode(true)
  103. writeXTDb, err = gorm.Open("mysql", wxdsn)
  104. if err != nil {
  105. beego.Error(err)
  106. }
  107. writeXTDb.DB().SetMaxIdleConns(10)
  108. writeXTDb.DB().SetMaxOpenConns(100)
  109. writeXTDb.LogMode(true)
  110. // gormDb,err = gorm.Open("mysql", "shenyh:syh@#$%123456!@tcp(112.74.16.180:3306)/sgj_patient?charset=utf8mb4&parseTime=true&loc=Local")
  111. // gormDb.DB().SetMaxIdleConns(10)
  112. // gormDb.DB().SetMaxOpenConns(100)
  113. }
  114. //func DisconnectDB() {
  115. // if err := readDb.Close(); nil != err {
  116. // beego.Error("Disconnect from database failed: " + err.Error())
  117. // }
  118. //}