添加分类管理实现。

master
a7458969 2020-01-25 22:26:41 +08:00
parent 3b2aaa604e
commit dc1e6c6f5b
4 changed files with 15 additions and 7 deletions

View File

@ -65,6 +65,9 @@ type MongoConfig struct {
var gConf ConfAPI var gConf ConfAPI
func ApiConfig() ConfAPI{
return gConf
}
func Init(path string) error { func Init(path string) error {
file, e := os.Open(path) file, e := os.Open(path)
if nil != e { if nil != e {

View File

@ -23,7 +23,12 @@ func Init() {
blogConf := config.GetMysqlBlogConfig() blogConf := config.GetMysqlBlogConfig()
//InitMongoDb() //InitMongoDb()
blogDb = Database{Type: string(""), DB: initMysql(blogConf)} 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 return _db
} }
func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB {
func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB {
rootCertPool := x509.NewCertPool() rootCertPool := x509.NewCertPool()
pem, err := ioutil.ReadFile("pem/ca.pem") pem, err := ioutil.ReadFile("pem/ca.pem")
if err != nil { if err != nil {
@ -65,7 +70,6 @@ func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB {
Certificates: clientCert, Certificates: clientCert,
InsecureSkipVerify: true, InsecureSkipVerify: true,
}) })
cnn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&tls=custom", mysqlconf.UserName, mysqlconf.Password, cnn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&tls=custom", mysqlconf.UserName, mysqlconf.Password,
mysqlconf.Addr, mysqlconf.Db) mysqlconf.Addr, mysqlconf.Db)
log.Print("Connect to mysql " + cnn) log.Print("Connect to mysql " + cnn)
@ -73,7 +77,6 @@ func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB {
if err != nil { if err != nil {
fmt.Println("connect sql server ", err.Error()) fmt.Println("connect sql server ", err.Error())
os.Exit(200) os.Exit(200)
} }
e := _db.Ping() e := _db.Ping()
if nil != e { if nil != e {

View File

@ -110,7 +110,6 @@ func main() {
api.GET("/image_download/:file",fileController.OnDownLoad) // 下载图片 api.GET("/image_download/:file",fileController.OnDownLoad) // 下载图片
api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型 api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型
} }
e := r.Run(":" + strconv.Itoa(config.GetPort())) e := r.Run(":" + strconv.Itoa(config.GetPort()))

View File

@ -4,6 +4,7 @@ import (
"background/db" "background/db"
"background/logs" "background/logs"
"fmt" "fmt"
"qiniupkg.com/x/log.v7"
"strings" "strings"
) )
@ -17,20 +18,22 @@ type Doc struct {
type ArticleType struct { type ArticleType struct {
Id int64 `sql:"id" json:"id"` 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"` Author string `sql:"author" json:"author"`
} }
func GetArticlesType() []ArticleType { func GetArticlesType() []ArticleType {
ret := []ArticleType{} ret := []ArticleType{}
sql := fmt.Sprintf("select * from article_type") sql := fmt.Sprintf("select * from doc_type")
e := db.GetBlogMysql().Query2(sql, &ret) e := db.GetBlogMysql().Query2(sql, &ret)
log.Print(ret)
if nil != e { if nil != e {
logs.Error(e.Error()) logs.Error(e.Error())
return nil return nil
} }
return ret return ret
} }
func CreateDoc(doc Doc) error { func CreateDoc(doc Doc) error {
sql := fmt.Sprintf(`INSERT INTO doc ( doc.title, doc.content, doc.author, doc.type ) SELECT sql := fmt.Sprintf(`INSERT INTO doc ( doc.title, doc.content, doc.author, doc.type ) SELECT
'%s', '%s',