-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
49 lines (39 loc) · 1.13 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
package controller
import (
"net/http"
"github.com/blackstorm/ingress-go/pkg/controller/matcher"
)
type handler func(resp http.ResponseWriter, req *http.Request)
type serverHandler struct {
matcher *matcher.RequestMatcher
beforeHandlers []handler
afterHandlers []handler
}
func newServerHandler(matcher *matcher.RequestMatcher) *serverHandler {
return &serverHandler{
matcher: matcher,
beforeHandlers: make([]handler, 0),
afterHandlers: make([]handler, 0),
}
}
func (s *serverHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
s.doBefores(resp, req)
resp.Write([]byte("todo"))
s.doAfters(resp, req)
}
func (s *serverHandler) doBefores(resp http.ResponseWriter, req *http.Request) {
for _, hr := range s.beforeHandlers {
hr(resp, req)
}
}
func (s *serverHandler) doAfters(resp http.ResponseWriter, req *http.Request) {
for _, hr := range s.afterHandlers {
hr(resp, req)
}
}
func (s *serverHandler) addBeforeHandleRequest(hr handler) {
s.beforeHandlers = append(s.beforeHandlers, hr)
}
func (s *serverHandler) addAfterHandleRequest(hr handler) {
s.afterHandlers = append(s.afterHandlers, hr)
}