From 9b626958ea727bd307421ac684c737edf195dbce Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Thu, 25 Jun 2020 01:15:46 +0800 Subject: [PATCH 1/8] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=A4=87=E5=BF=98=E5=BD=95=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- camelcase/swagger.json | 13 +++++ camelcase/swagger.yaml | 10 ++++ controller/blog.go | 119 +++++++++++++++++++++++++++++++++++++++++ controller/user.go | 62 ++------------------- docs/docs.go | 57 ++++++++++++++++++++ main.go | 21 ++++++++ model/memo.go | 73 +++++++++++++++++++++++++ 7 files changed, 298 insertions(+), 57 deletions(-) create mode 100644 camelcase/swagger.json create mode 100644 camelcase/swagger.yaml create mode 100644 docs/docs.go create mode 100644 model/memo.go diff --git a/camelcase/swagger.json b/camelcase/swagger.json new file mode 100644 index 0000000..a728199 --- /dev/null +++ b/camelcase/swagger.json @@ -0,0 +1,13 @@ +{ + "swagger": "2.0", + "info": { + "description": "{{.Description}}", + "title": "大厅功能api", + "contact": {}, + "license": {}, + "version": "1.0" + }, + "host": "localhost:8080", + "basePath": "/api/v1", + "paths": {} +} \ No newline at end of file diff --git a/camelcase/swagger.yaml b/camelcase/swagger.yaml new file mode 100644 index 0000000..7ec9bdd --- /dev/null +++ b/camelcase/swagger.yaml @@ -0,0 +1,10 @@ +basePath: /api/v1 +host: localhost:8080 +info: + contact: {} + description: '{{.Description}}' + license: {} + title: 大厅功能api + version: "1.0" +paths: {} +swagger: "2.0" diff --git a/controller/blog.go b/controller/blog.go index 7a147b1..4161673 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -349,4 +349,123 @@ func ArticlesTypes(c *gin.Context) { resp.Data = docTypes resp.Msg = "OK" resp.Status = 0 +} + +func CreateMemo(c *gin.Context) { + resp := RespBase{"unkown error",-231,nil} + defer func() { + c.JSON(200,resp) + }() + var req model.Memo + e := c.BindJSON(&req) + if nil != e{ + resp.Msg = "wrong input" + return + } + e = model.CreateMemo(req) + if nil != e{ + logs.Error(e.Error()) + return + } + resp.Data = nil + resp.Msg = "OK" + resp.Status = 0 +} + + +func UpdateMemo(c *gin.Context) { + resp := RespBase{"unkown error",-231,nil} + defer func() { + c.JSON(200,resp) + }() + var req model.Memo + e := c.BindJSON(&req) + if nil != e{ + resp.Msg = "wrong input" + return + } + e = model.UpdateMemo(req) + if nil != e{ + logs.Error(e.Error()) + return + } + resp.Data = nil + resp.Msg = "OK" + resp.Status = 0 +} + +func GetMemos(c *gin.Context) { + rsp := RespBase{"ERR",-1,nil} + defer func() { + c.JSON(200,rsp) + }() + type ReqMemos struct { + title string `json:"title"` + } + req := ReqMemos{} + e := c.BindJSON(&req) + if nil != e{ + logs.Error(e.Error()) + return + } + limit,offset := GetPageParaFromQuery(c) + if limit != 0 && offset != 0{ + dat, e := model.GetMemos(req.title,10,0) + if nil != e{ + return + } + rsp.Data = dat + rsp.Status = 0 + rsp.Msg = "OK" + }else { + dat, e := model.GetMemos(req.title,10,0) + if nil != e{ + return + } + rsp.Data = dat + rsp.Status = 0 + rsp.Msg = "OK" + } + +} + +func GetMemo(c *gin.Context) { + rsp := RespBase{"ERR",-1,nil} + defer func() { + c.JSON(200,rsp) + }() + type ReqMemos struct { + title string `json:"title"` + } + id := c.Query("id") + dat, e := model.ReadMemo(int32(db.Atoi(id))) + if nil != e{ + return + } + rsp.Msg = "OK" + rsp.Status = 0 + rsp.Data = dat +} +func DeleteMemos(c *gin.Context) { + resp := RespBase{"unkown error",-231,nil} + defer func() { + c.JSON(200,resp) + }() + type DelReq struct { + Id int32 `json:"id"` + } + var req DelReq + e := c.BindJSON(&req) + if nil != e{ + resp.Msg = "wrong input" + return + } + e = model.DeleteMemo(req.Id) + if nil != e{ + logs.Error(e.Error()) + return + } + resp.Data = nil + resp.Msg = "OK" + resp.Status = 0 } \ No newline at end of file diff --git a/controller/user.go b/controller/user.go index e1c1af3..20b4345 100644 --- a/controller/user.go +++ b/controller/user.go @@ -75,25 +75,10 @@ func (this *UserController) Auth(c *gin.Context) { } } -// SetUser godoc -// @Summary SetUser -// @Description set userinfo -// @Accept json -// @Produce json -// @Param q query string false "name search by q" -// @Success 200 {array} util.RespBase -// @Router /setUser [get] func (this *UserController) SetUser(c *gin.Context) { } -// SetUser godoc -// @Summary SetUser -// @Description set userinfo -// @Accept json -// @Produce json -// @Param q query string false "name search by q" -// @Success 200 {array} util.RespBase -// @Router /setUser [get] + func (this *UserController) ModifyPasswd(c *gin.Context) { type ReqModifyPasswd struct{ id int `json:"id"` @@ -123,14 +108,7 @@ func (this *UserController) ModifyPasswd(c *gin.Context) { resp.Status = 0 } -// DelUser godoc -// @Summary Delete a user from database -// @Description you must have a token in your parameter -// @Accept json -// @Produce json -// @Param q query string false "name search by q" -// @Success 200 {array} util.RespBase -// @Router /setUser [get] + func (this *UserController) DelUser(c *gin.Context) { } @@ -159,17 +137,7 @@ func (this *UserController) GetUser(c *gin.Context) { resp.Data = users } -// GetUsers godoc -// @Summary GetUsers -// @Description Get all user with query -// @Accept json -// @Produce json -// @Param page query int 1 "分页的页数" -// @Param pageSize query int 10 "name search by q" -// @Param displayname query string false "name search by q" -// @Param department_id query string false "name search by q" -// @Param permission_type query string false "name search by q" -// @Router /api/users [get] + func (this *UserController) Users(c *gin.Context) { var statuscode int var resp RespBase @@ -201,17 +169,7 @@ func (this *UserController) Users(c *gin.Context) { resp.Data = dat } -// GetUsers godoc -// @Summary GetUsers -// @Description Get all user with query -// @Accept json -// @Produce json -// @Param page query int 1 "分页的页数" -// @Param pageSize query int 10 "name search by q" -// @Param displayname query string false "name search by q" -// @Param department_id query string false "name search by q" -// @Param permission_type query string false "name search by q" -// @Router /api/users [get] + func (this *UserController) SerarchUsers(c *gin.Context) { var statuscode int var resp RespBase @@ -276,17 +234,7 @@ func DefaultOption(c *gin.Context) { }() } -// Login godoc -// @Summary Login -// @Description login -// @Accept json -// @Produce json -// @Param logininfo query {object} LoginReq "登录请求参数" -// @Success 200 {array} util.RespBase -// @Failure 400 {object} util.RespBase -// @Failure 404 {object} util.RespBase -// @Failure 500 {object} util.RespBase -// @Router /api/login [post] + func (this *UserController) Login(c *gin.Context) { type LoginReq struct { RememberMe int32 `json:"remember_me"` diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 0000000..154785c --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,57 @@ +// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT +// This file was generated by swaggo/swag at +// 2020-06-24 21:33:14.5754792 +0800 CST m=+0.035031801 + +package docs + +import ( + "bytes" + + "github.com/alecthomas/template" + "github.com/swaggo/swag" +) + +var doc = `{ + "swagger": "2.0", + "info": { + "description": "{{.Description}}", + "title": "大厅功能api", + "contact": {}, + "license": {}, + "version": "1.0" + }, + "host": "localhost:8080", + "basePath": "/api/v1", + "paths": {} +}` + +type swaggerInfo struct { + Version string + Host string + BasePath string + Title string + Description string +} + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo swaggerInfo + +type s struct{} + +func (s *s) ReadDoc() string { + t, err := template.New("swagger_info").Parse(doc) + if err != nil { + return doc + } + + var tpl bytes.Buffer + if err := t.Execute(&tpl, SwaggerInfo); err != nil { + return doc + } + + return tpl.String() +} + +func init() { + swag.Register(swag.Name, &s{}) +} diff --git a/main.go b/main.go index 9b99898..519c7dd 100644 --- a/main.go +++ b/main.go @@ -5,14 +5,22 @@ import ( "background/controller" "background/controller/middle" "background/db" + _ "background/docs" "background/logs" "background/model" "github.com/gin-gonic/gin" + "github.com/swaggo/files" // swagger embed files + "github.com/swaggo/gin-swagger" // gin-swagger middleware "github.com/tommy351/gin-sessions" "log" "strconv" ) +// @title 大厅功能api +// @version 1.0 +// @host localhost:8080 +// @BasePath /api/v1 + var ( userController = controller.UserController{} mailContoller = controller.MailController{} @@ -95,6 +103,14 @@ func main() { store := sessions.NewCookieStore([]byte("secret123")) r.Use(sessions.Middleware("sess_store", store)) r.Use(CORSMiddleware) + + go func() { + // programatically set swagger info + r := gin.New() + // use ginSwagger middleware to serve the API docs + r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + r.Run() + }() api := r.Group("/api") { /** 添加或修改用户 **/ @@ -140,6 +156,11 @@ func main() { api.GET("/filelist", fileController.FileList) // 文件列表 api.GET("/fileType", fileController.FileType) // 文件类型 + api.PUT("/memo", controller.CreateMemo) // 备忘录新建 + api.POST("/memo", controller.UpdateMemo) // 备忘录更新 + api.POST("/memos", controller.GetMemos) // 备忘录批量 + api.DELETE("/memo", controller.DeleteMemos) //删除备忘录 + api.GET("/memo", controller.GetMemo) // 单独读取 } hookapi := r.Group("hookapi") { diff --git a/model/memo.go b/model/memo.go new file mode 100644 index 0000000..773a606 --- /dev/null +++ b/model/memo.go @@ -0,0 +1,73 @@ +package model + +import ( + "background/db" + "background/logs" + "fmt" + "strings" +) + +type Memo struct { + ID int64 `sql:"id" json:"id"` + Title string `sql:"title" json:"title"` + CreateTime int64 `sql:"create_time" json:"create_time"` + Content string `sql:"content" json:"content"` +} + +func CreateMemo(memo Memo) error{ + sql := fmt.Sprintf(`insert into memo(title,create_time,content) values (%s,%s,%s) `, + memo.Title,memo.CreateTime,strings.Replace(memo.Content, "'", "\\'", -1)) + _, e := db.GetMysqlClient().Query(sql) + if nil != e { + logs.Error(e.Error()) + return e + } + return nil +} + +func UpdateMemo(memo Memo) error{ + sql := fmt.Sprintf(`update memo set title = %s, + create_time = %s, + content = %s where memo.id = '%d'`, + memo.Title,memo.CreateTime,strings.Replace(memo.Content, "'", "\\'", -1),memo.ID) + _, e := db.GetMysqlClient().Query(sql) + if nil != e { + logs.Error(e.Error()) + return e + } + return nil +} + +func DeleteMemo(id int32) error{ + sql := fmt.Sprintf(`delete from memo where memo.id = '%d'`,id) + _, e := db.GetMysqlClient().Query(sql) + if nil != e { + logs.Error(e.Error()) + return e + } + return nil +} + +func ReadMemo(id int32) ([]Memo,error){ + memo := []Memo{} + sql := fmt.Sprintf(`select * from memo where memo.id = '%d'`,id) + e := db.GetMysqlClient().Query2(sql,&memo) + if nil != e { + logs.Error(e.Error()) + return nil,e + } + return memo,nil +} + +func GetMemos(title string,limit int,offset int) ([]Memo,error) { + memo := []Memo{} + sql := fmt.Sprintf(`select * from memo where memo.title like '%d' offset %d limit %d`, + title,offset,limit) + e := db.GetMysqlClient().Query2(sql,&memo) + if nil != e { + logs.Error(e.Error()) + return nil,e + } + return memo,nil + +} \ No newline at end of file From 9509653e553975aaf5ea81d4eebb8800c1612514 Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Fri, 26 Jun 2020 18:20:36 +0800 Subject: [PATCH 2/8] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/blog.go | 7 +++-- main.go | 2 +- model/memo.go | 20 ++++++++----- model/template.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 model/template.go diff --git a/controller/blog.go b/controller/blog.go index 4161673..48260f4 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -359,6 +359,7 @@ func CreateMemo(c *gin.Context) { var req model.Memo e := c.BindJSON(&req) if nil != e{ + logs.Error(e.Error()) resp.Msg = "wrong input" return } @@ -400,7 +401,7 @@ func GetMemos(c *gin.Context) { c.JSON(200,rsp) }() type ReqMemos struct { - title string `json:"title"` + Title string `json:"title"` } req := ReqMemos{} e := c.BindJSON(&req) @@ -410,7 +411,7 @@ func GetMemos(c *gin.Context) { } limit,offset := GetPageParaFromQuery(c) if limit != 0 && offset != 0{ - dat, e := model.GetMemos(req.title,10,0) + dat, e := model.GetMemos(req.Title,10,0) if nil != e{ return } @@ -418,7 +419,7 @@ func GetMemos(c *gin.Context) { rsp.Status = 0 rsp.Msg = "OK" }else { - dat, e := model.GetMemos(req.title,10,0) + dat, e := model.GetMemos(req.Title,10,0) if nil != e{ return } diff --git a/main.go b/main.go index 519c7dd..d1624f6 100644 --- a/main.go +++ b/main.go @@ -109,7 +109,7 @@ func main() { r := gin.New() // use ginSwagger middleware to serve the API docs r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) - r.Run() + r.Run(":9992") }() api := r.Group("/api") { diff --git a/model/memo.go b/model/memo.go index 773a606..fa745d9 100644 --- a/model/memo.go +++ b/model/memo.go @@ -4,19 +4,22 @@ import ( "background/db" "background/logs" "fmt" + "qiniupkg.com/x/log.v7" "strings" ) type Memo struct { ID int64 `sql:"id" json:"id"` Title string `sql:"title" json:"title"` - CreateTime int64 `sql:"create_time" json:"create_time"` + CreateTime string `sql:"create_time" json:"create_time"` Content string `sql:"content" json:"content"` } func CreateMemo(memo Memo) error{ - sql := fmt.Sprintf(`insert into memo(title,create_time,content) values (%s,%s,%s) `, - memo.Title,memo.CreateTime,strings.Replace(memo.Content, "'", "\\'", -1)) + sql := fmt.Sprintf(`insert into memo(title,create_time,content) select '%s','%s','%s' + from dual where not exists (select * from memo where memo.title='%s') `, + memo.Title,memo.CreateTime,strings.Replace(memo.Content, "'", "\\'", -1),memo.Title) + log.Print(sql) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) @@ -26,9 +29,9 @@ func CreateMemo(memo Memo) error{ } func UpdateMemo(memo Memo) error{ - sql := fmt.Sprintf(`update memo set title = %s, - create_time = %s, - content = %s where memo.id = '%d'`, + sql := fmt.Sprintf(`update memo set title = '%s', + create_time = '%s', + content = '%s' where memo.id = %d`, memo.Title,memo.CreateTime,strings.Replace(memo.Content, "'", "\\'", -1),memo.ID) _, e := db.GetMysqlClient().Query(sql) if nil != e { @@ -61,8 +64,9 @@ func ReadMemo(id int32) ([]Memo,error){ func GetMemos(title string,limit int,offset int) ([]Memo,error) { memo := []Memo{} - sql := fmt.Sprintf(`select * from memo where memo.title like '%d' offset %d limit %d`, - title,offset,limit) + sql := fmt.Sprintf(`select * from memo where memo.title like '%s%%' limit %d offset %d`, + title,limit,offset) + log.Print(sql) e := db.GetMysqlClient().Query2(sql,&memo) if nil != e { logs.Error(e.Error()) diff --git a/model/template.go b/model/template.go new file mode 100644 index 0000000..f618c48 --- /dev/null +++ b/model/template.go @@ -0,0 +1,73 @@ +package model + +import ( + "background/db" + "background/logs" + "fmt" + "log" +) + +type DocTemplate struct { + Id int32 `sql:"id" json:"id"` + Content string `sql:"content" json:"content"` + Name string `sql:"content" json:"name"` +} + + +func CreateDocTemplate(templ DocTemplate) error{ + sql := fmt.Sprintf(`insert into doc_template(content,name) select '%s','%s + from dual where not exists (select * from doc_template where doc_template.name='%s') `, + templ.Content,templ.Name,templ.Name) + log.Print(sql) + _, e := db.GetMysqlClient().Query(sql) + if nil != e { + log.Print(sql) + return e + } + return nil +} + +func UpdateDocTemplate(templ DocTemplate) error{ + sql := fmt.Sprintf(`update doc_template set content = %s, + name = %s, where memo.id = '%d'`,templ.Content,templ.Name,templ.Id) + _, e := db.GetMysqlClient().Query(sql) + if nil != e { + log.Print(e.Error()) + return e + } + return nil +} + +func DeleteDocTemplate(id int32) error{ + sql := fmt.Sprintf(`delete from doc_template doc_template memo.id = '%d'`,id) + _, e := db.GetMysqlClient().Query(sql) + if nil != e { + logs.Error(e.Error()) + return e + } + return nil +} + +func ReadDocTemplate(id int32) ([]DocTemplate,error){ + memo := []DocTemplate{} + sql := fmt.Sprintf(`select * from doc_template where doc_template.id = '%d'`,id) + e := db.GetMysqlClient().Query2(sql,&memo) + if nil != e { + logs.Error(e.Error()) + return nil,e + } + return memo,nil +} + +func GetDocTemplate(title string,limit int,offset int) ([]DocTemplate,error) { + memo := []DocTemplate{} + sql := fmt.Sprintf(`select * from doc_template where doc_template.name like '%s%%' limit %d offset %d`, + title,limit,offset) + log.Print(sql) + e := db.GetMysqlClient().Query2(sql,&memo) + if nil != e { + logs.Error(e.Error()) + return nil,e + } + return memo,nil +} \ No newline at end of file From 5482310fb26a8c37f6f240220a2befbd430d01e7 Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Sat, 27 Jun 2020 00:52:07 +0800 Subject: [PATCH 3/8] no message --- model/memo.go | 1 + 1 file changed, 1 insertion(+) diff --git a/model/memo.go b/model/memo.go index fa745d9..36e3737 100644 --- a/model/memo.go +++ b/model/memo.go @@ -43,6 +43,7 @@ func UpdateMemo(memo Memo) error{ func DeleteMemo(id int32) error{ sql := fmt.Sprintf(`delete from memo where memo.id = '%d'`,id) + log.Print(sql) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) From a2275ab37e3e7b27ab02b9f318cfd3bba1d6643b Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Sat, 27 Jun 2020 00:52:15 +0800 Subject: [PATCH 4/8] no message --- controller/blog.go | 3 --- main.go | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/controller/blog.go b/controller/blog.go index 48260f4..63653e7 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -435,9 +435,6 @@ func GetMemo(c *gin.Context) { defer func() { c.JSON(200,rsp) }() - type ReqMemos struct { - title string `json:"title"` - } id := c.Query("id") dat, e := model.ReadMemo(int32(db.Atoi(id))) if nil != e{ diff --git a/main.go b/main.go index d1624f6..df37e03 100644 --- a/main.go +++ b/main.go @@ -159,7 +159,7 @@ func main() { api.PUT("/memo", controller.CreateMemo) // 备忘录新建 api.POST("/memo", controller.UpdateMemo) // 备忘录更新 api.POST("/memos", controller.GetMemos) // 备忘录批量 - api.DELETE("/memo", controller.DeleteMemos) //删除备忘录 + api.POST("/delmemo", controller.DeleteMemos) //删除备忘录 api.GET("/memo", controller.GetMemo) // 单独读取 } hookapi := r.Group("hookapi") From f7ad0d02f4c43b9d7f8ff174fe0bb43933eb3cb0 Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Tue, 30 Jun 2020 23:28:51 +0800 Subject: [PATCH 5/8] no message --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index df37e03..4894b0a 100644 --- a/main.go +++ b/main.go @@ -136,9 +136,9 @@ func main() { api.POST("/articles", controller.GetArticles) // 获取所有文章 api.PUT("/article", controller.AddArticle) // 添加文章 api.PUT("/article_search", controller.SearchArticle) // 添加文章 - api.GET("article_type", controller.ArticlesType) //获取所有文章分类 - api.PUT("article_type", controller.AddArticleType) - api.DELETE("article_type", controller.DeleteArticleType) + api.GET("/article_type", controller.ArticlesType) //获取所有文章分类 + api.PUT("/article_type", controller.AddArticleType) + api.DELETE("/article_type", controller.DeleteArticleType) api.POST("/article_update", controller.UpdateArtilce) //更新文章 api.GET("/articleCount", controller.GetArticleCount) //获取所有文章个数 From 17a4a998bf34b852ee428c0d704a6e2dc7cb35b9 Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Thu, 23 Jul 2020 00:06:07 +0800 Subject: [PATCH 6/8] =?UTF-8?q?=E6=96=87=E7=AB=A0=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E5=88=86=E7=BB=84=E5=90=8E=E7=AB=AF=E7=AE=A1=E7=90=86=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 93 +++++++++++++++++++++++++++++++++------------ model/blog.go | 14 +++++++ 2 files changed, 82 insertions(+), 25 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0de3cc4..725236c 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,13 +3,8 @@ - - - - - + - - + + + + + @@ -50,20 +51,9 @@ - - - - - + + + @@ -80,19 +70,72 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/model/blog.go b/model/blog.go index deaeca7..0ef601c 100644 --- a/model/blog.go +++ b/model/blog.go @@ -17,16 +17,30 @@ type Doc struct { IsPublic int `sql:"is_public" json:"is_public"` } +type DocGroup struct { + Int int32 `sql:"int"` + Name string `sql:"name"` +} type ArticleType struct { Id int64 `sql:"id" json:"id"` Name string `sql:"type_name" json:"type_name"` Author string `sql:"author" json:"author"` + Group int32 `sql:"group" json:"group"` + GroupName string `json:"group_name"` } func GetArticlesType() []ArticleType { ret := []ArticleType{} sql := fmt.Sprintf("select * from doc_type") e := db.GetBlogMysql().Query2(sql, &ret) + for k,_ := range ret{ + group := []DocGroup{} + sql = fmt.Sprintf("select * from doc_group where doc_group.int = %d",ret[k].Group) + db.GetBlogMysql().Query2(sql, &group) + if len(group) > 0{ + ret[k].GroupName = group[0].Name + } + } log.Print(ret) if nil != e { logs.Error(e.Error()) From 1c2ff376309803ab18727032d9bd30e344c81c70 Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Sun, 26 Jul 2020 15:03:42 +0800 Subject: [PATCH 7/8] =?UTF-8?q?=E4=BA=8C=E7=BA=A7=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 18 ++++++++++-------- controller/blog.go | 23 +++++++++++++++++++++-- main.go | 1 + model/blog.go | 15 +++++++++++++-- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 725236c..fa4872f 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,6 +3,8 @@ + + \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..9902fe2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${fileDirname}", + "env": {}, + "args": [] + } + ] +} diff --git a/11.yaml b/11.yaml new file mode 100644 index 0000000..b573b34 --- /dev/null +++ b/11.yaml @@ -0,0 +1,42 @@ +listen_api: 4596 +runmode: debug +max_conn: 121 +logs: + dir: /var/log/user + file: user.log + level: 1 + savefile: false +redis1: + addr: 49.235.25.67:16379 + password: ~ + db: 1 +mysql: + addr: 49.235.25.67 + user: caiyu + password: 12345678 + db: background + max_open: 100 + MaxIdle: 99 +elasticsearch: + address: http://hyrtc.net:9200 +mysql1: + addr: 49.235.25.67 + user: caiyu + password: 12345678 + db: background + max_open: 100 + MaxIdle: 99 +mongo: + addr: 49.235.25.67 + port: 27017 + user: root + password: root + db: test + max_open: 100 + MaxIdle: 99 +ca_cert: "" +client_cert: "" +client_key: "" +test_arr: + - 1 + - 23 \ No newline at end of file diff --git a/controller/blog.go b/controller/blog.go index 404acfe..d1cc003 100644 --- a/controller/blog.go +++ b/controller/blog.go @@ -5,94 +5,95 @@ import ( "background/logs" "background/model" "fmt" + "strconv" + "github.com/gin-gonic/gin" "qiniupkg.com/x/log.v7" - "strconv" ) -func GetPageParaFromQuery(c *gin.Context) (int,int){ +func GetPageParaFromQuery(c *gin.Context) (int, int) { limit := c.Query("limit") offset := c.Query("offset") - iLmit,e := strconv.Atoi(limit) - if nil != e{ - return 0,0 + iLmit, e := strconv.Atoi(limit) + if nil != e { + return 0, 0 } - iOffset,e := strconv.Atoi(offset) - if nil != e{ - return 0,0 + iOffset, e := strconv.Atoi(offset) + if nil != e { + return 0, 0 } - return iLmit,iOffset + return iLmit, iOffset } -func GetArticles(c *gin.Context) { +func GetArticles(c *gin.Context) { type ReqArticles struct { Name string `json:"name"` Type int32 `json:"types"` } var req ReqArticles - rsp := RespBase{"ERR",-1,nil} + rsp := RespBase{"ERR", -1, nil} defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() e := c.BindJSON(&req) - if nil != e{ + if nil != e { logs.Error(e.Error()) return } article := []model.Doc{} - limit,offset := GetPageParaFromQuery(c) + limit, offset := GetPageParaFromQuery(c) public := c.Query("is_public") var sql string - if req.Name != ""{ - if req.Type == 110{ + if req.Name != "" { + if req.Type == 110 { is_public := 0 - if public == "true"{ + if public == "true" { is_public = 0 - }else{ + } else { is_public = 1 } sql = "select * from doc where doc.title like '%%%s%%' and is_public = %d limit %d offset %d " - sql = fmt.Sprintf(sql,req.Name,is_public,limit,offset*limit) - }else{ + sql = fmt.Sprintf(sql, req.Name, is_public, limit, offset*limit) + } else { is_public := 0 if public == "true" { is_public = 0 - }else{ + } else { is_public = 1 } sql = fmt.Sprintf("select * from doc where doc.title like '%%%s%%' and doc.type = %d and is_public = %d limit %d offset %d ", - req.Name,req.Type,is_public,limit,offset*limit) + req.Name, req.Type, is_public, limit, offset*limit) } - }else{ - if req.Type == 110{ + } else { + if req.Type == 110 { is_public := 0 if public == "true" { is_public = 1 - }else{ + } else { is_public = 0 } - sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title from doc where is_public = %d " + - "order by id desc limit %d offset %d",is_public,limit,offset) - }else{ + sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title from doc where is_public = %d "+ + "order by id desc limit %d offset %d", is_public, limit, offset) + } else { is_public := 0 if public == "true" { is_public = 1 - }else{ + } else { is_public = 0 } - sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title,doc.type from doc where doc.type = %d and is_public = %d " + - " order by id desc limit %d offset %d",req.Type,is_public,limit,offset) + sql = fmt.Sprintf("SELECT doc.id,doc.author,LEFT(doc.content,50) as content,doc.title,doc.type from doc where doc.type = %d and is_public = %d "+ + " order by id desc limit %d offset %d", req.Type, is_public, limit, offset) } } log.Print(sql) - e = db.GetMysqlClient().Query2(sql,&article) - if nil != e{ - logs.Error(e.Error(),sql) + e = db.GetMysqlClient().Query2(sql, &article) + if nil != e { + logs.Error(e.Error(), sql) return } - for k,_ := range article{ + for k, _ := range article { article[k].Content += "..." } rsp.Data = article @@ -101,21 +102,21 @@ func GetArticles(c *gin.Context) { rsp.Msg = "OK" } -func GetArticleCount(c *gin.Context) { - resp := RespBase{Msg:"FAIL",Status: 211} +func GetArticleCount(c *gin.Context) { + resp := RespBase{Msg: "FAIL", Status: 211} defer func() { - c.JSON(200,resp) + c.JSON(200, resp) }() query := fmt.Sprintf("select count(*) as cnt from doc") type Cnt struct { Cnt int64 `sql:"cnt" json:"cnt"` } - cnt := []Cnt{}; - - e := db.GetMysqlClient().Query2(query,&cnt) - if nil != e{ + cnt := []Cnt{} + + e := db.GetMysqlClient().Query2(query, &cnt) + if nil != e { log.Print(e.Error()) return } @@ -124,32 +125,32 @@ func GetArticleCount(c *gin.Context) { resp.Msg = "OK" } -func GetArticle(c *gin.Context) { - resp := RespBase{Msg:"FAIL",Status: 211} +func GetArticle(c *gin.Context) { + resp := RespBase{Msg: "FAIL", Status: 211} sid := c.Param("id") var id int var err error defer func() { - c.JSON(200,resp) + c.JSON(200, resp) }() - if sid == ""{ + if sid == "" { return - }else{ - id,err = strconv.Atoi(sid) - if nil != err{ + } else { + id, err = strconv.Atoi(sid) + if nil != err { return } } - query := fmt.Sprintf("select * from doc where doc.id = '%d'",id) + query := fmt.Sprintf("select * from doc where doc.id = '%d'", id) docs := []model.Doc{} - e := db.GetMysqlClient().Query2(query,&docs) - if nil != e{ + e := db.GetMysqlClient().Query2(query, &docs) + if nil != e { log.Print(e.Error()) return } - if len(docs) > 0{ + if len(docs) > 0 { resp.Data = docs[0] resp.Status = 0 resp.Msg = "OK" @@ -157,86 +158,86 @@ func GetArticle(c *gin.Context) { } -func UpdateArtilce(c *gin.Context) { - rsp := RespBase{Msg:"FAIL", Status:210,} +func UpdateArtilce(c *gin.Context) { + rsp := RespBase{Msg: "FAIL", Status: 210} type ReqUpdateArticle struct { - Id int64 `json:"id"` - Title string `json:"title"` + Id int64 `json:"id"` + Title string `json:"title"` Content string `json:"content"` - author string `json:"author"` - Type int64 `json:"type"` - IsPublic int `json:"is_public"` + author string `json:"author"` + Type int64 `json:"type"` + IsPublic int `json:"is_public"` } var req ReqUpdateArticle defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() er := c.BindJSON(&req) - if nil != er{ + if nil != er { logs.Error(er.Error()) return } - if req.Title == ""{ + if req.Title == "" { rsp.Msg = "title required" return } e := model.UpdateDoc( model.Doc{ - Type:req.Type, - Title:req.Title, - Content:req.Content, - Author:req.author, - ID:req.Id, - IsPublic:req.IsPublic, + Type: req.Type, + Title: req.Title, + Content: req.Content, + Author: req.author, + ID: req.Id, + IsPublic: req.IsPublic, }, ) - if nil != e{ + if nil != e { logs.Error(e.Error()) return } rsp.Msg = "OK" rsp.Status = 0 - + } func SearchArticle(c *gin.Context) { - rsp := RespBase{Msg:"FAIL", Status:210,} + rsp := RespBase{Msg: "FAIL", Status: 210} defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() } -func AddArticle(c *gin.Context) { - rsp := RespBase{Msg:"FAIL", Status:210,} +func AddArticle(c *gin.Context) { + rsp := RespBase{Msg: "FAIL", Status: 210} type ReqAddArticle struct { - Id int64 `json:"id"` - Title string `json:"title"` + Id int64 `json:"id"` + Title string `json:"title"` Content string `json:"content"` - author string `json:"author"` - Type int64 `json:"type"` - Ispublic int `json:"is_public"` + author string `json:"author"` + Type int64 `json:"type"` + Ispublic int `json:"is_public"` } var req ReqAddArticle defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() er := c.BindJSON(&req) - if nil != er{ + if nil != er { logs.Error(er.Error()) return } - if req.Title == ""{ + if req.Title == "" { rsp.Msg = "title required" return } e := model.CreateDoc( model.Doc{ - Type:req.Type, - Title:req.Title, - Content:req.Content, - Author:req.author, - IsPublic:req.Ispublic, + Type: req.Type, + Title: req.Title, + Content: req.Content, + Author: req.author, + IsPublic: req.Ispublic, }, - ) - if nil != e{ + ) + if nil != e { logs.Error(e.Error()) return } @@ -244,28 +245,27 @@ func AddArticle(c *gin.Context) { rsp.Status = 0 } - -func ArticlesType(c *gin.Context) { - rsp := RespBase{Msg:"FAIL", Status:210,} +func ArticlesType(c *gin.Context) { + rsp := RespBase{Msg: "FAIL", Status: 210} defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() rsp.Data = model.GetArticlesType() rsp.Msg = "OK" rsp.Status = 0 } -func DeleteArticleType(c *gin.Context) { - rsp := RespBase{Msg:"Fail",Status:210} +func DeleteArticleType(c *gin.Context) { + rsp := RespBase{Msg: "Fail", Status: 210} defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() - sid :=c.Query("id") - if sid == ""{ + sid := c.Query("id") + if sid == "" { return } - id ,e := strconv.Atoi(sid) - if nil != e{ + id, e := strconv.Atoi(sid) + if nil != e { log.Print(e.Error()) return } @@ -273,39 +273,39 @@ func DeleteArticleType(c *gin.Context) { rsp.Msg = "OK" rsp.Status = 0 } -func AddArticleType(c *gin.Context) { - rsp := RespBase{Msg:"Fail",Status:210} +func AddArticleType(c *gin.Context) { + rsp := RespBase{Msg: "Fail", Status: 210} defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() - typeName :=c.Query("name") + typeName := c.Query("name") group := c.Query("group") - groupid,e := strconv.Atoi(group) - if nil != e{ + groupid, e := strconv.Atoi(group) + if nil != e { return } id := c.Query("id") - if id != ""{ - if typeName == ""{ + if id != "" { + if typeName == "" { return } articleType := model.ArticleType{ - Id: 0, - Name: typeName, - Group: int32(groupid), + Id: 0, + Name: typeName, + Group: int32(groupid), } rsp.Data = model.UpdateArticleType(articleType) rsp.Msg = "OK" rsp.Status = 0 - }else{ - if typeName == ""{ + } else { + if typeName == "" { return } articleType := model.ArticleType{ - Id: 0, - Name: typeName, - Group: int32(groupid), + Id: 0, + Name: typeName, + Group: int32(groupid), } rsp.Data = model.AddArticleType(articleType) rsp.Msg = "OK" @@ -313,41 +313,40 @@ func AddArticleType(c *gin.Context) { } } -func DeleteArticle(c *gin.Context) { - rsp := RespBase{Msg:"FAIL", Status:210,} +func DeleteArticle(c *gin.Context) { + rsp := RespBase{Msg: "FAIL", Status: 210} defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() sid := c.Param("id") - id,err := strconv.Atoi(sid) - if nil != err{ + id, err := strconv.Atoi(sid) + if nil != err { rsp.Status = -234 - c.JSON(200,rsp) + c.JSON(200, rsp) } err = model.DeleteDoc(int64(id)) - if nil != err{ + if nil != err { rsp.Status = -234 - c.JSON(200,rsp) + c.JSON(200, rsp) } rsp.Data = id rsp.Status = 0 rsp.Msg = "OK" } - -func ArticlesTypes(c *gin.Context) { - resp := RespBase{"unkown error",-231,nil} +func ArticlesTypes(c *gin.Context) { + resp := RespBase{"unkown error", -231, nil} defer func() { - c.JSON(200,resp) + c.JSON(200, resp) }() type DocType struct { - Id int64 `sql:"id"` + Id int64 `sql:"id"` TypeName string `sql:"type_name"` } docTypes := []DocType{} e := db.GetMysqlClient().Query2("select * from doc_type", &docTypes) - if nil != e{ + if nil != e { log.Print(e.Error()) return } @@ -356,20 +355,20 @@ func ArticlesTypes(c *gin.Context) { resp.Status = 0 } -func CreateMemo(c *gin.Context) { - resp := RespBase{"unkown error",-231,nil} +func CreateMemo(c *gin.Context) { + resp := RespBase{"unkown error", -231, nil} defer func() { - c.JSON(200,resp) + c.JSON(200, resp) }() var req model.Memo e := c.BindJSON(&req) - if nil != e{ + if nil != e { logs.Error(e.Error()) resp.Msg = "wrong input" return } e = model.CreateMemo(req) - if nil != e{ + if nil != e { logs.Error(e.Error()) return } @@ -378,20 +377,19 @@ func CreateMemo(c *gin.Context) { resp.Status = 0 } - -func UpdateMemo(c *gin.Context) { - resp := RespBase{"unkown error",-231,nil} +func UpdateMemo(c *gin.Context) { + resp := RespBase{"unkown error", -231, nil} defer func() { - c.JSON(200,resp) + c.JSON(200, resp) }() var req model.Memo e := c.BindJSON(&req) - if nil != e{ + if nil != e { resp.Msg = "wrong input" return } e = model.UpdateMemo(req) - if nil != e{ + if nil != e { logs.Error(e.Error()) return } @@ -401,12 +399,12 @@ func UpdateMemo(c *gin.Context) { } func GetDocGroup(c *gin.Context) { - rsp := RespBase{"ERR",-1,nil} + rsp := RespBase{"ERR", -1, nil} defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() - group,e := model.GetAllGroup() - if nil != e{ + group, e := model.GetAllGroup() + if nil != e { log.Print(e.Error()) return } @@ -414,32 +412,41 @@ func GetDocGroup(c *gin.Context) { rsp.Status = 0 rsp.Msg = "OK" } -func GetMemos(c *gin.Context) { - rsp := RespBase{"ERR",-1,nil} + +func GetTemplates(c *gin.Context) { + rsp := RespBase{"ERR", -1, nil} defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) + }() + +} + +func GetMemos(c *gin.Context) { + rsp := RespBase{"ERR", -1, nil} + defer func() { + c.JSON(200, rsp) }() type ReqMemos struct { Title string `json:"title"` } req := ReqMemos{} e := c.BindJSON(&req) - if nil != e{ + if nil != e { logs.Error(e.Error()) return } - limit,offset := GetPageParaFromQuery(c) - if limit != 0 && offset != 0{ - dat, e := model.GetMemos(req.Title,10,0) - if nil != e{ + limit, offset := GetPageParaFromQuery(c) + if limit != 0 && offset != 0 { + dat, e := model.GetMemos(req.Title, 10, 0) + if nil != e { return } rsp.Data = dat rsp.Status = 0 rsp.Msg = "OK" - }else { - dat, e := model.GetMemos(req.Title,10,0) - if nil != e{ + } else { + dat, e := model.GetMemos(req.Title, 10, 0) + if nil != e { return } rsp.Data = dat @@ -448,15 +455,17 @@ func GetMemos(c *gin.Context) { } } +func GetDocTemplate(c *gin.Context) { +} func GetMemo(c *gin.Context) { - rsp := RespBase{"ERR",-1,nil} + rsp := RespBase{"ERR", -1, nil} defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() id := c.Query("id") dat, e := model.ReadMemo(int32(db.Atoi(id))) - if nil != e{ + if nil != e { return } rsp.Msg = "OK" @@ -464,25 +473,25 @@ func GetMemo(c *gin.Context) { rsp.Data = dat } func DeleteMemos(c *gin.Context) { - resp := RespBase{"unkown error",-231,nil} + resp := RespBase{"unkown error", -231, nil} defer func() { - c.JSON(200,resp) + c.JSON(200, resp) }() type DelReq struct { Id int32 `json:"id"` } var req DelReq e := c.BindJSON(&req) - if nil != e{ + if nil != e { resp.Msg = "wrong input" return } e = model.DeleteMemo(req.Id) - if nil != e{ + if nil != e { logs.Error(e.Error()) return } resp.Data = nil resp.Msg = "OK" resp.Status = 0 -} \ No newline at end of file +} diff --git a/controller/hardware.go b/controller/hardware.go index cc73993..a082042 100644 --- a/controller/hardware.go +++ b/controller/hardware.go @@ -3,25 +3,26 @@ package controller import ( "background/logs" "background/model" + "github.com/gin-gonic/gin" "qiniupkg.com/x/log.v7" ) -func AddHardware(c *gin.Context) { - resp := RespBase{"unkown error",-231,nil} +func AddHardware(c *gin.Context) { + resp := RespBase{"unkown error", -231, nil} defer func() { - c.JSON(200,resp) + c.JSON(200, resp) }() var hardware model.Hardware e := c.BindJSON(&hardware) - if nil != e{ + if nil != e { log.Print(e) print(e) return } - e = hardware.CreateHardware() - if nil != e{ + e = hardware.CreateHardware() + if nil != e { resp.Status = -100 resp.Msg = e.Error() log.Print(e) @@ -32,17 +33,17 @@ func AddHardware(c *gin.Context) { resp.Status = 0 } -func DeleteHardWare(c *gin.Context) { - resp := RespBase{"unkown error",-231,nil} +func DeleteHardWare(c *gin.Context) { + resp := RespBase{"unkown error", -231, nil} defer func() { - c.JSON(200,resp) + c.JSON(200, resp) }() name := c.Query("name") - if name == ""{ + if name == "" { return } e := model.DeleteHardware(name) - if nil != e{ + if nil != e { logs.Error(e.Error()) return } @@ -51,20 +52,20 @@ func DeleteHardWare(c *gin.Context) { resp.Data = nil } -func UpdateHardWare(c *gin.Context) { +func UpdateHardWare(c *gin.Context) { } -func ReadHardWare(c *gin.Context) { - rsp := RespBase{"ERR",-1,nil} +func ReadHardWare(c *gin.Context) { + rsp := RespBase{"ERR", -1, nil} defer func() { - c.JSON(200,rsp) + c.JSON(200, rsp) }() - limit,offset := GetPageParaFromQuery(c) - log.Print(limit,offset) - hardware,e := model.GetHardwares(limit,offset) - if nil != e{ + limit, offset := GetPageParaFromQuery(c) + log.Print(limit, offset) + hardware, e := model.GetHardwares(limit, offset) + if nil != e { return } rsp.Data = hardware diff --git a/controller/helper/helper.go b/controller/helper/helper.go new file mode 100644 index 0000000..4393682 --- /dev/null +++ b/controller/helper/helper.go @@ -0,0 +1,19 @@ +package helper + +import ( + "background/controller/params" + + "github.com/gin-gonic/gin" +) + +func GetPageParam(c *gin.Context) (int, int) { + var limit, offset int + var ok bool + limit, ok = c.Keys[params.K_OFFSET].(int) + if !ok { + } + offset, ok = c.Keys[params.K_OFFSET].(int) + if !ok { + } + return limit, offset +} diff --git a/controller/params/params b/controller/params/params new file mode 100644 index 0000000..212d79a --- /dev/null +++ b/controller/params/params @@ -0,0 +1,5 @@ +package params + +const K_OFFSET = "offset" +const K_LIMIT = "limit" + diff --git a/controller/user.go b/controller/user.go index 20b4345..2693a3a 100644 --- a/controller/user.go +++ b/controller/user.go @@ -1,16 +1,17 @@ package controller import ( + "background/config" + "background/db" + "background/logs" + "background/model" + "background/redis" "background/utils" "bytes" "crypto/md5" "encoding/json" "errors" "fmt" - "github.com/fatih/structs" - "github.com/gin-gonic/gin" - _ "github.com/go-sql-driver/mysql" - "github.com/tommy351/gin-sessions" "io" "log" "math/rand" @@ -20,15 +21,14 @@ import ( "strconv" "strings" "time" - "background/config" - "background/db" - "background/logs" - "background/model" - "background/redis" + + "github.com/fatih/structs" + "github.com/gin-gonic/gin" + _ "github.com/go-sql-driver/mysql" + sessions "github.com/tommy351/gin-sessions" ) type UserController struct { - } type ReqSendEmailCode struct { @@ -80,8 +80,8 @@ func (this *UserController) SetUser(c *gin.Context) { } func (this *UserController) ModifyPasswd(c *gin.Context) { - type ReqModifyPasswd struct{ - id int `json:"id"` + type ReqModifyPasswd struct { + id int `json:"id"` UserName string `json:"user_name"` Password string `json:"password"` } @@ -91,16 +91,16 @@ func (this *UserController) ModifyPasswd(c *gin.Context) { resp.Msg = "err" defer func() { - c.JSON(200,resp) + c.JSON(200, resp) }() e := c.BindJSON(&req) - if nil != e{ + if nil != e { logs.Error(e.Error()) return } - e = model.ModyfyPassword(req.UserName,req.Password) - if nil != e{ + e = model.ModyfyPassword(req.UserName, req.Password) + if nil != e { logs.Error(e.Error()) return } @@ -137,7 +137,6 @@ func (this *UserController) GetUser(c *gin.Context) { resp.Data = users } - func (this *UserController) Users(c *gin.Context) { var statuscode int var resp RespBase @@ -169,7 +168,6 @@ func (this *UserController) Users(c *gin.Context) { resp.Data = dat } - func (this *UserController) SerarchUsers(c *gin.Context) { var statuscode int var resp RespBase @@ -234,7 +232,6 @@ func DefaultOption(c *gin.Context) { }() } - func (this *UserController) Login(c *gin.Context) { type LoginReq struct { RememberMe int32 `json:"remember_me"` @@ -258,7 +255,7 @@ func (this *UserController) Login(c *gin.Context) { h.Write([]byte(req.UserPwd)) // 需要加密的字符串为 123456 passmd5 := h.Sum(nil) var result []model.Users - query := fmt.Sprintf("select * from users where user_name = '%s'",req.UserName) + query := fmt.Sprintf("select * from users where user_name = '%s'", req.UserName) er := db.GetMysqlClient().Query2(query, &result) logs.Debug("sql query:" + query) @@ -395,7 +392,7 @@ func (this *UserController) Register(c *gin.Context) { func (this *UserController) Logout(c *gin.Context) { token := c.Param("token") - log.Print("logout token is ",token) + log.Print("logout token is ", token) var resp RespBase config.RedisOne().Del(token) resp.Msg = "退出成功" diff --git a/db/sqlManager.go b/db/sqlManager.go index 3746281..b2017c6 100644 --- a/db/sqlManager.go +++ b/db/sqlManager.go @@ -7,48 +7,62 @@ import ( "crypto/x509" "database/sql" "fmt" - "github.com/go-sql-driver/mysql" - _ "github.com/go-sql-driver/mysql" - "gopkg.in/mgo.v2" - "gopkg.in/olivere/elastic.v7" "io/ioutil" "log" "os" + + "github.com/go-sql-driver/mysql" + _ "github.com/go-sql-driver/mysql" + "github.com/jinzhu/gorm" + "gopkg.in/mgo.v2" + "gopkg.in/olivere/elastic.v7" ) + var gEla ElkEngine var gDb Database var blogDb Database var gMongo *mgo.Session var gElkEngine ElkEngine - +var gOrm *gorm.DB func Init() { + var e error mysqlconf := config.GetMysqlConfig() - blogConf := config.GetMysqlBlogConfig() //InitMongoDb() - fmt.Print("api runmode is " + config.ApiConfig().RunMode) - if config.ApiConfig().RunMode == "debug"{ - blogDb = Database{Type: string(""), DB: initMysqlTLS(blogConf)} - }else{ - blogDb = Database{Type: string(""), DB: initMysqlTLS(blogConf)} - } - if config.ApiConfig().RunMode == "debug"{ - gDb = Database{Type: string(""), DB: initMysqlTLS(mysqlconf)} - }else{ - gDb = Database{Type: string(""), DB: initMysqlTLS(mysqlconf)} + log.Print("api runmode is " + config.ApiConfig().RunMode) + if config.ApiConfig().RunMode == "debug" { + gDb = Database{Type: string(""), DB: initMysql(mysqlconf)} + sqls := fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local", + mysqlconf.UserName, mysqlconf.Password, mysqlconf.Addr, + mysqlconf.Db) + log.Print(sqls) + gOrm, e = gorm.Open("mysql", sqls) + if nil != e { + log.Print(e.Error()) + os.Exit(-1) + } + } else { + sqls := fmt.Sprintf("%s:%s@/%s?charset=utf8&parseTime=True&loc=Local", mysqlconf.UserName, mysqlconf.Password, + mysqlconf.Db) + gOrm, e = gorm.Open("mysql", sqls) + if nil != e { + log.Print(e.Error()) + os.Exit(-1) + } } + InitELK() } -func InitELK() { +func InitELK() { var e error elkconf := config.GetElkConfig() log.Print(elkconf) - gElkEngine.cli,e = elastic.NewClient( + gElkEngine.cli, e = elastic.NewClient( elastic.SetURL(elkconf.Address), // Must turn off sniff in docker - elastic.SetSniff(false),) - if nil != e{ + elastic.SetSniff(false)) + if nil != e { logs.Error(e.Error()) gElkEngine.cli = nil } @@ -72,7 +86,7 @@ func initMysql(mysqlconf *config.MysqlConfig) *sql.DB { fmt.Println(e.Error()) os.Exit(200) } - return _db + return _db } func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB { @@ -91,8 +105,8 @@ func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB { } clientCert = append(clientCert, certs) mysql.RegisterTLSConfig("custom", &tls.Config{ - RootCAs: rootCertPool, - Certificates: clientCert, + RootCAs: rootCertPool, + Certificates: clientCert, InsecureSkipVerify: true, }) cnn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&tls=custom", mysqlconf.UserName, mysqlconf.Password, @@ -108,22 +122,22 @@ func initMysqlTLS(mysqlconf *config.MysqlConfig) *sql.DB { fmt.Println(e.Error()) os.Exit(200) } - return _db + return _db } -func initMongoDb(conf *config.MongoConfig) *mgo.Session{ +func initMongoDb(conf *config.MongoConfig) *mgo.Session { var url string - if conf.UserName == ""{ - url = fmt.Sprintf("mongodb://%s:%d/%s",conf.Addr,conf.Port,conf.Db) + if conf.UserName == "" { + url = fmt.Sprintf("mongodb://%s:%d/%s", conf.Addr, conf.Port, conf.Db) - }else{ - url = fmt.Sprintf("mongodb://%s:%s@%s:%d/%s",conf.UserName,conf.Password,conf.Addr,conf.Port,conf.Db) + } else { + 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{ + mgo, err := mgo.Dial(url) + if nil != err { logs.Error(err.Error()) return nil } @@ -133,20 +147,23 @@ func initMongoDb(conf *config.MongoConfig) *mgo.Session{ func GetMysqlClient() *Database { return &gDb } -func GetBlogMysql() *Database{ - return &blogDb +func GetBlogMysql() *Database { + return &blogDb } func GetMongoDb() *mgo.Session { return gMongo } -func InitMongoDb() { +func InitMongoDb() { mongoConf := config.GetMongoDBConfig() gMongo = initMongoDb(mongoConf) - if gMongo == nil{ + if gMongo == nil { log.Print("error mongo initial") os.Exit(25) } } -func GetElastic() *ElkEngine{ +func GetElastic() *ElkEngine { return &gElkEngine -} \ No newline at end of file +} +func GetOrm() *gorm.DB { + return gOrm +} diff --git a/main.go b/main.go index 19e7e47..d13e5b3 100644 --- a/main.go +++ b/main.go @@ -8,12 +8,14 @@ import ( _ "background/docs" "background/logs" "background/model" - "github.com/gin-gonic/gin" - "github.com/swaggo/files" // swagger embed files - "github.com/swaggo/gin-swagger" // gin-swagger middleware - "github.com/tommy351/gin-sessions" "log" + "os" "strconv" + + "github.com/gin-gonic/gin" + swaggerFiles "github.com/swaggo/files" // swagger embed files + ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware + sessions "github.com/tommy351/gin-sessions" ) // @title 大厅功能api @@ -22,28 +24,27 @@ import ( // @BasePath /api/v1 var ( - userController = controller.UserController{} - mailContoller = controller.MailController{} - fileController = controller.FileController{} + userController = controller.UserController{} + mailContoller = controller.MailController{} + fileController = controller.FileController{} webhookController = controller.WebHookController{} ) - func CORSMiddleware(c *gin.Context) { c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE") - if config.ApiConfig().RunMode == "release"{ + if config.ApiConfig().RunMode == "release" { c.Writer.Header().Set("Access-Control-Allow-Origin", "https://testingcloud.club") - }else{ + } else { c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:8080") } c.Writer.Header().Set("Access-Control-Max-Age", "86400") c.Writer.Header().Set("Access-Control-Allow-Headers", - "X-Requested-With," + - " Content-Type, Origin, Authorization," + - "Accept,Client-Security-Token, Accept-Encoding," + - "x-access-token,Access-Control-Request-Method," + - "Access-Control-Request-Headers") + "X-Requested-With,"+ + " Content-Type, Origin, Authorization,"+ + "Accept,Client-Security-Token, Accept-Encoding,"+ + "x-access-token,Access-Control-Request-Method,"+ + "Access-Control-Request-Headers") c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length") c.Writer.Header().Set("Access-Control-Allow-Credentials", "true") @@ -79,9 +80,9 @@ func InitRedisConfig() { } } -func InitElasticSearch(){ - e := db.GetElastic().CreateIndex("hardware",model.HardwareTypeMapping()) - if nil != e{ +func InitElasticSearch() { + e := db.GetElastic().CreateIndex("hardware", model.HardwareTypeMapping()) + if nil != e { } } @@ -90,6 +91,8 @@ func InitLogs() { logs.Init(config.GetLogConfig().Dir, config.GetLogConfig().File, config.GetLogConfig().Level, config.GetLogConfig().SaveFile) } func main() { + log.Print("gid is", os.Getegid()) + InitConfig() InitLogs() InitRedisConfig() @@ -136,7 +139,7 @@ func main() { api.POST("/articles", controller.GetArticles) // 获取所有文章 api.PUT("/article", controller.AddArticle) // 添加文章 api.PUT("/article_search", controller.SearchArticle) // 添加文章 - api.GET("/article_type", controller.ArticlesType) //获取所有文章分类 + api.GET("/article_type", controller.ArticlesType) //获取所有文章分类 api.PUT("/article_type", controller.AddArticleType) api.DELETE("/article_type", controller.DeleteArticleType) @@ -156,16 +159,23 @@ func main() { api.GET("/filelist", fileController.FileList) // 文件列表 api.GET("/fileType", fileController.FileType) // 文件类型 - api.PUT("/memo", controller.CreateMemo) // 备忘录新建 - api.POST("/memo", controller.UpdateMemo) // 备忘录更新 - api.POST("/memos", controller.GetMemos) // 备忘录批量 - api.POST("/delmemo", controller.DeleteMemos) //删除备忘录 - api.GET("/memo", controller.GetMemo) // 单独读取 - api.GET("doc_groups",controller.GetDocGroup) // 获取所有的文章分组 + api.PUT("/memo", controller.CreateMemo) // 备忘录新建 + api.POST("/memo", controller.UpdateMemo) // 备忘录更新 + api.POST("/memos", controller.GetMemos) // 备忘录批量 + api.POST("/delmemo", controller.DeleteMemos) //删除备忘录 + api.GET("/memo", controller.GetMemo) // 单独读取 + api.GET("doc_groups", controller.GetDocGroup) // 获取所有的文章分组 + api.GET("templates", controller.GetDocTemplate) // 获取所有文章的模板 + + api.GET("doc_versions", nil) // 获取文章的某个版本 } hookapi := r.Group("hookapi") { - hookapi.POST("/push_hook/:repo",webhookController.PushHook) + hookapi.POST("/push_hook/:repo", webhookController.PushHook) + } + openapi := r.Group("openapi") + { + openapi.POST("negotiate") } e := r.Run(":" + strconv.Itoa(config.GetPort())) if nil != e { diff --git a/model/blog.go b/model/blog.go index fcf3afa..c95bc14 100644 --- a/model/blog.go +++ b/model/blog.go @@ -4,28 +4,29 @@ import ( "background/db" "background/logs" "fmt" - "qiniupkg.com/x/log.v7" "strings" + + "qiniupkg.com/x/log.v7" ) 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"` - IsPublic int `sql:"is_public" json:"is_public"` + 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"` + IsPublic int `sql:"is_public" json:"is_public"` } type DocGroup struct { - Int int32 `sql:"int"` + Int int32 `sql:"int"` Name string `sql:"name"` } type ArticleType struct { - Id int64 `sql:"id" json:"id"` - Name string `sql:"type_name" json:"type_name"` - Author string `sql:"author" json:"author"` - Group int32 `sql:"group" json:"group"` + Id int64 `sql:"id" json:"id"` + Name string `sql:"type_name" json:"type_name"` + Author string `sql:"author" json:"author"` + Group int32 `sql:"group" json:"group"` GroupName string `json:"group_name"` } @@ -33,11 +34,11 @@ func GetArticlesType() []ArticleType { ret := []ArticleType{} sql := fmt.Sprintf("select * from doc_type") e := db.GetBlogMysql().Query2(sql, &ret) - for k,_ := range ret{ + for k, _ := range ret { group := []DocGroup{} - sql = fmt.Sprintf("select * from doc_group where doc_group.int = %d",ret[k].Group) + sql = fmt.Sprintf("select * from doc_group where doc_group.int = %d", ret[k].Group) db.GetBlogMysql().Query2(sql, &group) - if len(group) > 0{ + if len(group) > 0 { ret[k].GroupName = group[0].Name } } @@ -60,7 +61,7 @@ func CreateDoc(doc Doc) error { DUAL WHERE NOT EXISTS ( SELECT * FROM doc WHERE doc.title = '%s' );`, doc.Title, strings.Replace(doc.Content, "'", "\\'", -1), - doc.Author, doc.Type,doc.IsPublic,doc.Title) + doc.Author, doc.Type, doc.IsPublic, doc.Title) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) @@ -69,10 +70,10 @@ func CreateDoc(doc Doc) error { return nil } -func UpdateDoc(doc Doc) error{ +func UpdateDoc(doc Doc) error { sql := fmt.Sprintf(`update doc set doc.author = '%s' ,doc.title = '%s',doc.type = '%d',doc.content = '%s' where doc.id = '%d'; `, - doc.Author, doc.Title, doc.Type, - strings.Replace(doc.Content, "'", "\\'", -1),doc.ID) + doc.Author, doc.Title, doc.Type, + strings.Replace(doc.Content, "'", "\\'", -1), doc.ID) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) @@ -81,8 +82,8 @@ func UpdateDoc(doc Doc) error{ return nil } -func DeleteDoc(id int64) error{ - sql := fmt.Sprintf(`delete from doc where id = %d`,id) +func DeleteDoc(id int64) error { + sql := fmt.Sprintf(`delete from doc where id = %d`, id) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) @@ -91,8 +92,8 @@ func DeleteDoc(id int64) error{ return nil } -func AddArticleType(t ArticleType) error{ - sql := fmt.Sprintf("insert into doc_type(id,type_name,`group`) values ('%d','%s','%d')",t.Id,t.Name,t.Group) +func AddArticleType(t ArticleType) error { + sql := fmt.Sprintf("insert into doc_type(id,type_name,`group`) values ('%d','%s','%d')", t.Id, t.Name, t.Group) log.Print(sql) _, e := db.GetMysqlClient().Query(sql) if nil != e { @@ -101,18 +102,8 @@ func AddArticleType(t ArticleType) error{ } return nil } -func UpdateArticleType(t ArticleType) error{ - sql := fmt.Sprintf("update doc_type set type_name = '%s' and group = '%d' where id = %d",t.Name,t.Group,t.Id) - _, e := db.GetMysqlClient().Query(sql) - if nil != e { - logs.Error(e.Error()) - return e - } - return nil - -} -func DeleteArticleType(id int32) error { - sql := fmt.Sprintf("delete from doc_type where id = '%d'",id) +func UpdateArticleType(t ArticleType) error { + sql := fmt.Sprintf("update doc_type set type_name = '%s' and group = '%d' where id = %d", t.Name, t.Group, t.Id) _, e := db.GetMysqlClient().Query(sql) if nil != e { logs.Error(e.Error()) @@ -121,24 +112,34 @@ func DeleteArticleType(id int32) error { return nil } -func GetAllDocs() ([]Doc,error) { +func DeleteArticleType(id int32) error { + sql := fmt.Sprintf("delete from doc_type where id = '%d'", id) + _, e := db.GetMysqlClient().Query(sql) + if nil != e { + logs.Error(e.Error()) + return e + } + return nil + +} +func GetAllDocs() ([]Doc, error) { ret := []Doc{} sql := fmt.Sprintf("select * from doc") - e := db.GetMysqlClient().Query2(sql,&ret) + e := db.GetMysqlClient().Query2(sql, &ret) if nil != e { logs.Error(e.Error()) - return nil,e + return nil, e } - return ret,nil + return ret, nil } -func GetAllGroup() ([]DocGroup,error) { +func GetAllGroup() ([]DocGroup, error) { ret := []DocGroup{} sql := fmt.Sprintf("select * from doc_group") - e := db.GetMysqlClient().Query2(sql,&ret) + e := db.GetMysqlClient().Query2(sql, &ret) if nil != e { logs.Error(e.Error()) - return nil,e + return nil, e } - return ret,nil -} \ No newline at end of file + return ret, nil +}