sso

db.go 3.7KB

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