From dc1e6c6f5bcf0b5f35dc722c4eff5f9f20dc16a5 Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Sat, 25 Jan 2020 22:26:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=88=86=E7=B1=BB=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=AE=9E=E7=8E=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 3 +++ db/sqlManager.go | 11 +++++++---- main.go | 1 - model/blog.go | 7 +++++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index 4aba4b0..63dc0e7 100644 --- a/config/config.go +++ b/config/config.go @@ -65,6 +65,9 @@ type MongoConfig struct { var gConf ConfAPI +func ApiConfig() ConfAPI{ + return gConf +} func Init(path string) error { file, e := os.Open(path) if nil != e { diff --git a/db/sqlManager.go b/db/sqlManager.go index 45d189e..4a94e14 100644 --- a/db/sqlManager.go +++ b/db/sqlManager.go @@ -23,7 +23,12 @@ func Init() { blogConf := config.GetMysqlBlogConfig() //InitMongoDb() blogDb = Database{Type: string(""), DB: initMysql(blogConf)} - gDb = Database{Type: string(""), DB: initMysqlTLS(mysqlconf)} + if config.ApiConfig().RunMode == "debug"{ + gDb = Database{Type: string(""), DB: initMysql(mysqlconf)} + + }else{ + gDb = Database{Type: string(""), DB: initMysqlTLS(mysqlconf)} + } } @@ -44,8 +49,8 @@ func initMysql(mysqlconf *config.MysqlConfig) *sql.DB { } return _db } -func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB { +func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB { rootCertPool := x509.NewCertPool() pem, err := ioutil.ReadFile("pem/ca.pem") if err != nil { @@ -65,7 +70,6 @@ func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB { 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) @@ -73,7 +77,6 @@ func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB { if err != nil { fmt.Println("connect sql server ", err.Error()) os.Exit(200) - } e := _db.Ping() if nil != e { diff --git a/main.go b/main.go index c3c3c6e..57491fc 100644 --- a/main.go +++ b/main.go @@ -110,7 +110,6 @@ func main() { api.GET("/image_download/:file",fileController.OnDownLoad) // 下载图片 api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型 - } e := r.Run(":" + strconv.Itoa(config.GetPort())) diff --git a/model/blog.go b/model/blog.go index 836c3fb..acf24b2 100644 --- a/model/blog.go +++ b/model/blog.go @@ -4,6 +4,7 @@ import ( "background/db" "background/logs" "fmt" + "qiniupkg.com/x/log.v7" "strings" ) @@ -17,20 +18,22 @@ type Doc struct { type ArticleType struct { Id int64 `sql:"id" json:"id"` - Name string `sql:"name" json:"name"` + Name string `sql:"type_name" json:"type_name"` Author string `sql:"author" json:"author"` } func GetArticlesType() []ArticleType { ret := []ArticleType{} - sql := fmt.Sprintf("select * from article_type") + sql := fmt.Sprintf("select * from doc_type") e := db.GetBlogMysql().Query2(sql, &ret) + log.Print(ret) if nil != e { logs.Error(e.Error()) return nil } return ret } + func CreateDoc(doc Doc) error { sql := fmt.Sprintf(`INSERT INTO doc ( doc.title, doc.content, doc.author, doc.type ) SELECT '%s',