blog_backend_api/db/sqlManager.go

170 lines
3.8 KiB
Go

package db
import (
"background/config"
"background/logs"
"crypto/tls"
"crypto/x509"
"database/sql"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"gopkg.in/mgo.v2"
"gopkg.in/olivere/elastic.v7"
)
var gEla ElkEngine
var gDb Database
var blogDb Database
var gMongo *mgo.Session
var gElkEngine ElkEngine
var gOrm *gorm.DB
func Init() {
var e error
mysqlconf := config.GetMysqlConfig()
//InitMongoDb()
log.Print("api runmode is " + config.ApiConfig().RunMode)
if config.ApiConfig().RunMode == "debug" {
gDb = Database{Type: string(""), DB: initMysql(mysqlconf)}
sqls := fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local",
mysqlconf.UserName, mysqlconf.Password, mysqlconf.Addr,
mysqlconf.Db)
log.Print(sqls)
gOrm, e = gorm.Open("mysql", sqls)
if nil != e {
log.Print(e.Error())
os.Exit(-1)
}
} else {
sqls := fmt.Sprintf("%s:%s@/%s?charset=utf8&parseTime=True&loc=Local", mysqlconf.UserName, mysqlconf.Password,
mysqlconf.Db)
gOrm, e = gorm.Open("mysql", sqls)
if nil != e {
log.Print(e.Error())
os.Exit(-1)
}
}
InitELK()
}
func InitELK() {
var e error
elkconf := config.GetElkConfig()
log.Print(elkconf)
gElkEngine.cli, e = elastic.NewClient(
elastic.SetURL(elkconf.Address),
// Must turn off sniff in docker
elastic.SetSniff(false))
if nil != e {
logs.Error(e.Error())
gElkEngine.cli = nil
}
}
func initMysql(mysqlconf *config.MysqlConfig) *sql.DB {
cnn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8", mysqlconf.UserName, mysqlconf.Password,
mysqlconf.Addr, mysqlconf.Db)
log.Print("Connect to mysql " + cnn)
//db := mysql.New(proto, "", addr, user, pass, dbname)
_db, err := sql.Open("mysql", cnn)
if err != nil {
fmt.Println("connect sql server ", err.Error())
os.Exit(200)
}
e := _db.Ping()
if nil != e {
fmt.Println(e.Error())
os.Exit(200)
}
return _db
}
func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB {
rootCertPool := x509.NewCertPool()
pem, err := ioutil.ReadFile("pem/ca.pem")
if err != nil {
log.Fatal(err)
}
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
log.Fatal("Failed to append PEM.")
}
clientCert := make([]tls.Certificate, 0, 1)
certs, err := tls.LoadX509KeyPair("pem/client-cert.pem", "pem/client-key.pem")
if err != nil {
log.Fatal(err)
}
clientCert = append(clientCert, certs)
mysql.RegisterTLSConfig("custom", &tls.Config{
RootCAs: rootCertPool,
Certificates: clientCert,
InsecureSkipVerify: true,
})
cnn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&tls=custom", mysqlconf.UserName, mysqlconf.Password,
mysqlconf.Addr, mysqlconf.Db)
log.Print("Connect to mysql " + cnn)
_db, err := sql.Open("mysql", cnn)
if err != nil {
fmt.Println("connect sql server ", err.Error())
os.Exit(200)
}
e := _db.Ping()
if nil != e {
fmt.Println(e.Error())
os.Exit(200)
}
return _db
}
func initMongoDb(conf *config.MongoConfig) *mgo.Session {
var url string
if conf.UserName == "" {
url = fmt.Sprintf("mongodb://%s:%d/%s", conf.Addr, conf.Port, conf.Db)
} else {
url = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s", conf.UserName, conf.Password, conf.Addr, conf.Port, conf.Db)
}
log.Print("connect to url " + url)
logs.Debug("connect to url " + url)
mgo, err := mgo.Dial(url)
if nil != err {
logs.Error(err.Error())
return nil
}
return mgo
}
func GetMysqlClient() *Database {
return &gDb
}
func GetBlogMysql() *Database {
return &blogDb
}
func GetMongoDb() *mgo.Session {
return gMongo
}
func InitMongoDb() {
mongoConf := config.GetMongoDBConfig()
gMongo = initMongoDb(mongoConf)
if gMongo == nil {
log.Print("error mongo initial")
os.Exit(25)
}
}
func GetElastic() *ElkEngine {
return &gElkEngine
}
func GetOrm() *gorm.DB {
return gOrm
}