/
response.go
52 lines (43 loc) · 1.36 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package response
import (
"net/http"
"github.com/axiaoxin/pink-lady/app/services/retcode"
"github.com/gin-gonic/gin"
)
// Response the unified json structure
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
// JSON respond unified JSON structure with 200 http status code
func JSON(c *gin.Context, rc *retcode.RetCode, data interface{}) {
Respond(c, http.StatusOK, rc, data)
}
// JSON400 respond unified JSON structure with 400 http status code
func JSON400(c *gin.Context, rc *retcode.RetCode, data interface{}) {
Respond(c, http.StatusBadRequest, rc, data)
}
// JSON404 respond unified JSON structure with 404 http status code
func JSON404(c *gin.Context, rc *retcode.RetCode, data interface{}) {
Respond(c, http.StatusNotFound, rc, data)
}
// JSON500 respond unified JSON structure with 500 http status code
func JSON500(c *gin.Context, rc *retcode.RetCode, data interface{}) {
Respond(c, http.StatusInternalServerError, rc, data)
}
// Respond encapsulates c.JSON
// debug mode respond indented json
func Respond(c *gin.Context, status int, rc *retcode.RetCode, data interface{}) {
code, msg := rc.Decode()
resp := Response{
Code: code,
Message: msg,
Data: data,
}
if gin.Mode() == gin.ReleaseMode {
c.JSON(status, resp)
} else {
c.IndentedJSON(status, resp)
}
}