From 3e99618b5646c898ba231f3d29ce21538e6479b3 Mon Sep 17 00:00:00 2001 From: zhanghan2021 Date: Wed, 14 Jun 2023 15:55:49 +0800 Subject: [PATCH] packaging response body --- prometheus/server/httphandler/target.go | 6 ++-- .../server/httphandler/targetManager.go | 33 ++++--------------- sdk/response/response.go | 21 ++++++++++++ 3 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 sdk/response/response.go diff --git a/prometheus/server/httphandler/target.go b/prometheus/server/httphandler/target.go index e01535a7..98b20bca 100644 --- a/prometheus/server/httphandler/target.go +++ b/prometheus/server/httphandler/target.go @@ -6,6 +6,7 @@ import ( "net/http/httputil" "net/url" + "gitee.com/openeuler/PilotGo-plugins/sdk/response" "github.com/gin-gonic/gin" "openeuler.org/PilotGo/prometheus-plugin/httphandler/service" ) @@ -13,10 +14,7 @@ import ( func DBTargets(c *gin.Context) { targets, err := service.GetPrometheusTarget() if err != nil { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusBadRequest, - "data": nil, - "msg": err.Error()}) + response.Fail(c, nil, err.Error()) return } diff --git a/prometheus/server/httphandler/targetManager.go b/prometheus/server/httphandler/targetManager.go index 74678a19..79e40c82 100644 --- a/prometheus/server/httphandler/targetManager.go +++ b/prometheus/server/httphandler/targetManager.go @@ -1,8 +1,7 @@ package httphandler import ( - "net/http" - + "gitee.com/openeuler/PilotGo-plugins/sdk/response" "github.com/gin-gonic/gin" "openeuler.org/PilotGo/prometheus-plugin/httphandler/service" ) @@ -11,46 +10,28 @@ import ( func AddPrometheusTarget(c *gin.Context) { var target service.PrometheusTarget if err := c.BindJSON(&target); err != nil { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusBadRequest, - "data": nil, - "msg": err.Error()}) + response.Fail(c, nil, err.Error()) return } err := service.AddPrometheusTarget(target) if err != nil { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusBadRequest, - "data": nil, - "msg": err.Error()}) + response.Fail(c, nil, err.Error()) return } - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "data": nil, - "msg": "添加成功"}) + response.Success(c, nil, "添加成功") } // 将监控target从prometheus插件db中删除 func DeletePrometheusTarget(c *gin.Context) { var target service.PrometheusTarget if err := c.BindJSON(&target); err != nil { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusBadRequest, - "data": nil, - "msg": err.Error()}) + response.Fail(c, nil, err.Error()) return } err := service.DeletePrometheusTarget(target) if err != nil { - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusBadRequest, - "data": nil, - "msg": err.Error()}) + response.Fail(c, nil, err.Error()) return } - c.JSON(http.StatusOK, gin.H{ - "code": http.StatusOK, - "data": nil, - "msg": "删除成功"}) + response.Success(c, nil, "删除成功") } diff --git a/sdk/response/response.go b/sdk/response/response.go new file mode 100644 index 00000000..84da3434 --- /dev/null +++ b/sdk/response/response.go @@ -0,0 +1,21 @@ +package response + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func Success(c *gin.Context, data interface{}, msg string) { + result(c, http.StatusOK, http.StatusOK, data, msg) +} +func Fail(c *gin.Context, data interface{}, msg string) { + result(c, http.StatusOK, http.StatusBadRequest, data, msg) +} + +func result(c *gin.Context, httpStatus int, code int, data interface{}, msg string) { + c.JSON(httpStatus, gin.H{ + "code": code, + "data": data, + "msg": msg}) +} -- Gitee