diff --git a/.idea/background.iml b/.idea/background.iml
new file mode 100644
index 0000000..c956989
--- /dev/null
+++ b/.idea/background.iml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..28a804d
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..74b1dd3
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
new file mode 100644
index 0000000..e5f8d94
--- /dev/null
+++ b/.idea/workspace.xml
@@ -0,0 +1,322 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/config/config.go b/config/config.go
index e9cbf20..401c350 100644
--- a/config/config.go
+++ b/config/config.go
@@ -19,6 +19,8 @@ type ConfAPI struct {
Logs LogConfig `yaml:"logs"` // 日志
Redis1 EntityRedis `yaml:"redis1"`
Mysql MysqlConfig `yaml:"mysql"` // 认证配置
+ Mysql1 MysqlConfig `yaml:"mysql1"` // 认证配置
+
init bool
}
@@ -81,6 +83,13 @@ func GetMysqlConfig() *MysqlConfig {
return nil
}
}
+func GetMysqlBlogConfig() *MysqlConfig {
+ if gConf.init {
+ return &gConf.Mysql1
+ } else {
+ return nil
+ }
+}
func GetRedis1() *EntityRedis {
if gConf.init {
return &gConf.Redis1
diff --git a/controller/blog.go b/controller/blog.go
new file mode 100644
index 0000000..8adaac9
--- /dev/null
+++ b/controller/blog.go
@@ -0,0 +1,41 @@
+package controller
+
+import (
+ "background/logs"
+ "background/model"
+ "github.com/gin-gonic/gin"
+)
+
+
+func AddArticle(c *gin.Context) {
+ rsp := RespBase{Msg:"FAIL", Status:210,}
+ type ReqAddArticle struct {
+ Title string `json:"title"`
+ Content string `json:"content"`
+ author string `json:"author"`
+ Type int64 `json:"type"`
+ }
+ var req ReqAddArticle
+ defer func() {
+ c.JSON(200,rsp)
+ }()
+ er := c.BindJSON(&req)
+ if nil != er{
+ logs.Error(er.Error())
+ return
+ }
+ e := model.CreateDoc(
+ model.Doc{
+ Type:req.Type,
+ Title:req.Title,
+ Content:req.Content,
+ Author:req.author,
+ },
+ )
+ if nil != e{
+ logs.Error(e.Error())
+ return
+ }
+ rsp.Msg = "OK"
+ rsp.Status = 0
+}
\ No newline at end of file
diff --git a/db/sqlManager.go b/db/sqlManager.go
index f9b2cf0..6f03c03 100644
--- a/db/sqlManager.go
+++ b/db/sqlManager.go
@@ -8,9 +8,16 @@ import (
)
var gDb Database
+var blogDb Database
func Init() {
mysqlconf := config.GetMysqlConfig()
+ blogConf := config.GetMysqlBlogConfig()
+
+ gDb = Database{Type: string(""), DB: initMysql(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,
mysqlconf.Addr, mysqlconf.Db)
_db, err := sql.Open("mysql", cnn)
@@ -21,9 +28,11 @@ func Init() {
if nil != e {
fmt.Println(e.Error())
}
- gDb = Database{Type: string(""), DB: _db}
+ return _db
}
-
func GetMysqlClient() *Database {
return &gDb
}
+func GetBlogMysql() *Database{
+ return &blogDb
+}
\ No newline at end of file
diff --git a/main.go b/main.go
index 794e8b4..80a1f78 100644
--- a/main.go
+++ b/main.go
@@ -88,6 +88,9 @@ func main() {
api.POST("/verify", mailContoller.OnSendEmailCode)
/** 修改密码**/
api.POST("modify_pass",middle.AuthMiddle,userController.ModifyPasswd)
+
+ api.PUT("article",controller.AddArticle) // 添加文章
+
}
e := r.Run(":" + strconv.Itoa(config.GetPort()))
diff --git a/model/blog.go b/model/blog.go
new file mode 100644
index 0000000..78d0f31
--- /dev/null
+++ b/model/blog.go
@@ -0,0 +1,29 @@
+package model
+
+import (
+ "background/db"
+ "background/logs"
+ "errors"
+ "fmt"
+)
+
+type Doc struct {
+ ID int64 `sql:"id" json:"id"`
+ Title string `sql:"title" json:"title"`
+ Type int64 `sql:"type" json:"type"`
+ Content string `sql:"content" json:"content"`
+ Author string `sql:"author" json:"author"`
+}
+
+func CreateDoc(doc Doc) error {
+ if doc.ID == 0{
+ return errors.New("wrong primary key")
+ }
+ 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.GetMysqlClient().Query(sql)
+ if nil != e{
+ logs.Error(e.Error())
+ return e
+ }
+ return nil
+}
\ No newline at end of file
diff --git a/user.yaml b/user.yaml
index 7d254e6..0f63f5d 100644
--- a/user.yaml
+++ b/user.yaml
@@ -17,3 +17,11 @@ mysql:
db: background
max_open: 100
MaxIdle: 99
+mysql2:
+ addr: 118.24.238.198
+ user: server
+ password: server
+ db: background
+ max_open: 100
+ MaxIdle: 99
+