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',