-
Notifications
You must be signed in to change notification settings - Fork 21
/
WRPHandler.go
146 lines (121 loc) · 3.83 KB
/
WRPHandler.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
// SPDX-FileCopyrightText: 2017 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"net/http"
"go.uber.org/zap"
gokithttp "github.com/go-kit/kit/transport/http"
"github.com/xmidt-org/webpa-common/v2/adapter"
"github.com/xmidt-org/webpa-common/v2/device"
// nolint:staticcheck
"github.com/xmidt-org/webpa-common/v2/xhttp"
"github.com/xmidt-org/wrp-go/v3/wrphttp"
)
func withDeviceAccessCheck(errorLogger *zap.Logger, wrpRouterHandler wrphttp.HandlerFunc, d deviceAccess) wrphttp.HandlerFunc {
encodeError := talariaWRPErrorEncoder(errorLogger)
return func(w wrphttp.ResponseWriter, r *wrphttp.Request) {
err := d.authorizeWRP(r.Context(), &r.Entity.Message)
if err != nil {
encodeError(r.Context(), err, w)
return
}
wrpRouterHandler(w, r)
}
}
func wrpRouterHandler(logger *zap.Logger, router device.Router, ctxlogger func(ctx context.Context) *zap.Logger) wrphttp.HandlerFunc {
if logger == nil {
log := adapter.DefaultLogger()
logger = log.Logger
}
if router == nil {
panic("router is a required component")
}
return func(w wrphttp.ResponseWriter, r *wrphttp.Request) {
deviceRequest := &device.Request{
Message: &r.Entity.Message,
Format: r.Entity.Format,
Contents: r.Entity.Bytes,
}
var errorLogger *zap.Logger
if ctxlogger != nil && ctxlogger(r.Context()) != nil {
errorLogger = ctxlogger(r.Context())
} else {
errorLogger = logger
}
// deviceRequest carries the context through the routing infrastructure
deviceResponse, err := router.Route(deviceRequest.WithContext(r.Context()))
if err != nil {
code := http.StatusGatewayTimeout
// nolint:errorlint
switch err {
case device.ErrorInvalidDeviceName:
code = http.StatusBadRequest
case device.ErrorDeviceNotFound:
code = http.StatusNotFound
case device.ErrorNonUniqueID:
code = http.StatusBadRequest
case device.ErrorInvalidTransactionKey:
code = http.StatusBadRequest
case device.ErrorTransactionAlreadyRegistered:
code = http.StatusBadRequest
}
errorLogger.Error("Could not process device request", zap.Error(err), zap.Int("code", code))
w.Header().Set("X-Xmidt-Message-Error", err.Error())
xhttp.WriteErrorf(
w,
code,
"Could not process device request: %s",
err,
)
return
}
// if deviceReponse == nil, that just means the request was not something that represented
// the start of a transaction. For example, events do not carry a transaction key because
// they do not expect responses.
if deviceResponse == nil {
return
}
if len(deviceResponse.Contents) < 1 {
_, err = xhttp.WriteError(
w,
http.StatusInternalServerError,
"Transaction response had no content")
errorLogger.Error("Transaction response was empty", zap.Error(err))
return
}
_, err = w.WriteWRP(&wrphttp.Entity{
Bytes: deviceResponse.Contents,
Message: *deviceResponse.Message,
Format: deviceResponse.Format,
})
if err != nil {
errorLogger.Error("Error while writing transaction response", zap.Error(err))
}
}
}
func decorateRequestDecoder(decode wrphttp.Decoder) wrphttp.Decoder {
return func(c context.Context, r *http.Request) (*wrphttp.Entity, error) {
entity, err := decode(c, r)
if err != nil {
return nil, &xhttp.Error{
Code: http.StatusBadRequest,
Text: "Unable to decode request: " + err.Error(),
}
}
return entity, err
}
}
func talariaWRPErrorEncoder(errorLogger *zap.Logger) gokithttp.ErrorEncoder {
return func(_ context.Context, err error, w http.ResponseWriter) {
code := http.StatusInternalServerError
// nolint errorlint
if sc, ok := err.(gokithttp.StatusCoder); ok {
code = sc.StatusCode()
}
if code == http.StatusInternalServerError {
errorLogger.Error("Possibly false internal server error", zap.Error(err))
}
w.WriteHeader(code)
}
}