-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrouter.go
81 lines (62 loc) · 2.06 KB
/
router.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
package anor
import (
"net/http"
"strings"
)
type MiddlewareFunc func(http.Handler) http.Handler
type Router struct {
middlewares []MiddlewareFunc
basePath string
mux *http.ServeMux
}
func (router *Router) Use(mw ...MiddlewareFunc) {
router.middlewares = append(router.middlewares, mw...)
}
// HandleFunc registers a new route with the given pattern, handler function, and optional middleware functions.
func (router *Router) HandleFunc(pattern string, handler http.HandlerFunc, middlewares ...MiddlewareFunc) {
finalHandler := http.Handler(handler)
finalHandler = router.applyMiddlewares(finalHandler, middlewares...)
ps := strings.Fields(pattern)
pattern = ps[0] + " " + router.basePath + ps[1]
router.mux.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
finalHandler.ServeHTTP(w, r)
})
}
func (router *Router) Handle(pattern string, handler http.Handler, middlewares ...MiddlewareFunc) {
finalHandler := handler
finalHandler = router.applyMiddlewares(finalHandler, middlewares...)
ps := strings.Fields(pattern)
pattern = ps[0] + " " + router.basePath + ps[1]
router.mux.Handle(pattern, finalHandler)
}
// ServeHTTP implements the http.Handler interface.
func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
router.mux.ServeHTTP(w, r)
}
func NewRouter() *Router {
return &Router{
mux: http.NewServeMux(),
}
}
func (router *Router) Group(prefix string, middlewares ...MiddlewareFunc) *Router {
m := make([]MiddlewareFunc, len(router.middlewares))
copy(m, router.middlewares)
r := &Router{
mux: router.mux,
basePath: router.basePath + prefix,
middlewares: m,
}
r.middlewares = append(r.middlewares, middlewares...)
return r
}
func (router *Router) applyMiddlewares(handler http.Handler, middlewares ...MiddlewareFunc) http.Handler {
// Apply per-route middlewares
for i := len(middlewares) - 1; i >= 0; i-- {
handler = middlewares[i](handler)
}
// Apply global middlewares
for i := len(router.middlewares) - 1; i >= 0; i-- {
handler = router.middlewares[i](handler)
}
return handler
}