This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action_handler.go
66 lines (54 loc) · 1.74 KB
/
action_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
package detour
import (
"net/http"
"sync"
)
type actionHandler struct {
controller monadicAction
generateNewInputModel createModel
}
// Install merely allows *actionHandler to implement a non-public/internal, company-specific interface.
// Deprecated
func (this *actionHandler) Install(http.Handler) {}
var buffers = sync.Pool{New: func() interface{} { return newResponseBuffer() }}
func (this *actionHandler) ServeHTTP(response http.ResponseWriter, request *http.Request) {
model := this.generateNewInputModel()
status, err := prepareInputModel(model, request)
result := this.determineResult(model, status, err)
buffer := buffers.Get().(*responseBuffer)
result.Render(buffer, request)
buffer.flush(response)
buffers.Put(buffer)
}
func (this *actionHandler) determineResult(model interface{}, status int, err error) Renderer {
if err != nil {
return inputModelErrorResult(status, err)
} else {
return this.controllerActionResult(model)
}
}
func inputModelErrorResult(code int, err error) Renderer {
_, isErrors := err.(Errors)
if isErrors {
return &JSONResult{StatusCode: code, Content: err}
}
_, isDiagnosticErr := err.(*DiagnosticError)
if isDiagnosticErr {
return &DiagnosticResult{StatusCode: code, Message: err.Error()}
}
_, isDiagnosticErrors := err.(DiagnosticErrors)
if isDiagnosticErrors {
return &DiagnosticResult{StatusCode: code, Message: http.StatusText(code) + "\n\n" + err.Error()}
}
if errRenderer, ok := err.(Renderer); ok {
return errRenderer
}
return &StatusCodeResult{StatusCode: code, Message: err.Error()}
}
func (this *actionHandler) controllerActionResult(model interface{}) Renderer {
if result := this.controller(model); result != nil {
return result
} else {
return NopRenderer{}
}
}