From 9e7b3848368bc2205ab2f2a40266d5d1ee0248a6 Mon Sep 17 00:00:00 2001 From: a7458969 <290198252@qq.com> Date: Wed, 25 Mar 2020 10:57:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0mq=E5=92=8Ccontroller?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 2 +- controller/hardware.go | 17 ++++++++++++++--- db/elasticEngine.go | 1 - db/sqlManager.go | 2 ++ main.go | 2 ++ model/hardware.go | 19 +++++++++++++++---- 6 files changed, 34 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index a792bfa..a628535 100644 --- a/config/config.go +++ b/config/config.go @@ -28,7 +28,7 @@ type ConfAPI struct { init bool } type ElasticSearch struct{ - Address string `yaml:"string"` + Address string `yaml:"address"` } type EntityRedis struct { diff --git a/controller/hardware.go b/controller/hardware.go index bf01d40..87ec3ca 100644 --- a/controller/hardware.go +++ b/controller/hardware.go @@ -1,9 +1,9 @@ package controller import ( - + "background/model" "github.com/gin-gonic/gin" - + "qiniupkg.com/x/log.v7" ) @@ -12,14 +12,25 @@ func AddHardware(c *gin.Context) { defer func() { c.JSON(200,resp) }() + var hardware model.Hardware + e := c.BindJSON(&hardware) + if nil != e{ + log.Print(e) + print(e) + return + } + if nil != hardware.CreateHardware(){ + print(e) + return + } resp.Data = nil resp.Msg = "OK" resp.Status = 0 } func DeleteHardWare(c *gin.Context) { - + } func UpdateHardWare(c *gin.Context) { diff --git a/db/elasticEngine.go b/db/elasticEngine.go index aa48453..5828e70 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -19,7 +19,6 @@ type ElkEngine struct { - func (p *ElkEngine)Create(index string,types string,id string,data interface{}) (error) { if nil != p{ if (reflect.TypeOf(data).Kind() != reflect.String) && (reflect.TypeOf(data).Kind() != reflect.Struct){ diff --git a/db/sqlManager.go b/db/sqlManager.go index 7685fbf..7962bd3 100644 --- a/db/sqlManager.go +++ b/db/sqlManager.go @@ -38,11 +38,13 @@ func Init() { }else{ gDb = Database{Type: string(""), DB: initMysqlTLS(mysqlconf)} } + InitELK() } func InitELK() { var e error elkconf := config.GetElkConfig() + log.Print(elkconf) gElkEngine.cli,e = elastic.NewClient( elastic.SetURL(elkconf.Address), // Must turn off sniff in docker diff --git a/main.go b/main.go index 5a4f995..7f8ddcc 100644 --- a/main.go +++ b/main.go @@ -116,6 +116,8 @@ func main() { api.POST("/image_upload",fileController.OnUpload) // 上传图片 api.GET("/image_download/:file",fileController.OnDownLoad) // 下载图片 api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型 + + api.POST("/hardware",controller.AddHardware) // 新增硬件 } e := r.Run(":" + strconv.Itoa(config.GetPort())) diff --git a/model/hardware.go b/model/hardware.go index 868a58c..b217af9 100644 --- a/model/hardware.go +++ b/model/hardware.go @@ -1,6 +1,11 @@ package model -import "time" +import ( + "background/db" + "github.com/pkg/errors" + "qiniupkg.com/x/log.v7" + "time" +) // this api is based on elasticsearch type Hardware struct { @@ -12,8 +17,14 @@ type Hardware struct { Doc string `json:"doc"` //文档资料 } -func (*Hardware )Create() error{ - - +func (this *Hardware )CreateHardware( ) error{ + if nil == this{ + return errors.New("null pointer") + } + e := db.GetElastic().Create("hardware","0","",*this) + if nil != e{ + log.Print(e.Error()) + return e + } return nil; } \ No newline at end of file