-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhandler.go
98 lines (83 loc) · 3.03 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
package there
import (
"net/http"
"path"
)
func (router *Router) ServeHTTP(rw http.ResponseWriter, request *http.Request) {
_, pattern := router.serveMux.Handler(request)
if len(pattern) == 0 { // no handler was found
// not found with global middlewares applied
wrappedNotFoundHandler := router.applyGlobalMiddlewares(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
router.Configuration.RouteNotFoundHandler(NewHttpRequest(rw, req)).ServeHTTP(rw, req)
}))
wrappedNotFoundHandler.ServeHTTP(rw, request)
} else {
router.serveMux.ServeHTTP(rw, request)
}
}
// muxHandler defines a struct that encapsulates a handler and its middleware.
type (
muxHandler struct {
router *Router
methods map[method]*muxHandlerEndpoint
}
muxHandlerEndpoint struct {
endpoint Endpoint
middlewares []Middleware
}
)
// newMuxHandler initializes and returns a new muxHandler.
func newMuxHandler(router *Router, endpoint Endpoint) *muxHandler {
return &muxHandler{
router: router,
methods: map[method]*muxHandlerEndpoint{},
}
}
// AddMiddleware adds a new middleware to the handler's stack.
func (h *muxHandlerEndpoint) AddMiddleware(m Middleware) {
h.middlewares = append(h.middlewares, m)
}
// ServeHTTP implements the http.Handler interface for muxHandler.
func (h *muxHandler) ServeHTTP(rw http.ResponseWriter, request *http.Request) {
httpRequest := NewHttpRequest(rw, request)
method := methodToInt(request.Method)
sanitizedPath := request.URL.Path
if h.router.Configuration.SanitizePaths {
sanitizedPath = path.Clean(sanitizedPath)
}
muxHandlerEndpoint, ok := h.methods[method]
if !ok {
// not found with global middlewares applied
h.router.applyGlobalMiddlewares(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
h.router.Configuration.RouteNotFoundHandler(httpRequest).ServeHTTP(rw, req)
})).ServeHTTP(rw, request)
return
}
endpoint, middlewares := muxHandlerEndpoint.endpoint, muxHandlerEndpoint.middlewares
var next Response = ResponseFunc(func(rw http.ResponseWriter, r *http.Request) {
endpoint(httpRequest).ServeHTTP(rw, r)
})
// Apply endpoint-specific middleware in reverse order.
for i := len(middlewares) - 1; i >= 0; i-- {
next = middlewares[i](httpRequest, next)
}
// Apply global middlewares in reverse order.
for i := len(h.router.globalMiddlewares) - 1; i >= 0; i-- {
next = h.router.globalMiddlewares[i](httpRequest, next)
}
next.ServeHTTP(rw, request)
}
// applyGlobalMiddlewares wraps the given http.Handler with the router's global middlewares.
func (router *Router) applyGlobalMiddlewares(handler http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, request *http.Request) {
httpRequest := NewHttpRequest(rw, request)
var next Response = ResponseFunc(func(rw http.ResponseWriter, r *http.Request) {
handler.ServeHTTP(rw, r)
})
// Apply global middlewares in reverse order.
for i := len(router.globalMiddlewares) - 1; i >= 0; i-- {
next = router.globalMiddlewares[i](httpRequest, next)
}
next.ServeHTTP(rw, request)
})
}