123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- // 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/mssql" // mssql
- _ "github.com/jinzhu/gorm/dialects/mysql" // mysql
- )
-
- var readDb *gorm.DB
- var writeDb *gorm.DB
- var tempwriteDb *gorm.DB
- var readRemoteDb *gorm.DB
- var writeUserDb *gorm.DB
- var readUserDb *gorm.DB
-
- var readMiddleDb *gorm.DB
- var writeMiddleDb *gorm.DB
- var backupDb *gorm.DB
- var err error
-
- var blDb *gorm.DB
- var shDb *gorm.DB
-
- func CreateSqlServiceDB(Host string, Port string, User string, Pass string, Name string) (mssqlDb *gorm.DB, err error) {
- //rdb := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", User, Pass, Host, Port, Name)
- mssqlstring := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s;encrypt=disable", Host, Port, Name, User, Pass)
- fmt.Println(mssqlstring)
- mssqlDb, err = gorm.Open("mssql", mssqlstring)
- if err != nil {
- //panic(err)
- }
- //mssqlDb.DB().SetMaxIdleConns(5)
- //mssqlDb.DB().SetMaxOpenConns(20)
- //mssqlDb.LogMode(true)
- return
- }
-
- func CloseDB(DB *gorm.DB) {
- DB.Close()
- }
-
- func ConnectDB() {
-
- readHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
- readPort := beego.AppConfig.String("readmysqlport")
- readUser := "syh"
- readPass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
- readName := beego.AppConfig.String("readmysqlname")
-
- writeHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
- writePort := beego.AppConfig.String("writemysqlport")
- writeUser := "syh"
- writePass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
- writeName := beego.AppConfig.String("writemysqlname")
-
- // tempwriteHost := beego.AppConfig.String("tempwritemysqlhost")
- // tempwritePort := beego.AppConfig.String("tempwritemysqlport")
- // tempwriteUser := beego.AppConfig.String("tempwritemysqluser")
- // tempwritePass := beego.AppConfig.String("tempwritemysqlpass")
- // tempwriteName := beego.AppConfig.String("tempwritemysqlname")
-
- readUserHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
- readUserPort := beego.AppConfig.String("readuserport")
- readUserUser := "syh"
- readUserPass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
- readUserName := beego.AppConfig.String("readusername")
-
- // readRemoteHost := beego.AppConfig.String("readremotehost")
- // readRemotePort := beego.AppConfig.String("readremoteport")
- // readRemoteUser := beego.AppConfig.String("readremoteuser")
- // readRemotePass := beego.AppConfig.String("readremotepass")
- // readRemoteName := beego.AppConfig.String("readremotename")
-
- writeUserHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
- writeUserPort := beego.AppConfig.String("writeuserport")
- writeUserUser := "syh"
- writeUserPass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
- writeUserName := beego.AppConfig.String("writeusername")
-
- readMiddleHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
- readMiddlePort := beego.AppConfig.String("readmiddleport")
- readMiddleUser := "syh"
- readMiddlePass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
- readMiddleName := beego.AppConfig.String("readmiddlename")
-
- writeMiddleHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com"
- writeMiddlePort := beego.AppConfig.String("writemiddleport")
- writeMiddleUser := "syh"
- writeMiddlePass := "Zc8xxwN4jkZswprx8VKHRSd2rBMRWgC4"
- writeMiddleName := beego.AppConfig.String("writemiddlename")
-
- // backupHost := beego.AppConfig.String("backuphost")
- // backupPort := beego.AppConfig.String("backupport")
- // backupUser := beego.AppConfig.String("backupuser")
- // backupPass := beego.AppConfig.String("backuppass")
- // backupName := beego.AppConfig.String("backupname")
-
- // blHost := beego.AppConfig.String("blmysqlhost")
- // blPort := beego.AppConfig.String("blmysqlport")
- // blUser := beego.AppConfig.String("blmysqluser")
- // blPass := beego.AppConfig.String("blmysqlpass")
- // blName := beego.AppConfig.String("blmysqlname")
-
- // fmt.Println(blHost)
- // fmt.Println(blPort)
- // fmt.Println(blUser)
- // fmt.Println(blPass)
- // fmt.Println(blName)
-
- // shHost := beego.AppConfig.String("shmysqlhost")
- // shPort := beego.AppConfig.String("shmysqlport")
- // shUser := beego.AppConfig.String("shmysqluser")
- // shPass := beego.AppConfig.String("shmysqlpass")
- // shName := beego.AppConfig.String("shmysqlname")
-
- 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)
- // tempwdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", tempwriteUser, tempwritePass, tempwriteHost, tempwritePort, tempwriteName)
-
- // rudsn := fmt.Sprintf("server=%s;port=%s;database=%s;user id=%s;password=%s", readRemoteHost, readRemotePort, readRemoteName, readRemoteUser, readRemotePass)
- wudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeUserUser, writeUserPass, writeUserHost, writeUserPort, writeUserName)
-
- rmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readMiddleUser, readMiddlePass, readMiddleHost, readMiddlePort, readMiddleName)
- wmdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", writeMiddleUser, writeMiddlePass, writeMiddleHost, writeMiddlePort, writeMiddleName)
- rudsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", readUserUser, readUserPass, readUserHost, readUserPort, readUserName)
-
- // bldsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", blUser, blPass, blHost, blPort, blName)
-
- // shdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", shUser, shPass, shHost, shPort, shName)
-
- // bdsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true", backupUser, backupPass, backupHost, backupPort, backupName)
-
- readDb, err = gorm.Open("mysql", rdsn)
- if err != nil {
- //beego.Error(err)
- }
- readDb.DB().SetMaxIdleConns(10)
- readDb.DB().SetMaxOpenConns(100)
- readDb.LogMode(true)
-
- // backupDb, err = gorm.Open("mysql", bdsn)
- // if err != nil {
- // //beego.Error(err)
- // }
- // backupDb.DB().SetMaxIdleConns(10)
- // backupDb.DB().SetMaxOpenConns(100)
- // backupDb.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)
-
- // tempwriteDb, err = gorm.Open("mysql", tempwdsn)
- // if err != nil {
- // //beego.Error(err)
- // }
- // tempwriteDb.DB().SetMaxIdleConns(10)
- // tempwriteDb.DB().SetMaxOpenConns(100)
- // tempwriteDb.LogMode(true)
-
- // fmt.Println(rudsn)
-
- // readRemoteDb, err = gorm.Open("mssql", rudsn)
- // if err != nil {
- // panic(err)
- // }
- // readRemoteDb.DB().SetMaxIdleConns(10)
- // readRemoteDb.DB().SetMaxOpenConns(100)
- // readRemoteDb.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)
-
- readMiddleDb, err = gorm.Open("mysql", rmdsn)
- if err != nil {
- //beego.Error(err)
- }
- readMiddleDb.DB().SetMaxIdleConns(10)
- readMiddleDb.DB().SetMaxOpenConns(100)
- readMiddleDb.LogMode(true)
-
- writeMiddleDb, err = gorm.Open("mysql", wmdsn)
- if err != nil {
- //beego.Error(err)
- }
- writeMiddleDb.DB().SetMaxIdleConns(10)
- writeMiddleDb.DB().SetMaxOpenConns(100)
- writeMiddleDb.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)
-
- // blDb, err = gorm.Open("mysql", bldsn)
- // if err != nil {
- // fmt.Println("12344")
- // fmt.Println(bldsn)
-
- // fmt.Println(err)
- // }
- // blDb.DB().SetMaxIdleConns(10)
- // blDb.DB().SetMaxOpenConns(100)
- // blDb.LogMode(true)
-
- // shDb, err = gorm.Open("mysql", shdsn)
- // if err != nil {
- // fmt.Println("12344")
-
- // fmt.Println(err)
- // }
- // shDb.DB().SetMaxIdleConns(10)
- // shDb.DB().SetMaxOpenConns(100)
- // shDb.LogMode(true)
-
- }
-
- //func DisconnectDB() {
- // if err := readDb.Close(); nil != err {
- // beego.Error("Disconnect from database failed: " + err.Error())
- // }
- //}
-
- func XTReadDB() *gorm.DB {
- return readDb
- }
- func XTWriteDB() *gorm.DB {
- return writeDb
- }
-
- // func RemoteReadDB() *gorm.DB {
- // return readRemoteDb
- // }
- func UserWriteDB() *gorm.DB {
- return writeUserDb
- }
-
- func MiddleReadDB() *gorm.DB {
- return readMiddleDb
- }
- func MiddleWriteDB() *gorm.DB {
- return writeMiddleDb
- }
|