forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
137 lines (114 loc) · 2.59 KB
/
common.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package api
import (
"encoding/json"
"net/http"
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
"gopkg.in/macaron.v1"
)
var (
NotFound = func() Response {
return Error(404, "Not found", nil)
}
ServerError = func(err error) Response {
return Error(500, "Server error", err)
}
)
type Response interface {
WriteTo(ctx *m.ReqContext)
}
type NormalResponse struct {
status int
body []byte
header http.Header
errMessage string
err error
}
func wrap(action interface{}) macaron.Handler {
return func(c *m.ReqContext) {
var res Response
val, err := c.Invoke(action)
if err == nil && val != nil && len(val) > 0 {
res = val[0].Interface().(Response)
} else {
res = ServerError(err)
}
res.WriteTo(c)
}
}
func (r *NormalResponse) WriteTo(ctx *m.ReqContext) {
if r.err != nil {
ctx.Logger.Error(r.errMessage, "error", r.err)
}
header := ctx.Resp.Header()
for k, v := range r.header {
header[k] = v
}
ctx.Resp.WriteHeader(r.status)
ctx.Resp.Write(r.body)
}
func (r *NormalResponse) Cache(ttl string) *NormalResponse {
return r.Header("Cache-Control", "public,max-age="+ttl)
}
func (r *NormalResponse) Header(key, value string) *NormalResponse {
r.header.Set(key, value)
return r
}
// Empty create an empty response
func Empty(status int) *NormalResponse {
return Respond(status, nil)
}
// JSON create a JSON response
func JSON(status int, body interface{}) *NormalResponse {
return Respond(status, body).Header("Content-Type", "application/json")
}
// Success create a successful response
func Success(message string) *NormalResponse {
resp := make(map[string]interface{})
resp["message"] = message
return JSON(200, resp)
}
// Error create a erroneous response
func Error(status int, message string, err error) *NormalResponse {
data := make(map[string]interface{})
switch status {
case 404:
data["message"] = "Not Found"
case 500:
data["message"] = "Internal Server Error"
}
if message != "" {
data["message"] = message
}
if err != nil {
if setting.Env != setting.PROD {
data["error"] = err.Error()
}
}
resp := JSON(status, data)
if err != nil {
resp.errMessage = message
resp.err = err
}
return resp
}
// Respond create a response
func Respond(status int, body interface{}) *NormalResponse {
var b []byte
var err error
switch t := body.(type) {
case []byte:
b = t
case string:
b = []byte(t)
default:
if b, err = json.Marshal(body); err != nil {
return Error(500, "body json marshal", err)
}
}
return &NormalResponse{
body: b,
status: status,
header: make(http.Header),
}
}