grafana/pkg/api/common.go

138 lines
2.6 KiB
Go
Raw Normal View History

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 {
2018-03-22 16:13:46 -05:00
return Error(404, "Not found", nil)
}
ServerError = func(err error) Response {
2018-03-22 16:13:46 -05:00
return Error(500, "Server error", err)
}
)
type Response interface {
2018-03-07 10:54:50 -06:00
WriteTo(ctx *m.ReqContext)
}
type NormalResponse struct {
status int
body []byte
header http.Header
errMessage string
err error
}
2018-07-02 07:10:39 -05:00
func Wrap(action interface{}) macaron.Handler {
2018-03-07 10:54:50 -06:00
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)
}
}
2018-03-07 10:54:50 -06:00
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
}
2018-03-22 16:13:46 -05:00
// Empty create an empty response
func Empty(status int) *NormalResponse {
return Respond(status, nil)
}
2018-03-22 16:13:46 -05:00
// JSON create a JSON response
func JSON(status int, body interface{}) *NormalResponse {
return Respond(status, body).Header("Content-Type", "application/json")
}
2018-03-22 16:13:46 -05:00
// Success create a successful response
func Success(message string) *NormalResponse {
resp := make(map[string]interface{})
resp["message"] = message
2018-03-22 16:13:46 -05:00
return JSON(200, resp)
}
2018-03-22 16:13:46 -05:00
// 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()
}
}
2018-03-22 16:13:46 -05:00
resp := JSON(status, data)
if err != nil {
resp.errMessage = message
resp.err = err
}
return resp
}
2018-03-22 16:13:46 -05:00
// 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 {
2018-03-22 16:13:46 -05:00
return Error(500, "body json marshal", err)
}
}
return &NormalResponse{
body: b,
status: status,
header: make(http.Header),
}
}