diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 2b28593..f8824f8 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -3,9 +3,11 @@
-
+
+
+
@@ -14,62 +16,114 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -77,8 +131,8 @@
-
-
+
+
@@ -86,11 +140,14 @@
-
+
-
-
+
+
+
+
+
@@ -98,21 +155,12 @@
-
+
-
-
-
-
-
-
-
-
-
@@ -122,24 +170,41 @@
-
+
+
+ rootCertPool
+ clientCert
+
+
+
-
-
-
-
-
+
+
+
+ true
+ DEFINITION_ORDER
+
+
+
+
+
+
@@ -147,7 +212,17 @@
-
+
+
+
+
+
+
+
+
+
+
+
@@ -159,11 +234,11 @@
-
+
-
+
@@ -195,23 +270,22 @@
-
-
+
-
+
-
-
+
+
-
+
@@ -225,41 +299,26 @@
+
+
+
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -271,48 +330,107 @@
-
+
-
-
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
-
-
-
-
-
-
-
-
-
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/config/config.go b/config/config.go
index 401c350..aefcf04 100644
--- a/config/config.go
+++ b/config/config.go
@@ -12,15 +12,16 @@ var ostype = runtime.GOOS
var conf ConfAPI
type ConfAPI struct {
- ListenSvr int `yaml:"listen_svr"` // 服务监听端口
- ListenApi int `yaml:"listen_api"` // 服务监听端口
- RunMode string `yaml:"runmode"` // 服务运行模式
- MaxConn int `yaml:"max_conn"`
- Logs LogConfig `yaml:"logs"` // 日志
- Redis1 EntityRedis `yaml:"redis1"`
- Mysql MysqlConfig `yaml:"mysql"` // 认证配置
+ ListenSvr int `yaml:"listen_svr"` // 服务监听端口
+ ListenApi int `yaml:"listen_api"` // 服务监听端口
+ RunMode string `yaml:"runmode"` // 服务运行模式
+ MaxConn int `yaml:"max_conn"`
+ Logs LogConfig `yaml:"logs"` // 日志
+ Redis1 EntityRedis `yaml:"redis1"`
+ Mysql MysqlConfig `yaml:"mysql"` // 认证配置
Mysql1 MysqlConfig `yaml:"mysql1"` // 认证配置
-
+ MongoConf MongoConfig `yaml:"mongo"`
+ CaCert string `yaml:"ca_cert"`
init bool
}
@@ -49,6 +50,16 @@ type MysqlConfig struct {
MaxOpen int `yaml:"max_open"`
MaxIdle int `yaml:"max_idle"`
}
+// mongodb://myuser:mypass@localhost:40001
+type MongoConfig struct {
+ Addr string `yaml:"addr"`
+ Port int64 `yaml:"port"`
+ UserName string `yaml:"user"`
+ Password string `yaml:"password"`
+ Db string `yaml:"db"`
+ MaxOpen int `yaml:"max_open"`
+ MaxIdle int `yaml:"max_idle"`
+}
var gConf ConfAPI
@@ -90,6 +101,16 @@ func GetMysqlBlogConfig() *MysqlConfig {
return nil
}
}
+func GETCaCert() string{
+ return gConf.CaCert
+}
+func GetMongoDBConfig() *MongoConfig{
+ if gConf.init{
+ return &gConf.MongoConf
+ } else {
+ return nil
+ }
+}
func GetRedis1() *EntityRedis {
if gConf.init {
return &gConf.Redis1
@@ -104,3 +125,4 @@ func GetLogConfig() *LogConfig {
return nil
}
}
+
diff --git a/db/sqlManager.go b/db/sqlManager.go
index 6f03c03..60e5758 100644
--- a/db/sqlManager.go
+++ b/db/sqlManager.go
@@ -1,38 +1,115 @@
package db
import (
+ "background/config"
+ "background/logs"
+ "crypto/tls"
+ "crypto/x509"
"database/sql"
"fmt"
+ "github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"
- "background/config"
+ "gopkg.in/mgo.v2"
+ "io/ioutil"
+ "log"
+ "os"
)
var gDb Database
var blogDb Database
-
+var gMongo *mgo.Session
func Init() {
mysqlconf := config.GetMysqlConfig()
blogConf := config.GetMysqlBlogConfig()
-
- gDb = Database{Type: string(""), DB: initMysql(mysqlconf)}
+ InitMongoDb()
+ gDb = Database{Type: string(""), DB: initMysqlTLS(mysqlconf)}
blogDb = Database{Type: string(""), DB: initMysql(blogConf)}
}
+
+
func initMysql(mysqlconf *config.MysqlConfig) *sql.DB {
- cnn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", mysqlconf.UserName, mysqlconf.Password,
+ 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 {
fmt.Println(e.Error())
+ os.Exit(200)
}
return _db
}
+func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB {
+
+ rootCertPool := x509.NewCertPool()
+ pem, err := ioutil.ReadFile("D:\\data\\腾讯云测试服mysql证书\\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("D:\\data\\腾讯云测试服mysql证书\\client-cert.pem", "D:\\data\\腾讯云测试服mysql证书\\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{
+ 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)
+ }
+
}
\ No newline at end of file
diff --git a/main.go b/main.go
index bfe4025..ce1ccc8 100644
--- a/main.go
+++ b/main.go
@@ -64,6 +64,9 @@ func main() {
InitRedis()
InitMysql()
+ mgo := db.GetMongoDb()
+ mgo = mgo
+ config.GetMongoDBConfig()
r := gin.Default()
store := sessions.NewCookieStore([]byte("secret123"))
r.Use(sessions.Middleware("sess_store", store))
diff --git a/model/blog.go b/model/blog.go
index e999cb6..e76c2ae 100644
--- a/model/blog.go
+++ b/model/blog.go
@@ -31,9 +31,9 @@ func GetArticlesType() []ArticleType{
return ret
}
func CreateDoc(doc Doc) error {
-
- sql := fmt.Sprintf("insert into doc(title,type,content,author) values ('%s','%d','%s','%s')",doc.Title,doc.Type,doc.Content,doc.Author)
- _,e := db.GetBlogMysql().Query(sql)
+ sql := fmt.Sprintf(`insert into doc(title,type,content,author) values ('%s','%d','%s','%s')
+ on duplicate key update title = '%s',type = '%d',content = '%s',author = '%s';`,doc.Title,doc.Type,doc.Content,doc.Author,doc.Title,doc.Type,doc.Content,doc.Author)
+ _,e := db.GetMysqlClient().Query(sql)
if nil != e{
logs.Error(e.Error())
return e
diff --git a/model/model.go b/model/model.go
index fcfc951..572aead 100644
--- a/model/model.go
+++ b/model/model.go
@@ -18,7 +18,7 @@ type Users struct {
DisplayName string `sql:"display_name" json:"display_name"`
EmailAddress string `sql:"email_address" json:"email_address"`
Tel string `sql:"tel" json:"tel"`
- Avatar string `sql:"Avatar" json:"avatar"`
+ Avatar string `sql:"avatar" json:"avatar"`
}
func GetUsers(limit int32, offsetPage int32, name string) ([]Users, int32) {
diff --git a/user.yaml b/user.yaml
index 5e893f3..83ea2ff 100644
--- a/user.yaml
+++ b/user.yaml
@@ -12,16 +12,43 @@ redis1:
db: 1
mysql:
addr: 118.24.238.198
- user: server
- password: server
+ user: caiyu
+ password: 123456
db: background
max_open: 100
MaxIdle: 99
-mysql2:
- addr: 35.240.179.248
+mysql1:
+ addr: 127.0.0.1
user: root
- password: a7458969
+ password:
db: blog
max_open: 100
MaxIdle: 99
-
+mongo:
+ addr: 118.24.238.198
+ port: 27017
+ user: root
+ password: root
+ db: test
+ max_open: 100
+ MaxIdle: 99
+ca_cert: "
+ -----BEGIN CERTIFICATE-----
+ MIIDAzCCAeugAwIBAgIBATANBgkqhkiG9w0BAQsFADA8MTowOAYDVQQDDDFNeVNR
+ TF9TZXJ2ZXJfNS43LjIzX0F1dG9fR2VuZXJhdGVkX0NBX0NlcnRpZmljYXRlMB4X
+ DTE5MDYwNDE2NDE1N1oXDTI5MDYwMTE2NDE1N1ow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