-
Notifications
You must be signed in to change notification settings - Fork 0
/
respond.go
64 lines (45 loc) · 1.2 KB
/
respond.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
53
54
55
56
57
package web
// respond with JSON
import "fmt"
import "github.com/gin-gonic/gin"
func respond(c *gin.Context, statusCode int, data gin.H) {
if statusCode < 100 {
statusCode = 200
}
if data == nil {
data = gin.H{
"code": 0,
"msg": StatusCode[statusCode],
}
}
c.JSON(statusCode, data)
c.Abort()
}
func respondError(c *gin.Context, statusCode int, data gin.H) {
if statusCode < 100 {
statusCode = 500
}
respond(c, statusCode, data)
}
func Success(c *gin.Context, data map[string]interface{}) {
respond(c, 200, data)
}
func BadRequest(c *gin.Context, data map[string]interface{}) {
respondError(c, 400, data)
}
func Unauthorized(c *gin.Context, data map[string]interface{}) {
respondError(c, 401, data)
}
func Forbidden(c *gin.Context, data map[string]interface{}) {
respondError(c, 403, data)
}
func NotFound(c *gin.Context, data map[string]interface{}) {
respondError(c, 404, data)
}
func ServerError(c *gin.Context, data map[string]interface{}) {
respondError(c, 500, data)
}
//cache
func CacheControl(c *gin.Context, max int) {
c.Header("Cache-Control", fmt.Sprintf("max-age=%d", max))
}