blog_backend_api/db/sqlManager.go

131 lines
3.0 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"
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"
2019-06-23 11:33:45 +00:00
"gopkg.in/mgo.v2"
"io/ioutil"
"log"
"os"
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
2019-03-07 02:36:09 +00:00
func Init() {
2019-01-25 09:11:15 +00:00
mysqlconf := config.GetMysqlConfig()
2019-05-16 10:05:20 +00:00
blogConf := config.GetMysqlBlogConfig()
2019-10-06 06:03:37 +00:00
//InitMongoDb()
fmt.Print("api runmode is " + config.ApiConfig().RunMode)
if config.ApiConfig().RunMode == "debug"{
2020-03-20 15:13:04 +00:00
blogDb = Database{Type: string(""), DB: initMysqlTLS(blogConf)}
}else{
blogDb = Database{Type: string(""), DB: initMysqlTLS(blogConf)}
}
2020-01-25 14:26:41 +00:00
if config.ApiConfig().RunMode == "debug"{
2020-03-20 15:13:04 +00:00
gDb = Database{Type: string(""), DB: initMysqlTLS(mysqlconf)}
2020-01-25 14:26:41 +00:00
}else{
gDb = Database{Type: string(""), DB: initMysqlTLS(mysqlconf)}
}
2019-05-16 10:05:20 +00:00
}
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)
2020-02-10 13:47:28 +00:00
//db := mysql.New(proto, "", addr, user, pass, dbname)
2019-06-23 11:33:45 +00:00
_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
}
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{
RootCAs: rootCertPool,
Certificates: clientCert,
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
}
2019-05-16 10:05:20 +00:00
return _db
2019-01-25 09:11:15 +00:00
}
2019-06-23 11:33:45 +00:00
func initMongoDb(conf *config.MongoConfig) *mgo.Session{
2019-10-05 17:25:44 +00:00
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)
}
2019-06-23 11:33:45 +00:00
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
}
2019-01-25 09:11:15 +00:00
func GetMysqlClient() *Database {
return &gDb
}
2019-05-16 10:05:20 +00:00
func GetBlogMysql() *Database{
return &blogDb
2019-06-23 11:33:45 +00:00
}
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)
}
2019-05-16 10:05:20 +00:00
}