-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandler.go
406 lines (358 loc) · 10.7 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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package torque
import (
"context"
"encoding/json"
"errors"
"fmt"
"html/template"
"log"
"net/http"
"net/http/httptest"
"runtime/debug"
"time"
"github.com/gorilla/schema"
)
type handlerImpl[T ViewModel] struct {
ctl Controller
mode Mode
encoder *schema.Encoder
decoder *schema.Decoder
router *router
path string
parent Handler
children []Handler
override http.Handler
subscribers int
eventSource EventSource
handler http.Handler
action Action
loader Loader[T]
headers ResponseHeaders[T]
rendererT Renderer[T]
rendererVM DynamicRenderer
guards []Guard
plugins []Plugin
errorBoundary ErrorBoundary
panicBoundary PanicBoundary
}
func createHandlerImpl[T ViewModel]() *handlerImpl[T] {
h := &handlerImpl[T]{
ctl: nil,
mode: ModeDevelopment,
encoder: schema.NewEncoder(),
decoder: schema.NewDecoder(),
router: nil,
path: "/",
parent: nil,
override: nil,
handler: nil,
action: nil,
loader: nil,
rendererT: nil,
rendererVM: nil,
eventSource: nil,
errorBoundary: nil,
panicBoundary: nil,
guards: []Guard{},
plugins: []Plugin{},
}
h.encoder.SetAliasTag("json")
h.decoder.SetAliasTag("json")
return h
}
// ServeHTTP implements the http.Handler interface
func (h *handlerImpl[T]) ServeHTTP(wr http.ResponseWriter, req *http.Request) {
renderAsOutlet, ok := req.Context().Value(outletContextKey).(bool)
renderAsOutlet = renderAsOutlet && ok
renderAsOutlet = renderAsOutlet && req.Method == http.MethodGet
if h.router != nil && !renderAsOutlet {
log.Printf("[Router] (%s) %s -> %T\n", req.Method, req.URL, h.ctl)
// Indicate to any handlers they should not attempt to handle the request using
// their internal router, and instead just serve the request using the controller
h.router.ServeHTTP(wr, req.WithContext(context.WithValue(req.Context(), outletContextKey, true)))
} else if h.GetParent() != nil && renderAsOutlet {
h.serveOutlet(wr, req)
} else {
h.serveRequest(wr, req)
}
}
func (h *handlerImpl[T]) serveOutlet(wr http.ResponseWriter, req *http.Request) {
var (
childReq = req
childResp = httptest.NewRecorder()
parentReq = req.Clone(req.Context())
parentResp = httptest.NewRecorder()
)
// child before parent, because it can set additional context
// while handling the request
h.serveRequest(childResp, childReq)
if childResp.Code != http.StatusOK {
// child route is indicating a non-200 error code, do not
// render as outlet, maybe it's a redirect
for key := range childResp.Header() {
wr.Header().Set(key, childResp.Header().Get(key))
}
wr.WriteHeader(childResp.Code)
_, err := wr.Write(childResp.Body.Bytes())
if err != nil {
panic(err)
}
return
}
h.GetParent().ServeHTTP(parentResp, parentReq.WithContext(childReq.Context()))
t := template.Must(template.New("outlet").Parse(parentResp.Body.String()))
for key := range childResp.Header() {
wr.Header().Set(key, childResp.Header().Get(key))
}
for key := range parentResp.Header() {
wr.Header().Set(key, parentResp.Header().Get(key))
}
err := t.Execute(wr, template.HTML(childResp.Body.String()))
if err != nil {
panic(err)
}
}
// serveRequest is the core handler logic for torque. It is responsible for handling incoming
// HTTP requests and applying the appropriate API methods from the Controller API.
func (h *handlerImpl[T]) serveRequest(wr http.ResponseWriter, req *http.Request) {
var err error
// attach the decoder to the request context so it can be used
// by handlers in the request stack
*req = *req.WithContext(withDecoder(req.Context(), h.decoder))
// defer a panic recoverer and pass panics to the PanicBoundary
defer func() {
if err, ok := recover().(error); ok && err != nil {
h.handlePanic(wr, req, err)
return
}
}()
log.Printf("[Request] (%s) %s -> %T\n", req.Method, req.URL, h.ctl)
// plugins can be used to set up the request context
err = h.handlePluginSetup(wr, req)
if err != nil {
h.handleError(wr, req, err)
return
}
// guards can prevent a request from going through by
// returning an alternate http.HandlerFunc
for _, guard := range h.guards {
if h := guard(req); h != nil {
log.Printf("[Guard] %s -> handled by %T\n", req.URL, guard)
h(wr, req)
return
}
}
// If this is a wrapped vanilla http.Handler passed from a call to torque.MustNewV,
// it short-circuits a majority of the controller flow. Just serve the request.
if h.handler != nil {
h.handler.ServeHTTP(wr, req)
return
}
switch req.Method {
case http.MethodGet:
if req.Header.Get("Accept") == "text/event-stream" {
err = h.handleEventSource(wr, req)
if err != nil {
h.handleError(wr, req, err)
}
return
}
vm, err := h.handleLoader(wr, req)
if err != nil && !errors.Is(err, errNotImplemented) {
h.handleError(wr, req, err)
return
}
err = h.handleResponseHeaders(wr, req, vm)
if err != nil {
h.handleError(wr, req, err)
return
}
err = h.handleRender(wr, req, vm)
if err != nil {
h.handleError(wr, req, err)
return
}
case http.MethodPut, http.MethodPost, http.MethodPatch, http.MethodDelete:
err = h.handleAction(wr, req)
if err != nil {
h.handleError(wr, req, err)
return
}
default:
http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
return
}
}
func (h *handlerImpl[T]) handleAction(wr http.ResponseWriter, req *http.Request) error {
var start = time.Now()
if h.action != nil {
err := h.action.Action(wr, req)
if err != nil {
log.Printf("[Action] %s -> error: %s\n", req.URL, err.Error())
return err
} else {
log.Printf("[Action] %s -> success (%dms)\n", req.URL, time.Since(start).Milliseconds())
return nil
}
} else {
return fmt.Errorf("failed to handle action: %w", errNotImplemented)
}
}
func (h *handlerImpl[T]) handleError(wr http.ResponseWriter, req *http.Request, err error) {
if ok := h.handleReloadError(wr, req, err); ok {
return
} else if ok = h.handleRedirectError(wr, req, err); ok {
return
} else if ok := h.handleInternalError(wr, req, err); ok {
log.Printf("[Error] %s", err.Error())
return
} else if h.errorBoundary != nil {
// Calls to ErrorBoundary can return an http.HandlerFunc
// that can be used to cleanly handle the error. Or not
h := h.errorBoundary.ErrorBoundary(wr, req, err)
if h != nil {
log.Printf("[ErrorBoundary] %s -> handled\n", req.URL)
h(wr, req)
return
}
} else {
// No ErrorBoundary was implemented in the route Controller.
// So your error goes to the PanicBoundary.
log.Printf("[ErrorBoundary] %s -> not implemented\n", req.URL)
panic(err)
}
}
func (h *handlerImpl[T]) handleEventSource(wr http.ResponseWriter, req *http.Request) error {
if h.eventSource != nil {
h.subscribers++
log.Printf("[EventSource] %s -> new subscriber (%d total)\n", req.URL, h.subscribers)
err := h.eventSource.Subscribe(wr, req)
h.subscribers--
if err != nil {
log.Printf("[EventSource] %s -> closed error: %s\n", req.URL, err.Error())
} else {
log.Printf("[EventSource] %s -> closed ok (%d total)\n", req.URL, h.subscribers)
}
return err
} else {
return fmt.Errorf("failed to handle event source: %w", errNotImplemented)
}
}
func (h *handlerImpl[T]) handleInternalError(wr http.ResponseWriter, req *http.Request, err error) bool {
if errors.Is(err, errNotImplemented) {
http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
return true
}
return false
}
func (h *handlerImpl[T]) handleLoader(_ http.ResponseWriter, req *http.Request) (T, error) {
var (
vm T
err error
start = time.Now()
)
if h.loader != nil {
vm, err = h.loader.Load(req)
if err != nil {
log.Printf("[Loader] %s -> error: %s\n", req.URL, err.Error())
return vm, err
} else {
log.Printf("[Loader] %s -> success (%dms)\n", req.URL, time.Since(start).Milliseconds())
return vm, nil
}
} else {
return vm, fmt.Errorf("failed to handle loader: %w", errNotImplemented)
}
}
func (h *handlerImpl[T]) handlePanic(wr http.ResponseWriter, req *http.Request, err error) {
if h.panicBoundary != nil {
// Calls to PanicBoundary can return an http.HandlerFunc
// that can be used to cleanly handle the error.
h := h.panicBoundary.PanicBoundary(wr, req, err)
if h != nil {
log.Printf("[PanicBoundary] %s -> handled\n", req.URL)
h(wr, req)
return
}
} else {
stack := debug.Stack()
log.Printf("[UncaughtPanic] %s\n-- ERROR --\nUncaught panic in route ctl %T: %+v\n-- STACK TRACE --\n%s", req.URL, h.ctl, err, stack)
err = writeErrorResponse(wr, req, err, stack)
if err != nil {
log.Printf("[UncaughtPanic] %s -> failed to write error response: %v\n", req.URL, err)
}
}
}
func (h *handlerImpl[T]) handleResponseHeaders(wr http.ResponseWriter, req *http.Request, vm T) error {
if h.headers != nil {
err := h.headers.Headers(wr, req, vm)
if err != nil {
log.Printf("[Headers] %s -> error: %s\n", req.URL, err.Error())
return err
} else {
log.Printf("[Headers] %s -> success\n", req.URL)
}
}
return nil
}
func (h *handlerImpl[T]) handleRender(wr http.ResponseWriter, req *http.Request, vm T) error {
// If the requester set the content-type to json, we can just
// render the result of the loader directly
if req.Header.Get("Accept") == "application/json" {
log.Printf("[JSON] %s\n", req.URL)
encoder := json.NewEncoder(wr)
if UseMode(req.Context()) == ModeDevelopment {
encoder.SetIndent("", " ")
}
return encoder.Encode(vm)
}
var (
err error
start = time.Now()
)
if h.rendererT != nil {
err = h.rendererT.Render(wr, req, vm)
} else if h.rendererVM != nil {
err = h.rendererVM.Render(wr, req, vm)
} else {
return errNotImplemented
}
if err != nil {
log.Printf("[Renderer] %s -> error: %s\n", req.URL, err.Error())
return err
} else {
log.Printf("[Renderer] %s -> success (%dms)\n", req.URL, time.Since(start).Milliseconds())
return nil
}
}
func (h *handlerImpl[T]) handleRedirectError(wr http.ResponseWriter, req *http.Request, err error) bool {
if err, ok := err.(*errRedirect); !ok {
return false
} else {
http.Redirect(wr, req, err.url, err.status)
return true
}
}
func (h *handlerImpl[T]) handleReloadError(wr http.ResponseWriter, req *http.Request, err error) bool {
if err, ok := err.(*errReload); !ok {
return false
} else if req.Method == http.MethodGet {
panic(errors.New("ReloadWithError can only be returned from an Action"))
} else if err.err != nil {
req = req.WithContext(withError(req.Context(), err.err))
}
log.Printf("[ReloadWithError] %s -> %s\n", req.URL, err.Error())
req.Method = http.MethodGet
h.serveRequest(wr, req)
return true
}
func (h *handlerImpl[T]) handlePluginSetup(_ http.ResponseWriter, req *http.Request) error {
var err error
for _, plugin := range h.plugins {
err = plugin.Setup(req)
if err != nil {
return err
}
}
return nil
}