-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
72 lines (55 loc) · 1.49 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
package akumu
import (
"net/http"
)
type Handler func(*http.Request) error
func handleResponder(writer http.ResponseWriter, request *http.Request, parent *Builder, responder Responder) {
if parent != nil {
parent.
Merge(responder.Respond(request)).
Handle(writer, request)
return
}
responder.
Respond(request).
Handle(writer, request)
}
func handleNoError(writer http.ResponseWriter, request *http.Request, parent *Builder) {
if parent != nil {
parent.Handle(writer, request)
return
}
Response(http.StatusOK).Handle(writer, request)
}
func handle(writer http.ResponseWriter, request *http.Request, err error, parent *Builder) {
if err == nil {
handleNoError(writer, request, parent)
return
}
if responder, ok := err.(Responder); ok {
handleResponder(writer, request, parent, responder)
return
}
if parent != nil {
builder := NewProblem(err, parent.status).
Respond(request)
parent.
Merge(builder).
Handle(writer, request)
return
}
NewProblem(err, http.StatusInternalServerError).
Respond(request).
Handle(writer, request)
}
func (handler Handler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
handle(writer, request, handler(request), nil)
}
func (handler Handler) HandlerFunc() http.HandlerFunc {
return http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
handler.ServeHTTP(writer, request)
})
}
func HandlerFunc(handler func(*http.Request) error) http.HandlerFunc {
return Handler(handler).HandlerFunc()
}