-
Notifications
You must be signed in to change notification settings - Fork 0
/
httputil.go
112 lines (90 loc) · 2.5 KB
/
httputil.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
package http
import (
"fmt"
"net/http"
"reflect"
jsoniter "github.com/json-iterator/go"
"github.com/kudarap/ratatxt/errors"
"github.com/kudarap/ratatxt/ratatxt"
)
var json = jsoniter.ConfigFastest
type httpMsg struct {
Error bool `json:"error,omitempty"`
Typ string `json:"type,omitempty"`
Msg string `json:"message"`
}
func newMsg(msg string) httpMsg {
return httpMsg{Msg: msg}
}
func newError(err error) interface{} {
m := httpMsg{}
m.Error = true
m.Msg = err.Error()
return m
}
func handleWithError(h func(w http.ResponseWriter, r *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if err := h(w, r); err != nil {
respondError(w, err)
return
}
}
}
const respContentType = "application/json"
func respond(w http.ResponseWriter, code int, body interface{}) {
w.Header().Set("Content-Type", respContentType)
w.WriteHeader(code)
// Check for string response body.
if s, ok := body.(string); ok {
_, _ = w.Write([]byte(s))
return
}
// Generate the response
enc := json.NewEncoder(w)
if err := enc.Encode(body); err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf("could not encode body into JSON: %s", err)))
}
}
func respondOK(w http.ResponseWriter, body interface{}) {
respond(w, http.StatusOK, body)
}
func respondError(w http.ResponseWriter, err error) {
var body interface{}
status := http.StatusBadRequest
// Try to parse handled errors.
cErr, ok := errors.Parse(err)
if ok {
if cErr.Fatal {
status = http.StatusInternalServerError
} else if cErr.IsEqual(ratatxt.AuthErrNoAccess) {
status = http.StatusUnauthorized
}
body = httpMsg{true, cErr.Type.String(), err.Error()}
} else {
// Use generic error message
body = newError(err)
}
respond(w, status, body)
}
func handle404() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
respond(w, http.StatusNotFound, newError(fmt.Errorf("resource not found")))
}
}
func handle405() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
respond(w, http.StatusMethodNotAllowed, newError(fmt.Errorf("method not allowed")))
}
}
type listWithMeta struct {
Data interface{} `json:"data"`
ResultCount int `json:"result_count"`
TotalCount int `json:"total_count"`
}
func newListWithMeta(data interface{}, m *ratatxt.SearchMetadata) listWithMeta {
if reflect.ValueOf(data).IsNil() {
data = []struct{}{}
}
return listWithMeta{data, m.ResultCount, m.TotalCount}
}