mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-03 23:06:36 +08:00
feat: 重构响应处理,添加通用响应函数以简化代码
This commit is contained in:
@@ -3,8 +3,6 @@ package controller
|
|||||||
import (
|
import (
|
||||||
"atsflare/model"
|
"atsflare/model"
|
||||||
"atsflare/service"
|
"atsflare/service"
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -21,13 +19,11 @@ import (
|
|||||||
// @Router /api/agent/nodes/register [post]
|
// @Router /api/agent/nodes/register [post]
|
||||||
func AgentRegister(c *gin.Context) {
|
func AgentRegister(c *gin.Context) {
|
||||||
var payload service.AgentNodePayload
|
var payload service.AgentNodePayload
|
||||||
if err := json.NewDecoder(c.Request.Body).Decode(&payload); err != nil {
|
if err := decodeJSONBody(c.Request.Body, &payload); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
result *service.AgentRegistrationResponse
|
result *service.AgentRegistrationResponse
|
||||||
err error
|
err error
|
||||||
@@ -38,17 +34,10 @@ func AgentRegister(c *gin.Context) {
|
|||||||
result, err = service.RegisterNodeWithDiscovery(payload)
|
result, err = service.RegisterNodeWithDiscovery(payload)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, result)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": result,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AgentHeartbeat godoc
|
// AgentHeartbeat godoc
|
||||||
@@ -63,33 +52,23 @@ func AgentRegister(c *gin.Context) {
|
|||||||
// @Router /api/agent/nodes/heartbeat [post]
|
// @Router /api/agent/nodes/heartbeat [post]
|
||||||
func AgentHeartbeat(c *gin.Context) {
|
func AgentHeartbeat(c *gin.Context) {
|
||||||
var payload service.AgentNodePayload
|
var payload service.AgentNodePayload
|
||||||
if err := json.NewDecoder(c.Request.Body).Decode(&payload); err != nil {
|
if err := decodeJSONBody(c.Request.Body, &payload); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
authNode, ok := c.Get("agent_node")
|
authNode, ok := c.Get("agent_node")
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{
|
respondUnauthorized(c, "鏃犳潈杩涜姝ゆ搷浣滐紝Agent Token 鏃犳晥")
|
||||||
"success": false,
|
|
||||||
"message": "无权进行此操作,Agent Token 无效",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := service.HeartbeatNode(authNode.(*model.Node), payload)
|
node, err := service.HeartbeatNode(authNode.(*model.Node), payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccessWithExtras(c, node.Node, gin.H{
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": node.Node,
|
|
||||||
"agent_settings": node.AgentSettings,
|
"agent_settings": node.AgentSettings,
|
||||||
"active_config": node.ActiveConfig,
|
"active_config": node.ActiveConfig,
|
||||||
})
|
})
|
||||||
@@ -105,17 +84,10 @@ func AgentHeartbeat(c *gin.Context) {
|
|||||||
func AgentGetActiveConfig(c *gin.Context) {
|
func AgentGetActiveConfig(c *gin.Context) {
|
||||||
config, err := service.GetActiveConfigForAgent()
|
config, err := service.GetActiveConfigForAgent()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, "褰撳墠娌℃湁婵€娲荤増鏈?")
|
||||||
"success": false,
|
|
||||||
"message": "当前没有激活版本",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, config)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": config,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AgentReportApplyLog godoc
|
// AgentReportApplyLog godoc
|
||||||
@@ -130,30 +102,21 @@ func AgentGetActiveConfig(c *gin.Context) {
|
|||||||
// @Router /api/agent/apply-logs [post]
|
// @Router /api/agent/apply-logs [post]
|
||||||
func AgentReportApplyLog(c *gin.Context) {
|
func AgentReportApplyLog(c *gin.Context) {
|
||||||
var payload service.ApplyLogPayload
|
var payload service.ApplyLogPayload
|
||||||
if err := json.NewDecoder(c.Request.Body).Decode(&payload); err != nil {
|
if err := decodeJSONBody(c.Request.Body, &payload); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
authNode, ok := c.Get("agent_node")
|
|
||||||
if ok {
|
if authNode, ok := c.Get("agent_node"); ok {
|
||||||
payload.NodeID = authNode.(*model.Node).NodeID
|
payload.NodeID = authNode.(*model.Node).NodeID
|
||||||
}
|
}
|
||||||
|
|
||||||
log, err := service.ReportApplyLog(payload)
|
log, err := service.ReportApplyLog(payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, log)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": log,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNodes godoc
|
// GetNodes godoc
|
||||||
@@ -166,17 +129,10 @@ func AgentReportApplyLog(c *gin.Context) {
|
|||||||
func GetNodes(c *gin.Context) {
|
func GetNodes(c *gin.Context) {
|
||||||
nodes, err := service.ListNodeViews()
|
nodes, err := service.ListNodeViews()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, nodes)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": nodes,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetApplyLogs godoc
|
// GetApplyLogs godoc
|
||||||
@@ -190,15 +146,8 @@ func GetNodes(c *gin.Context) {
|
|||||||
func GetApplyLogs(c *gin.Context) {
|
func GetApplyLogs(c *gin.Context) {
|
||||||
logs, err := service.ListApplyLogs(c.Query("node_id"))
|
logs, err := service.ListApplyLogs(c.Query("node_id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, logs)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": logs,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
+35
-110
@@ -2,10 +2,6 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"atsflare/service"
|
"atsflare/service"
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -28,26 +24,17 @@ type nodeAgentUpdateRequest struct {
|
|||||||
// @Router /api/nodes/ [post]
|
// @Router /api/nodes/ [post]
|
||||||
func CreateNode(c *gin.Context) {
|
func CreateNode(c *gin.Context) {
|
||||||
var input service.NodeInput
|
var input service.NodeInput
|
||||||
if err := json.NewDecoder(c.Request.Body).Decode(&input); err != nil {
|
if err := decodeJSONBody(c.Request.Body, &input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := service.CreateNode(input)
|
node, err := service.CreateNode(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, node)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": node,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNodeBootstrapToken godoc
|
// GetNodeBootstrapToken godoc
|
||||||
@@ -60,17 +47,10 @@ func CreateNode(c *gin.Context) {
|
|||||||
func GetNodeBootstrapToken(c *gin.Context) {
|
func GetNodeBootstrapToken(c *gin.Context) {
|
||||||
bootstrap, err := service.GetNodeBootstrapView()
|
bootstrap, err := service.GetNodeBootstrapView()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, bootstrap)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": bootstrap,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RotateNodeBootstrapToken godoc
|
// RotateNodeBootstrapToken godoc
|
||||||
@@ -83,17 +63,10 @@ func GetNodeBootstrapToken(c *gin.Context) {
|
|||||||
func RotateNodeBootstrapToken(c *gin.Context) {
|
func RotateNodeBootstrapToken(c *gin.Context) {
|
||||||
bootstrap, err := service.RotateGlobalDiscoveryToken()
|
bootstrap, err := service.RotateGlobalDiscoveryToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, bootstrap)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": bootstrap,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateNode godoc
|
// UpdateNode godoc
|
||||||
@@ -110,33 +83,22 @@ func RotateNodeBootstrapToken(c *gin.Context) {
|
|||||||
func UpdateNode(c *gin.Context) {
|
func UpdateNode(c *gin.Context) {
|
||||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||||
if err != nil || id == 0 {
|
if err != nil || id == 0 {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var input service.NodeInput
|
var input service.NodeInput
|
||||||
if err = json.NewDecoder(c.Request.Body).Decode(&input); err != nil {
|
if err = decodeJSONBody(c.Request.Body, &input); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := service.UpdateNode(uint(id), input)
|
node, err := service.UpdateNode(uint(id), input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, node)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": node,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteNode godoc
|
// DeleteNode godoc
|
||||||
@@ -151,23 +113,15 @@ func UpdateNode(c *gin.Context) {
|
|||||||
func DeleteNode(c *gin.Context) {
|
func DeleteNode(c *gin.Context) {
|
||||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||||
if err != nil || id == 0 {
|
if err != nil || id == 0 {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = service.DeleteNode(uint(id)); err != nil {
|
if err = service.DeleteNode(uint(id)); err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccessMessage(c, "")
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequestNodeAgentUpdate godoc
|
// RequestNodeAgentUpdate godoc
|
||||||
@@ -182,38 +136,27 @@ func DeleteNode(c *gin.Context) {
|
|||||||
func RequestNodeAgentUpdate(c *gin.Context) {
|
func RequestNodeAgentUpdate(c *gin.Context) {
|
||||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||||
if err != nil || id == 0 {
|
if err != nil || id == 0 {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var request nodeAgentUpdateRequest
|
var request nodeAgentUpdateRequest
|
||||||
if c.Request.ContentLength > 0 {
|
if c.Request.ContentLength > 0 {
|
||||||
if err = json.NewDecoder(c.Request.Body).Decode(&request); err != nil && !errors.Is(err, io.EOF) {
|
if err = decodeOptionalJSONBody(c.Request.Body, &request); err != nil {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := service.RequestNodeAgentUpdate(uint(id), service.NodeAgentUpdateInput{
|
node, err := service.RequestNodeAgentUpdate(uint(id), service.NodeAgentUpdateInput{
|
||||||
Channel: request.Channel,
|
Channel: request.Channel,
|
||||||
TagName: request.TagName,
|
TagName: request.TagName,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, node)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": node,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequestNodeOpenrestyRestart godoc
|
// RequestNodeOpenrestyRestart godoc
|
||||||
@@ -228,25 +171,16 @@ func RequestNodeAgentUpdate(c *gin.Context) {
|
|||||||
func RequestNodeOpenrestyRestart(c *gin.Context) {
|
func RequestNodeOpenrestyRestart(c *gin.Context) {
|
||||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||||
if err != nil || id == 0 {
|
if err != nil || id == 0 {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
node, err := service.RequestNodeOpenrestyRestart(uint(id))
|
node, err := service.RequestNodeOpenrestyRestart(uint(id))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, node)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": node,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNodeAgentRelease godoc
|
// GetNodeAgentRelease godoc
|
||||||
@@ -262,23 +196,14 @@ func RequestNodeOpenrestyRestart(c *gin.Context) {
|
|||||||
func GetNodeAgentRelease(c *gin.Context) {
|
func GetNodeAgentRelease(c *gin.Context) {
|
||||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||||
if err != nil || id == 0 {
|
if err != nil || id == 0 {
|
||||||
c.JSON(http.StatusBadRequest, gin.H{
|
respondBadRequest(c, "")
|
||||||
"success": false,
|
|
||||||
"message": "无效的参数",
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
release, err := service.GetNodeAgentRelease(c.Request.Context(), uint(id), c.Query("channel"))
|
release, err := service.GetNodeAgentRelease(c.Request.Context(), uint(id), c.Query("channel"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondFailure(c, err.Error())
|
||||||
"success": false,
|
|
||||||
"message": err.Error(),
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
respondSuccess(c, release)
|
||||||
"success": true,
|
|
||||||
"message": "",
|
|
||||||
"data": release,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
const invalidParamsMessage = "鏃犳晥鐨勫弬鏁?"
|
||||||
|
|
||||||
|
func respondSuccess(c *gin.Context, data any) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": true,
|
||||||
|
"message": "",
|
||||||
|
"data": data,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func respondSuccessWithExtras(c *gin.Context, data any, extras gin.H) {
|
||||||
|
payload := gin.H{
|
||||||
|
"success": true,
|
||||||
|
"message": "",
|
||||||
|
"data": data,
|
||||||
|
}
|
||||||
|
for key, value := range extras {
|
||||||
|
payload[key] = value
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
func respondSuccessMessage(c *gin.Context, message string) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": true,
|
||||||
|
"message": message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func respondFailure(c *gin.Context, message string) {
|
||||||
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func respondBadRequest(c *gin.Context, message string) {
|
||||||
|
if message == "" {
|
||||||
|
message = invalidParamsMessage
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func respondUnauthorized(c *gin.Context, message string) {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{
|
||||||
|
"success": false,
|
||||||
|
"message": message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeJSONBody(body io.Reader, target any) error {
|
||||||
|
return json.NewDecoder(body).Decode(target)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeOptionalJSONBody(body io.Reader, target any) error {
|
||||||
|
if err := json.NewDecoder(body).Decode(target); err != nil && !errors.Is(err, io.EOF) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user