-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpeasy.go
More file actions
356 lines (304 loc) · 9.61 KB
/
httpeasy.go
File metadata and controls
356 lines (304 loc) · 9.61 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package httpeasy
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/gorilla/mux"
)
// Request represents a simplified HTTP request
type Request struct {
// Vars are the variables parsed out of the URL path.
Vars map[string]string
// Body is the contents of the HTTP request body.
Body io.Reader
// Headers are the HTTP headers
Headers http.Header
// URL contains the parsed URL information. See net/http.Request.URL for
// more information.
URL *url.URL
}
// Text consumes the request body and returns it as a string.
func (r Request) Text() (string, error) {
data, err := r.Bytes()
return string(data), err
}
// Bytes consumes the request body and returns it as a byte slice.
func (r Request) Bytes() ([]byte, error) {
return ioutil.ReadAll(r.Body)
}
// Cookie returns the named cookie if it exists, otherwise http.ErrNoCookie.
func (r Request) Cookie(name string) (*http.Cookie, error) {
for _, c := range readCookies(r.Headers, name) {
if c.Name == name {
return c, nil
}
}
return nil, http.ErrNoCookie
}
// Cookies returns the cookies attached to the request.
func (r Request) Cookies() []*http.Cookie {
return readCookies(r.Headers, "")
}
// InvalidJSONErr wraps an error encountered while trying to unmarshal JSON.
type InvalidJSONErr struct {
Err error
}
// Error implements the error interface for InvalidJSONErr
func (err InvalidJSONErr) Error() string {
return fmt.Sprintf("Invalid JSON: %v", err.Err)
}
// JSON deserializes the request body into `v`. `v` must be a pointer; all the
// standard `encoding/json.Unmarshal()` rules apply. If an error is encountered
// while unmarshaling, `InvalidJSONErr` is returned to distinguish it from
// errors encountered while reading the request body.
//
// var person struct {
// Name string `json:"name"`
// Age int `json:"age"`
// }
// if err := r.JSON(&person); err != nil {
// return err
// }
// fmt.Printf("Name='%s'; Age=%d", person.Name, person.Age)
//
func (r Request) JSON(v interface{}) error {
data, err := r.Bytes()
if err != nil {
return err
}
if err := json.Unmarshal(data, v); err != nil {
return InvalidJSONErr{err}
}
return nil
}
// Response represents a simplified HTTP response
type Response struct {
// Status is the HTTP status code to write
Status int
// Data is the data to be written to the client
Data Serializer
// Logging is the information to pass to the logger
Logging []interface{}
// Headers is the HTTP headers for the response
Headers http.Header
// Cookies are the list of cookies to be set on the response
Cookies []*http.Cookie
}
// WithHeaders returns a copy of the response with the specified headers
// attached. The headers provided by this method will be merged with any
// existing headers on the response.
func (r Response) WithHeaders(headers http.Header) Response {
if r.Headers == nil {
r.Headers = http.Header{}
}
for key, values := range headers {
r.Headers[key] = append(r.Headers[key], values...)
}
return r
}
// WithCookies returns a copy of the response with the specified cookies
// attached. The cookies provided by this method will be appended onto any
// existing cookies on the response.
func (r Response) WithCookies(cookies ...*http.Cookie) Response {
r.Cookies = append(r.Cookies, cookies...)
return r
}
// WithLogging returns a copy of the response with the specified logging
// attached. The logging provided by this method will be appended onto any
// existing cookies on the response.
func (r Response) WithLogging(logging ...interface{}) Response {
r.Logging = append(r.Logging, logging...)
return r
}
// requestLog represents a standard HTTP request log
type requestLog struct {
// Started holds the start time for the request
Started time.Time `json:"started"`
// Duration holds the duration to service the request
Duration time.Duration `json:"duration"`
// Method holds the HTTP method for the request
Method string `json:"method"`
// URL holds the URL for the request
URL url.URL `json:"url"`
// RequestHeaders holds the headers for the request
RequestHeaders http.Header `json:"requestHeaders"`
// ResponseHeaders holds the headers for the request
ResponseHeaders http.Header `json:"responseHeaders"`
// Status holds the HTTP status code returned by the request handler
Status int `json:"status"`
// Message holds the logging message returned by the request handler
Message []interface{} `json:"message"`
// WriteError holds any errors that were encountered during writing the
// response to the output socket.
WriteError interface{} `json:"writeError"`
}
// JSONLog returns a `LogFunc` which logs JSON to `w`.
func JSONLog(w io.Writer) LogFunc {
return func(v interface{}) {
data, err := json.Marshal(v)
if err != nil {
data, err = json.Marshal(struct {
Context string `json:"context"`
DebugData string `json:"debugData"`
Error string `json:"error"`
}{
Context: "Error marshaling 'debugData' into JSON",
DebugData: spew.Sdump(v),
Error: err.Error(),
})
if err != nil {
// We really REALLY should never get here
log.Println("ERROR MARSHALLING THE MARSHALLING ERROR!:", err)
return
}
}
if _, err := fmt.Fprintf(w, "%s\n", data); err != nil {
log.Println("ERROR WRITING TO LOGGER:", err)
}
}
}
// Handler handles HTTP requests
type Handler func(r Request) Response
// LogFunc logs its argument
type LogFunc func(v interface{})
// HTTP converts an httpeasy.Handler into an http.HandlerFunc. The returned
// function will collect a bunch of standard HTTP information and pass it to
// the provided log function.
func (h Handler) HTTP(log LogFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
defer r.Body.Close()
var rsp Response
contentLength := r.Header.Get("Content-Length")
i, err := strconv.ParseInt(contentLength, 10, 64)
if err != nil {
i = 0 // just being explicit
rsp.Logging = append(
rsp.Logging,
struct {
Context string `json:"context"`
Error string `json:"error"`
}{
Context: "Invalid or missing `Content-Length` header; " +
"defaulting to Content-Length=0",
Error: err.Error(),
},
)
}
rsp = h(Request{
Vars: mux.Vars(r),
Body: io.LimitReader(r.Body, i),
Headers: r.Header,
URL: r.URL,
})
writerTo, err := rsp.Data()
if err != nil {
rsp.Status = http.StatusInternalServerError
writerTo = strings.NewReader("500 Internal Server Error")
rsp.Logging = []interface{}{
struct {
Context string `json:"context"`
Error string `json:"error"`
OriginalLogging []interface{} `json:"originalLogging"`
}{
Context: "Error serializing response data",
Error: err.Error(),
OriginalLogging: rsp.Logging,
},
}
}
// Copy HTTP headers from the response object to the response writer.
// This has to go before the WriteHeader invocation or it won't take
// effect (quirk of net/http.ResponseWriter).
header := w.Header()
for key, values := range rsp.Headers {
for _, value := range values {
header.Add(key, value)
}
}
for _, cookie := range rsp.Cookies {
http.SetCookie(w, cookie)
}
w.WriteHeader(rsp.Status)
_, err = writerTo.WriteTo(w)
log(requestLog{
Started: start,
Duration: time.Since(start),
Method: r.Method,
URL: *r.URL,
RequestHeaders: r.Header,
ResponseHeaders: w.Header(),
Status: rsp.Status,
Message: rsp.Logging,
WriteError: err,
})
}
}
// Route holds the complete routing information
type Route struct {
// Method is the HTTP method for the route
Method string
// Path is the path to the handler. See github.com/gorilla/mux.Route.Path
// for additional details.
Path string
// Handler is the function which handles the request
Handler Handler
}
// StdlibRoute holds the complete routing information. It is the same as a
// `Route` except that the handler type is an `http.HandlerFunc` instead of a
// `Handler`.
type StdlibRoute struct {
// Method is the HTTP method for the route
Method string
// Path is the path to the handler. See github.com/gorilla/mux.Route.Path
// for additional details.
Path string
// Handler is the function which handles the request
Handler http.HandlerFunc
}
// Router is an HTTP mux for httpeasy.
type Router struct {
inner *mux.Router
}
// NewRouter constructs a new router.
func NewRouter() *Router { return &Router{mux.NewRouter()} }
// ServeHTTP implements the http.Handler interface for Router.
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.inner.ServeHTTP(w, req)
}
// Register registers routes with the provided Router and LogFunc and returns
// the same modified Router.
func (r *Router) Register(log LogFunc, routes ...Route) *Router {
for _, route := range routes {
r.inner.Path(route.Path).
Methods(route.Method).
HandlerFunc(route.Handler.HTTP(log))
}
return r
}
// RegisterStdlib registers `StdlibRoute`s with the provided Router and returns
// the same modified Router.
func (r *Router) RegisterStdlib(routes ...StdlibRoute) *Router {
for _, route := range routes {
r.inner.Path(route.Path).
Methods(route.Method).
HandlerFunc(route.Handler)
}
return r
}
// Register creates a new router and uses it to register all of the provided
// routes before returning it. It's purely a convenience wrapper around
//
// r := NewRouter()
// r.Register(log, routes...)
func Register(log LogFunc, routes ...Route) *Router {
return NewRouter().Register(log, routes...)
}