/
handler.go
174 lines (130 loc) · 4.45 KB
/
handler.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
package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
exec "github.com/ethpandaops/stubbies/pkg/execution"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
)
// Handler is an API handler that is responsible for negotiating with a HTTP api.
type Handler struct {
log logrus.FieldLogger
execution *exec.Handler
metrics Metrics
}
func NewHandler(log logrus.FieldLogger, conf *exec.Config) *Handler {
return &Handler{
log: log.WithField("module", "api"),
execution: exec.NewHandler(log, conf),
metrics: NewMetrics("http"),
}
}
func (h *Handler) Register(ctx context.Context, router *httprouter.Router) error {
router.POST("/", h.wrappedHandler(h.handleExecution))
return nil
}
func (h *Handler) Start(ctx context.Context) {
h.execution.Start(ctx)
}
func deriveRegisteredPath(request *http.Request, ps httprouter.Params) string {
registeredPath := request.URL.Path
for _, param := range ps {
registeredPath = strings.Replace(registeredPath, param.Value, fmt.Sprintf(":%s", param.Key), 1)
}
return registeredPath
}
func (h *Handler) wrappedHandler(handler func(ctx context.Context, r *http.Request, p httprouter.Params, contentType ContentType, body *JSONRequestBody) (*HTTPResponse, error)) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
start := time.Now()
contentType := NewContentTypeFromRequest(r)
ctx := r.Context()
registeredPath := deriveRegisteredPath(r, p)
h.log.WithFields(logrus.Fields{
"method": r.Method,
"path": r.URL.Path,
"content_type": contentType,
"accept": r.Header.Get("Accept"),
}).Debug("handling http request")
executionMethod := "unknown"
h.metrics.ObserveRequest(r.Method, registeredPath, executionMethod)
responseStatusCode := http.StatusInternalServerError
var err error
defer func() {
h.metrics.ObserveResponse(r.Method, registeredPath, fmt.Sprintf("%v", responseStatusCode), contentType.String(), executionMethod, time.Since(start))
}()
decoder := json.NewDecoder(r.Body)
var body JSONRequestBody
err = decoder.Decode(&body)
if err != nil {
responseStatusCode = http.StatusBadRequest
if writeErr := WriteErrorResponse(w, err.Error(), responseStatusCode); writeErr != nil {
h.log.WithError(writeErr).Error("Failed to decode request body")
}
return
}
if body.Method == "" {
responseStatusCode = http.StatusBadRequest
if writeErr := WriteErrorResponse(w, "Missing method", responseStatusCode); writeErr != nil {
h.log.WithError(writeErr).Error("Request body missing method")
}
return
}
executionMethod = body.Method
response, err := handler(ctx, r, p, contentType, &body)
if err != nil {
if response != nil && response.StatusCode != 0 {
responseStatusCode = response.StatusCode
}
if writeErr := WriteErrorResponse(w, err.Error(), responseStatusCode); writeErr != nil {
h.log.WithError(writeErr).Error("Failed to write error response")
}
return
}
responseStatusCode = response.StatusCode
data, err := response.MarshalAs(contentType)
if err != nil {
responseStatusCode = http.StatusInternalServerError
if writeErr := WriteErrorResponse(w, err.Error(), responseStatusCode); writeErr != nil {
h.log.WithError(writeErr).Error("Failed to write error response")
}
return
}
for header, value := range response.Headers {
w.Header().Set(header, value)
}
if err := WriteContentAwareResponse(w, data, contentType); err != nil {
h.log.WithError(err).Error("Failed to write response")
}
}
}
func (h *Handler) handleExecution(ctx context.Context, r *http.Request, p httprouter.Params, contentType ContentType, body *JSONRequestBody) (*HTTPResponse, error) {
if err := ValidateContentType(contentType, []ContentType{ContentTypeJSON}); err != nil {
return NewUnsupportedMediaTypeResponse(nil), err
}
var parms []string
for _, param := range body.Params {
if param != nil {
parms = append(parms, string(*param))
}
}
h.log.WithFields(logrus.Fields{
"method": body.Method,
"params": parms,
"id": body.ID,
}).Debug("handling execution request")
resp, err := h.execution.Request(ctx, body.ID, body.Method, body.Params)
if err != nil {
return NewInternalServerErrorResponse(nil), err
}
var rsp = NewSuccessResponse(ContentTypeResolvers{
ContentTypeJSON: func() ([]byte, error) {
return json.Marshal(resp)
},
})
rsp.SetCacheControl("public, s-max-age=30")
return rsp, nil
}