background/db/sqlManager.go

225 lines
5.5 KiB
Go
Raw Normal View History

2019-01-25 09:11:15 +00:00
package db
import (
2019-06-23 11:33:45 +00:00
"background/config"
"background/logs"
"crypto/tls"
"crypto/x509"
2019-01-25 09:11:15 +00:00
"database/sql"
"fmt"
2020-08-23 18:08:26 +00:00
"io/ioutil"
"log"
"os"
2020-08-30 10:44:29 +00:00
"time"
2020-08-23 18:08:26 +00:00
2019-06-23 11:33:45 +00:00
"github.com/go-sql-driver/mysql"
2019-01-25 09:11:15 +00:00
_ "github.com/go-sql-driver/mysql"
2020-08-23 18:08:26 +00:00
"github.com/jinzhu/gorm"
2019-06-23 11:33:45 +00:00
"gopkg.in/mgo.v2"
"gopkg.in/olivere/elastic.v7"
)
2020-08-23 18:08:26 +00:00
var gEla ElkEngine
2019-01-25 09:11:15 +00:00
var gDb Database
2019-05-16 10:05:20 +00:00
var blogDb Database
2019-06-23 11:33:45 +00:00
var gMongo *mgo.Session
var gElkEngine ElkEngine
2020-08-23 18:08:26 +00:00
var gOrm *gorm.DB
2019-03-07 02:36:09 +00:00
func Init() {
2020-08-23 18:08:26 +00:00
var e error
2019-01-25 09:11:15 +00:00
mysqlconf := config.GetMysqlConfig()
2019-10-06 06:03:37 +00:00
//InitMongoDb()
2020-08-23 18:08:26 +00:00
log.Print("api runmode is " + config.ApiConfig().RunMode)
if config.ApiConfig().RunMode == "debug" {
2020-08-30 10:44:29 +00:00
gDb = Database{Type: string(""), DB: initMysqlTLS(mysqlconf)}
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,
})
// try to connect to mysql database.
cfg := mysql.Config{
User: config.GetMysqlBlogConfig().UserName,
Passwd: config.GetMysqlBlogConfig().Password,
Addr: config.GetMysqlBlogConfig().Addr, //IP:PORT
Net: "tcp",
DBName: "background",
Loc: time.Local,
AllowNativePasswords: true,
}
cfg.TLSConfig = "custom"
str := cfg.FormatDSN()
gOrm, e = gorm.Open("mysql", str)
2020-08-23 18:08:26 +00:00
if nil != e {
log.Print(e.Error())
os.Exit(-1)
}
} else {
2020-08-30 10:44:29 +00:00
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)
log.Print("client cert is ", clientCert)
mysql.RegisterTLSConfig("custom", &tls.Config{
RootCAs: rootCertPool,
Certificates: clientCert,
})
// try to connect to mysql database.
cfg := mysql.Config{
User: config.GetMysqlBlogConfig().UserName,
Passwd: config.GetMysqlBlogConfig().Password,
Addr: config.GetMysqlBlogConfig().Addr, //IP:PORT
Net: "tcp",
DBName: "background",
Loc: time.Local,
AllowNativePasswords: true,
}
cfg.TLSConfig = "custom"
str := cfg.FormatDSN()
log.Print(str)
gOrm, e = gorm.Open("mysql", str)
2020-08-23 18:08:26 +00:00
if nil != e {
log.Print(e.Error())
os.Exit(-1)
}
2020-01-25 14:26:41 +00:00
}
2020-08-23 18:08:26 +00:00
2020-03-25 02:57:25 +00:00
InitELK()
2019-05-16 10:05:20 +00:00
}
2019-06-23 11:33:45 +00:00
2020-08-23 18:08:26 +00:00
func InitELK() {
var e error
elkconf := config.GetElkConfig()
2020-03-25 02:57:25 +00:00
log.Print(elkconf)
2020-08-23 18:08:26 +00:00
gElkEngine.cli, e = elastic.NewClient(
elastic.SetURL(elkconf.Address),
// Must turn off sniff in docker
2020-08-23 18:08:26 +00:00
elastic.SetSniff(false))
if nil != e {
logs.Error(e.Error())
gElkEngine.cli = nil
}
}
2019-06-23 11:33:45 +00:00
2019-05-16 10:05:20 +00:00
func initMysql(mysqlconf *config.MysqlConfig) *sql.DB {
2019-06-23 11:33:45 +00:00
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, err := sql.Open("mysql", cnn)
if err != nil {
fmt.Println("connect sql server ", err.Error())
os.Exit(200)
}
e := _db.Ping()
if nil != e {
2020-08-30 10:44:29 +00:00
fmt.Println("ping mysql server error", e.Error())
2019-06-23 11:33:45 +00:00
os.Exit(200)
}
2020-08-23 18:08:26 +00:00
return _db
2019-06-23 11:33:45 +00:00
}
2020-01-25 14:26:41 +00:00
func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB {
2019-06-23 11:33:45 +00:00
rootCertPool := x509.NewCertPool()
2019-06-30 14:59:00 +00:00
pem, err := ioutil.ReadFile("pem/ca.pem")
2019-06-23 11:33:45 +00:00
if err != nil {
log.Fatal(err)
}
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
log.Fatal("Failed to append PEM.")
}
clientCert := make([]tls.Certificate, 0, 1)
2019-06-30 14:59:00 +00:00
certs, err := tls.LoadX509KeyPair("pem/client-cert.pem", "pem/client-key.pem")
2019-06-23 11:33:45 +00:00
if err != nil {
log.Fatal(err)
}
clientCert = append(clientCert, certs)
mysql.RegisterTLSConfig("custom", &tls.Config{
2020-08-23 18:08:26 +00:00
RootCAs: rootCertPool,
Certificates: clientCert,
2019-06-23 11:33:45 +00:00
InsecureSkipVerify: true,
})
cnn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&tls=custom", mysqlconf.UserName, mysqlconf.Password,
2019-03-07 02:36:09 +00:00
mysqlconf.Addr, mysqlconf.Db)
2019-06-23 11:33:45 +00:00
log.Print("Connect to mysql " + cnn)
2019-03-07 02:36:09 +00:00
_db, err := sql.Open("mysql", cnn)
if err != nil {
fmt.Println("connect sql server ", err.Error())
2019-06-23 11:33:45 +00:00
os.Exit(200)
2019-01-25 09:11:15 +00:00
}
e := _db.Ping()
2019-03-07 02:36:09 +00:00
if nil != e {
2019-01-25 09:11:15 +00:00
fmt.Println(e.Error())
2019-06-23 11:33:45 +00:00
os.Exit(200)
2019-01-25 09:11:15 +00:00
}
2020-08-23 18:08:26 +00:00
return _db
2019-01-25 09:11:15 +00:00
}
2020-08-23 18:08:26 +00:00
func initMongoDb(conf *config.MongoConfig) *mgo.Session {
2019-10-05 17:25:44 +00:00
var url string
2020-08-23 18:08:26 +00:00
if conf.UserName == "" {
url = fmt.Sprintf("mongodb://%s:%d/%s", conf.Addr, conf.Port, conf.Db)
2019-10-05 17:25:44 +00:00
2020-08-23 18:08:26 +00:00
} else {
url = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s", conf.UserName, conf.Password, conf.Addr, conf.Port, conf.Db)
2019-10-05 17:25:44 +00:00
}
2019-06-23 11:33:45 +00:00
log.Print("connect to url " + url)
logs.Debug("connect to url " + url)
2020-08-23 18:08:26 +00:00
mgo, err := mgo.Dial(url)
if nil != err {
2019-06-23 11:33:45 +00:00
logs.Error(err.Error())
return nil
}
return mgo
}
2019-01-25 09:11:15 +00:00
func GetMysqlClient() *Database {
return &gDb
}
2020-08-23 18:08:26 +00:00
func GetBlogMysql() *Database {
return &blogDb
2019-06-23 11:33:45 +00:00
}
func GetMongoDb() *mgo.Session {
return gMongo
}
2020-08-23 18:08:26 +00:00
func InitMongoDb() {
2019-06-23 11:33:45 +00:00
mongoConf := config.GetMongoDBConfig()
gMongo = initMongoDb(mongoConf)
2020-08-23 18:08:26 +00:00
if gMongo == nil {
2019-06-23 11:33:45 +00:00
log.Print("error mongo initial")
os.Exit(25)
}
}
2020-08-23 18:08:26 +00:00
func GetElastic() *ElkEngine {
return &gElkEngine
2020-08-23 18:08:26 +00:00
}
func GetOrm() *gorm.DB {
return gOrm
}