This repository has been archived by the owner on Mar 30, 2022. It is now read-only.
/
transport.go
153 lines (130 loc) · 4.42 KB
/
transport.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
package http
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/go-kit/kit/log"
kithttp "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
"github.com/xeipuuv/gojsonschema"
"github.com/lifesum/configsum/pkg/errors"
"github.com/lifesum/configsum/pkg/instrument"
)
type contextKey string
const (
ctxKeyTimeBegin contextKey = "begin"
ctxKeyRoute contextKey = "route"
)
// Headers.
const (
headerContentType = "Content-Type"
)
// DecodeJSONSchema validates the request payload against the given schema and
// returns an invalid payload error in case the validation fails.
func DecodeJSONSchema(
next kithttp.DecodeRequestFunc,
schema *gojsonschema.Schema,
) kithttp.DecodeRequestFunc {
return func(ctx context.Context, r *http.Request) (interface{}, error) {
// Attempts to work with gojsonschema.NewReaderLoader turned out to
// lead to inexplicable io errors. While less elegant it works for
// now. Be aware of the re-assignment of the request body so
// subsequent decode functions can function properly.
raw, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
if len(raw) == 0 {
return nil, errors.Wrap(errors.ErrInvalidPayload, "empty body")
}
res, err := schema.Validate(gojsonschema.NewBytesLoader(raw))
if nil != err {
return nil, errors.Wrap(errors.ErrInvalidPayload, err.Error())
}
if !res.Valid() {
err := errors.ErrInvalidPayload
for _, e := range res.Errors() {
err = errors.Wrap(err, e.String())
}
return nil, err
}
r.Body = ioutil.NopCloser(bytes.NewBuffer(raw))
return next(ctx, r)
}
}
// ErrorEncoder translates domain specific errors to HTTP status codes.
func ErrorEncoder(_ context.Context, err error, w http.ResponseWriter) {
switch errors.Cause(err) {
case errors.ErrExists:
w.WriteHeader(http.StatusConflict)
case errors.ErrNotFound:
w.WriteHeader(http.StatusNotFound)
case errors.ErrClientNotFound, errors.ErrSecretMissing:
w.WriteHeader(http.StatusUnauthorized)
case errors.ErrSignatureMissing, errors.ErrSignatureMissmatch, errors.ErrUserIDMissing:
w.WriteHeader(http.StatusUnauthorized)
case errors.ErrInvalidPayload, errors.ErrParametersInvalid:
w.WriteHeader(http.StatusBadRequest)
default:
w.WriteHeader(http.StatusInternalServerError)
}
w.Header().Set(headerContentType, "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(struct {
Reason string `json:"reason"`
}{
Reason: err.Error(),
})
}
// PopulateRequestContext extracts common information about a request and stores
// it in the context.
func PopulateRequestContext(ctx context.Context, r *http.Request) context.Context {
route := "unknown"
if current := mux.CurrentRoute(r); current != nil {
route = current.GetName()
}
ctx = context.WithValue(ctx, ctxKeyTimeBegin, time.Now())
ctx = context.WithValue(ctx, ctxKeyRoute, route)
return ctx
}
// ServerFinalizer instruments handler calls to expose Prometheus metrics and
/// log request/response information.
func ServerFinalizer(
logger log.Logger,
reqObserve instrument.ObserveRequestFunc,
) kithttp.ServerFinalizerFunc {
return func(ctx context.Context, code int, r *http.Request) {
var (
begin = ctx.Value(ctxKeyTimeBegin).(time.Time)
method = ctx.Value(kithttp.ContextKeyRequestMethod).(string)
host = ctx.Value(kithttp.ContextKeyRequestHost).(string)
proto = ctx.Value(kithttp.ContextKeyRequestProto).(string)
route = ctx.Value(ctxKeyRoute).(string)
)
_ = logger.Log(
"duration", time.Since(begin).Nanoseconds(),
"request", map[string]interface{}{
"authorization": ctx.Value(kithttp.ContextKeyRequestAuthorization),
"header": r.Header,
"host": host,
"method": method,
"path": ctx.Value(kithttp.ContextKeyRequestPath),
"proto": proto,
"referer": ctx.Value(kithttp.ContextKeyRequestReferer),
"remoteAddr": ctx.Value(kithttp.ContextKeyRequestRemoteAddr),
"requestId": ctx.Value(kithttp.ContextKeyRequestXRequestID),
"requestUri": ctx.Value(kithttp.ContextKeyRequestURI),
"transferEncoding": r.TransferEncoding,
},
"response", map[string]interface{}{
"header": ctx.Value(kithttp.ContextKeyResponseHeaders).(http.Header),
"size": ctx.Value(kithttp.ContextKeyResponseSize).(int64),
"statusCode": code,
},
"route", route,
)
reqObserve(code, host, method, proto, route, begin)
}
}