123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- // Pipe - A small and beautiful blogging platform written in golang.
- // Copyright (C) 2017-2018, b3log.org
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
-
- package service
-
- import (
- "fmt"
-
- "github.com/astaxie/beego"
- "github.com/jinzhu/gorm"
- _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
- )
-
- var readDb *gorm.DB
- var writeDb *gorm.DB
- var readUserDb *gorm.DB
- var writeUserDb *gorm.DB
- var readXTDb *gorm.DB
- var writeXTDb *gorm.DB
- var err error
-
- // var gormDb *gorm.DB
-
- func ConnectDB() {
- readHost := beego.AppConfig.String("readmysqlhost")
- readPort := beego.AppConfig.String("readmysqlport")
- readUser := beego.AppConfig.String("readmysqluser")
- readPass := beego.AppConfig.String("readmysqlpass")
- readName := beego.AppConfig.String("readmysqlname")
-
- writeHost := beego.AppConfig.String("writemysqlhost")
- writePort := beego.AppConfig.String("writemysqlport")
- writeUser := beego.AppConfig.String("writemysqluser")
- writePass := beego.AppConfig.String("writemysqlpass")
- writeName := beego.AppConfig.String("writemysqlname")
-
- readUserHost := beego.AppConfig.String("readuserhost")
- readUserPort := beego.AppConfig.String("readuserport")
- readUserUser := beego.AppConfig.String("readuseruser")
- readUserPass := beego.AppConfig.String("readuserpass")
- readUserName := beego.AppConfig.String("readusername")
-
- writeUserHost := beego.AppConfig.String("writeuserhost")
- writeUserPort := beego.AppConfig.String("writeuserport")
- writeUserUser := beego.AppConfig.String("writeuseruser")
- writeUserPass := beego.AppConfig.String("writeuserpass")
- writeUserName := beego.AppConfig.String("writeusername")
-
- readXTHost := beego.AppConfig.String("readsqlhost")
- readXTPort := beego.AppConfig.String("readsqlport")
- readXTUser := beego.AppConfig.String("readsqluser")
- readXTPass := beego.AppConfig.String("readsqlpass")
- readXTName := beego.AppConfig.String("readsqlname")
-
- writeXTHost := beego.AppConfig.String("writesqlhost")
- writeXTPort := beego.AppConfig.String("writesqlport")
- writeXTUser := beego.AppConfig.String("writesqluser")
- writeXTPass := beego.AppConfig.String("writesqlpass")
- writeXTName := beego.AppConfig.String("writesqlname")
-
- rxdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readXTUser, readXTPass, readXTHost, readXTPort, readXTName)
- wxdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeXTUser, writeXTPass, writeXTHost, writeXTPort, writeXTName)
-
- rdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUser, readPass, readHost, readPort, readName)
- wdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUser, writePass, writeHost, writePort, writeName)
-
- rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
- wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
-
- readDb, err = gorm.Open("mysql", rdsn)
- if err != nil {
- beego.Error(err)
- }
- readDb.DB().SetMaxIdleConns(10)
- readDb.DB().SetMaxOpenConns(100)
- readDb.LogMode(true)
-
- writeDb, err = gorm.Open("mysql", wdsn)
- if err != nil {
- beego.Error(err)
- }
- writeDb.DB().SetMaxIdleConns(10)
- writeDb.DB().SetMaxOpenConns(100)
- writeDb.LogMode(true)
-
- readUserDb, err = gorm.Open("mysql", rudsn)
- if err != nil {
- beego.Error(err)
- }
- readUserDb.DB().SetMaxIdleConns(10)
- readUserDb.DB().SetMaxOpenConns(100)
- readUserDb.LogMode(true)
-
- writeUserDb, err = gorm.Open("mysql", wudsn)
- if err != nil {
- beego.Error(err)
- }
- writeUserDb.DB().SetMaxIdleConns(10)
- writeUserDb.DB().SetMaxOpenConns(100)
- writeUserDb.LogMode(true)
-
- readXTDb, err = gorm.Open("mysql", rxdsn)
- if err != nil {
- beego.Error(err)
- }
- readXTDb.DB().SetMaxIdleConns(10)
- readXTDb.DB().SetMaxOpenConns(100)
- readXTDb.LogMode(true)
-
- writeXTDb, err = gorm.Open("mysql", wxdsn)
- if err != nil {
- beego.Error(err)
- }
- writeXTDb.DB().SetMaxIdleConns(10)
- writeXTDb.DB().SetMaxOpenConns(100)
- writeXTDb.LogMode(true)
-
- // gormDb,err = gorm.Open("mysql", "shenyh:syh@#$%123456!@tcp(112.74.16.180:3306)/sgj_patient?charset=utf8mb4&parseTime=true&loc=Local")
- // gormDb.DB().SetMaxIdleConns(10)
- // gormDb.DB().SetMaxOpenConns(100)
-
- }
-
- //func DisconnectDB() {
- // if err := readDb.Close(); nil != err {
- // beego.Error("Disconnect from database failed: " + err.Error())
- // }
- //}
|