forked from open-falcon/falcon-plus
/
simple_reponse.go
63 lines (59 loc) · 1.25 KB
/
simple_reponse.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
58
59
60
61
62
63
package helper
import (
"encoding/json"
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
type RespJson struct {
Error string `json:"error,omitempty"`
Msg string `json:"message,omitempty"`
}
// func JSONR(c *gin.Context, wcode int, msg interface{}) (werror error) {
func JSONR(c *gin.Context, arg ...interface{}) (werror error) {
var (
wcode int
msg interface{}
)
if len(arg) == 1 {
wcode = http.StatusOK
msg = arg[0]
} else {
wcode = arg[0].(int)
msg = arg[1]
}
need_doc := viper.GetBool("gen_doc")
var body interface{}
defer func() {
if need_doc {
ds, _ := json.Marshal(body)
bodys := string(ds)
log.Debugf("body: %v, bodys: %v ", body, bodys)
c.Set("body_doc", bodys)
}
}()
if wcode == 200 {
switch msg.(type) {
case string:
body = RespJson{Msg: msg.(string)}
c.JSON(http.StatusOK, body)
default:
c.JSON(http.StatusOK, msg)
body = msg
}
} else {
switch msg.(type) {
case string:
body = RespJson{Error: msg.(string)}
c.JSON(wcode, body)
case error:
body = RespJson{Error: msg.(error).Error()}
c.JSON(wcode, body)
default:
body = RespJson{Error: "system type error. please ask admin for help"}
c.JSON(wcode, body)
}
}
return
}