-
Notifications
You must be signed in to change notification settings - Fork 2
/
input_body.go
95 lines (77 loc) · 2.86 KB
/
input_body.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
// Copyright 2017 Corey Scott http://www.sage42.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package middleware
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"reflect"
)
// key used to store the DTO in the context
type inputBodyKey int
var inputBodyDTO inputBodyKey
// InputBody will attempt to populate a copy of the supplied struct and store it in request context.
func InputBody(handler http.Handler, dto interface{}, client ...InputBodyLogger) http.HandlerFunc {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
dtoType := reflect.TypeOf(dto)
dtoCopy := reflect.New(dtoType.Elem()).Interface()
contents, err := ioutil.ReadAll(req.Body)
if err != nil {
logWarn(req, client, "error reading request body. err: %s", err)
http.Error(resp, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
logBody(req, client, contents)
err = json.Unmarshal(contents, dtoCopy)
if err != nil {
logWarn(req, client, "error during JSON decode of request body. err: %s", err)
http.Error(resp, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
// store the DTO into the context
req = InputBodySetDTO(req, dtoCopy)
handler.ServeHTTP(resp, req)
})
}
// InputBodyDTO returns the populated DTO for this request (or nil).
// NOTE: this method should be used in conjunction with `InputBody()`
func InputBodyDTO(req *http.Request) interface{} {
return req.Context().Value(inputBodyDTO)
}
// InputBodySetDTO sets the supplied DTO into the context.
// This method will generally be used only during testing.
func InputBodySetDTO(req *http.Request, dto interface{}) *http.Request {
return req.WithContext(context.WithValue(req.Context(), inputBodyDTO, dto))
}
// convenience method for logging when the logging client was supplied (via optional/variadic arg)
func logWarn(req *http.Request, client []InputBodyLogger, msg string, args ...interface{}) {
if len(client) == 0 {
return
}
client[0].BadRequest(req, msg, args...)
}
func logBody(req *http.Request, client []InputBodyLogger, body []byte) {
if len(client) == 0 {
return
}
client[0].Request(req, body)
}
// InputBodyLogger allows for logging
type InputBodyLogger interface {
// Log bad requests
BadRequest(req *http.Request, msg string, args ...interface{})
// Log request bodies
Request(req *http.Request, body []byte)
}