-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandler.go
More file actions
82 lines (65 loc) · 2 KB
/
handler.go
File metadata and controls
82 lines (65 loc) · 2 KB
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
package trama
import "net/http"
// Handler is the interface a trama handler must implement.
type Handler interface {
// Get handles HTTP requests with a GET method
Get(Response, *http.Request) error
// Post handles HTTP requests with a POST method
Post(Response, *http.Request) error
// Interceptors defines the interceptor chain to be called along with the
// handler when a request arrives.
Interceptors() InterceptorChain
// Templates returns a TemplateGroupSet to be registered by the framework.
// These templates are parsed at once when calling Mux’s ParseTemplates
// method.
Templates() TemplateGroupSet
}
// NopHandler is a facility for writing handlers. It is meant to be embedded in
// your handler if you don’t need to implement all Handler methods.
type NopHandler struct {
NopInterceptorChain
}
// Get writes no response and returns no error
func (n *NopHandler) Get(Response, *http.Request) error { return nil }
// Post writes no response and returns no error
func (n *NopHandler) Post(Response, *http.Request) error { return nil }
// Templates returns an empty TemplateGroupSet
func (n *NopHandler) Templates() TemplateGroupSet {
return NewTemplateGroupSet(nil)
}
type adapter struct {
handler func() Handler
templates TemplateGroupSet
log func(error)
}
func (a adapter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
response := &response{
responseWriter: w,
request: r,
templates: a.templates,
log: a.log,
}
handler := a.handler()
interceptors := handler.Interceptors()
var err error
for k, interceptor := range interceptors {
err = interceptor.Before(response, r)
if err != nil {
interceptors = interceptors[:k+1]
goto write
}
}
switch r.Method {
case "GET":
err = handler.Get(response, r)
case "POST":
err = handler.Post(response, r)
default:
response.returnStatus = http.StatusNotImplemented
}
write:
for k := len(interceptors) - 1; k >= 0; k-- {
interceptors[k].After(response, r, err)
}
response.write()
}