-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
260 lines (231 loc) · 6.14 KB
/
rest.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package rest
import (
"bytes"
syserrs "errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"runtime/debug"
"time"
"github.com/bww/go-rest/v1/errors"
"github.com/bww/go-metrics/v1"
"github.com/bww/go-router/v1"
"github.com/bww/go-util/v1/text"
"github.com/sirupsen/logrus"
)
type Service struct {
router.Router
pline *Pipeline
log *logrus.Logger
verbose bool
debug bool
metrics *metrics.Metrics
requestSampler metrics.SamplerVec
}
func New(opts ...Option) (*Service, error) {
s := &Service{
Router: router.New(),
}
var err error
for _, o := range opts {
s, err = o(s)
if err != nil {
return nil, err
}
}
if s.log == nil {
s.log = logrus.StandardLogger()
}
if s.metrics != nil {
s.requestSampler = s.metrics.RegisterSamplerVec("rest_request", "Request sampler", []string{"status"})
}
return s, nil
}
func (s *Service) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var rsp *router.Response
var err error
start := time.Now()
defer func() {
if err := recover(); err != nil {
s.log.WithFields(logrus.Fields{"because": err}).Errorf("PANIC: %s\n", resource((*router.Request)(req)))
fmt.Println(string(debug.Stack()))
return
}
if rsp != nil {
if s.requestSampler != nil {
s.requestSampler.With(metrics.Tags{"status": fmt.Sprint(rsp.Status)}).Observe(float64(time.Since(start)))
}
}
}()
var dump *bytes.Buffer
if s.debug {
dump = &bytes.Buffer{}
dump.WriteString(text.Indent(resource((*router.Request)(req)), " > "))
dump.WriteString("\n")
data := &bytes.Buffer{}
req.Header.Write(data)
dump.WriteString(text.Indent(string(data.Bytes()), " > "))
dump.WriteString("\n")
mtype := req.Header.Get("Content-Type")
if !isMimetypeBinary(mtype) {
data := &bytes.Buffer{}
_, err := io.Copy(data, req.Body)
if err != nil {
s.log.WithFields(logrus.Fields{"because": err}).Error("Could not read request entity")
} else {
dump.WriteString(text.Indent(string(data.Bytes()), " > "))
dump.WriteString("\n")
}
req.Body = ioutil.NopCloser(data)
} else if mtype != "" {
dump.WriteString(text.Indent("[binary data]", " > "))
dump.WriteString("\n")
}
}
var (
rrq *router.Request
cxt router.Context
hdl Handler
)
route, match, err := s.Router.Find((*router.Request)(req))
if err != nil {
s.log.WithFields(logrus.Fields{"because": err}).Error("Could not lookup route")
rrq = (*router.Request)(req)
hdl = HandlerFunc(s.handle500)
} else if route == nil {
s.log.Errorf("Not found: %s", resource((*router.Request)(req)))
rrq = (*router.Request)(req)
hdl = HandlerFunc(s.handle404)
} else {
rrq = (*router.Request)((*http.Request)(req).WithContext(router.NewMatchContext(req.Context(), match)))
cxt = route.Context(match)
hdl = s.handler(route)
}
if s.pline != nil {
rsp, err = s.pline.With(hdl).Handle(rrq, cxt)
} else {
rsp, err = hdl.Handle(rrq, cxt, nil)
}
if err != nil {
s.log.WithFields(logrus.Fields{"because": err}).Error("Handler failed")
return
}
if rsp == nil {
w.WriteHeader(http.StatusOK) // no response is an empty 200
return
}
h := w.Header()
for k, v := range rsp.Header {
h[k] = v
}
w.WriteHeader(rsp.Status)
if s.debug {
dump.WriteString(fmt.Sprintf(" *\n < %d / %s\n", rsp.Status, http.StatusText(rsp.Status)))
}
if entity := rsp.Entity; entity != nil {
defer entity.Close()
if dump != nil {
data := &bytes.Buffer{}
rsp.Header.Write(data)
dump.WriteString(text.Indent(string(data.Bytes()), " < "))
dump.WriteString("\n")
mtype := rsp.Header.Get("Content-Type")
if !isMimetypeBinary(mtype) {
data := &bytes.Buffer{}
_, err := io.Copy(data, entity)
if err != nil {
s.log.WithFields(logrus.Fields{"because": err}).Error("Could not read response entity")
} else {
dump.WriteString(text.Indent(string(data.Bytes()), " < "))
dump.WriteString("\n")
}
entity = ioutil.NopCloser(data)
} else if mtype != "" {
dump.WriteString(text.Indent("[binary data]", " < "))
dump.WriteString("\n")
}
_, err := io.Copy(os.Stdout, dump)
if err != nil {
s.log.WithFields(logrus.Fields{"because": err}).Errorf("Could not dump request")
}
}
_, err := io.Copy(w, entity)
if err != nil {
s.log.WithFields(logrus.Fields{"because": err}).Errorf("Could not write response entity")
}
}
}
func (s *Service) handler(route *router.Route) Handler {
return HandlerFunc(func(req *router.Request, cxt router.Context, next *Pipeline) (*router.Response, error) {
if s.verbose {
s.log.Info(resource(req))
}
if next.Len() > 0 {
return next.Handle(req, cxt)
}
rsp, err := route.Handle(req, cxt)
if err == nil { // short circuit on success; error handling follows
return rsp, nil
}
elog := errlog(s.log, err)
elog.Errorf("%s: %v", resource(req), err)
var rsperr errors.Responder
if syserrs.As(err, &rsperr) {
return rsperr.Response(), nil
} else {
return rsp, err
}
})
}
var (
err404 = errors.Errorf(http.StatusNotFound, "Not found")
err500 = errors.Errorf(http.StatusInternalServerError, "Internal server error")
)
func (s *Service) handle404(req *router.Request, cxt router.Context, next *Pipeline) (*router.Response, error) {
return err404.Response(), nil
}
func (s *Service) handle500(req *router.Request, cxt router.Context, next *Pipeline) (*router.Response, error) {
return err500.Response(), nil
}
// Format a resource name
func resource(req *router.Request) string {
r := fmt.Sprintf("%s %s", req.Method, req.URL.Path)
var p string
for k, v := range req.URL.Query() {
for _, e := range v {
if len(p) > 0 {
p += "&"
}
p += url.QueryEscape(k)
if len(e) > 0 {
p += fmt.Sprintf("=%s", url.QueryEscape(e))
}
}
}
if len(p) > 0 {
r += fmt.Sprintf("?%s", p)
}
return r
}
// Produce a logger for an error
func errlog(log *logrus.Logger, err error) *logrus.Entry {
fields := make(logrus.Fields)
for n := 0; err != nil; n++ {
var resterr *errors.Error
if !syserrs.As(err, &resterr) {
break
}
err = resterr.Unwrap()
if err != nil {
name := "because"
if n > 0 {
name = name + fmt.Sprintf(" #%d", n+1)
}
fields[name] = err.Error()
}
}
return log.WithFields(fields)
}