// 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 . 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 err error func ConnectDB() { readHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com" readPort := beego.AppConfig.String("readmysqlport") readUser := "kuyicloud" readPass := "yHXAcb7dTJyvMiX3vK2iOvTAHeaowy5P" readName := beego.AppConfig.String("readmysqlname") writeHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com" writePort := beego.AppConfig.String("writemysqlport") writeUser := "kuyicloud" writePass := "yHXAcb7dTJyvMiX3vK2iOvTAHeaowy5P" writeName := beego.AppConfig.String("writemysqlname") readUserHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com" readUserPort := beego.AppConfig.String("readuserport") readUserUser := "kuyicloud" readUserPass := "yHXAcb7dTJyvMiX3vK2iOvTAHeaowy5P" readUserName := beego.AppConfig.String("readusername") writeUserHost := "kuyicloud2024lt.mysql.rds.aliyuncs.com" writeUserPort := beego.AppConfig.String("writeuserport") writeUserUser := "kuyicloud" writeUserPass := "yHXAcb7dTJyvMiX3vK2iOvTAHeaowy5P" writeUserName := beego.AppConfig.String("readusername") 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) } //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 UserReadDB() *gorm.DB { return readUserDb } func UserWriteDB() *gorm.DB { return writeUserDb }