/
json.go
85 lines (67 loc) · 2.05 KB
/
json.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
/*
Package response provides helpers and utils for working with HTTP response
*/
package json
import (
"context"
"encoding/json"
"fmt"
"net/http"
httperrors "github.com/vardius/go-api-boilerplate/pkg/http/errors"
"github.com/vardius/go-api-boilerplate/pkg/http/response"
)
type HandlerFunc func(w http.ResponseWriter, r *http.Request) error
// ServeHTTP calls f(w, r) and handles error
func (f HandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := f(w, r); err != nil {
MustJSONError(r.Context(), w, err)
}
}
// JSON returns data as json response
func JSON(ctx context.Context, w http.ResponseWriter, statusCode int, payload interface{}) error {
w.Header().Set("Content-Type", "application/json")
// If there is nothing to marshal then set status code and return.
if payload == nil {
_, err := w.Write([]byte("{}"))
return err
}
if statusCode != http.StatusOK {
w.WriteHeader(statusCode)
}
encoder := json.NewEncoder(w)
encoder.SetEscapeHTML(true)
encoder.SetIndent("", "")
if err := encoder.Encode(payload); err != nil {
return err
}
response.Flush(w)
return nil
}
// MustJSON returns data as json response
// will panic if unable to marshal payload into JSON object
// uses JSON internally
func MustJSON(ctx context.Context, w http.ResponseWriter, statusCode int, payload interface{}) {
if err := JSON(ctx, w, statusCode, payload); err != nil {
panic(err)
}
}
func NotFound() http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) error {
httpError := &httperrors.HttpError{
Code: http.StatusNotFound,
Message: fmt.Sprintf("Route %s %s", r.URL.Path, http.StatusText(http.StatusNotFound)),
}
return JSON(r.Context(), w, httpError.Code, httpError)
}
return HandlerFunc(fn)
}
func NotAllowed() http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) error {
httpError := &httperrors.HttpError{
Code: http.StatusMethodNotAllowed,
Message: http.StatusText(http.StatusMethodNotAllowed),
}
return JSON(r.Context(), w, httpError.Code, httpError)
}
return HandlerFunc(fn)
}