-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
157 lines (127 loc) · 3.84 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package gohf
import (
"fmt"
"net/http"
"strings"
)
type Router struct {
pattern pattern
handlerRepository *handlerRepository
parentRouter *Router
subRouters []*Router
}
func New() *Router {
return &Router{
handlerRepository: newHandlerRepository(),
}
}
func (r *Router) Use(handlerFuncs ...HandlerFunc) {
for _, f := range handlerFuncs {
if f == nil {
continue
}
r.handlerRepository.addHandler(f, r, r.pattern, true)
}
}
func (r *Router) Handle(pattern string, handlerFuncs ...HandlerFunc) {
for _, f := range handlerFuncs {
if f == nil {
continue
}
parsedPattern, err := parsePattern(pattern)
if err != nil {
panic(err)
}
pat, err := mergePattern(r.pattern, parsedPattern)
if err != nil {
panic(err)
}
r.handlerRepository.addHandler(f, r, pat, false)
}
}
func (r *Router) GET(pattern string, handlerFuncs ...HandlerFunc) {
r.Handle(fmt.Sprintf("%s %s", http.MethodGet, pattern), handlerFuncs...)
}
func (r *Router) POST(pattern string, handlerFuncs ...HandlerFunc) {
r.Handle(fmt.Sprintf("%s %s", http.MethodPost, pattern), handlerFuncs...)
}
func (r *Router) PUT(pattern string, handlerFuncs ...HandlerFunc) {
r.Handle(fmt.Sprintf("%s %s", http.MethodPut, pattern), handlerFuncs...)
}
func (r *Router) PATCH(pattern string, handlerFuncs ...HandlerFunc) {
r.Handle(fmt.Sprintf("%s %s", http.MethodPatch, pattern), handlerFuncs...)
}
func (r *Router) DELETE(pattern string, handlerFuncs ...HandlerFunc) {
r.Handle(fmt.Sprintf("%s %s", http.MethodDelete, pattern), handlerFuncs...)
}
func (r *Router) OPTIONS(pattern string, handlerFuncs ...HandlerFunc) {
r.Handle(fmt.Sprintf("%s %s", http.MethodOptions, pattern), handlerFuncs...)
}
func (r *Router) HEAD(pattern string, handlerFuncs ...HandlerFunc) {
r.Handle(fmt.Sprintf("%s %s", http.MethodHead, pattern), handlerFuncs...)
}
func (r *Router) SubRouter(pattern string) *Router {
parsedPattern, err := parsePattern(pattern)
if err != nil {
panic(err)
}
pat, err := mergePattern(r.pattern, parsedPattern)
if err != nil {
panic(err)
}
pat.path = strings.TrimSuffix(pat.path, "/")
subRouter := &Router{
pattern: pat,
handlerRepository: r.handlerRepository,
parentRouter: r,
}
r.subRouters = append(r.subRouters, subRouter)
return subRouter
}
func (r *Router) CreateServeMux() *http.ServeMux {
mux := http.NewServeMux()
httpHandlerMap := r.getHttpHandlerMap()
for patternString, httpHandler := range httpHandlerMap.getMap() {
if httpHandler.len() > 0 {
mux.Handle(patternString, httpHandler)
}
}
return mux
}
func (r *Router) hasAncestor(router *Router) bool {
parentRouter := r.parentRouter
if parentRouter == router {
return true
}
return parentRouter != nil && parentRouter.hasAncestor(router)
}
func (r *Router) getHttpHandlerMap() *httpHandlerMap {
httpHandlerMap := newHttpHandlerMap()
r.setupHttpHandlerMap(httpHandlerMap)
return httpHandlerMap
}
func (r *Router) setupHttpHandlerMap(targetHttpHandlerMap *httpHandlerMap) {
localHandlerMap := newHttpHandlerMap()
routerPatternString := r.pattern.String()
if !strings.HasSuffix(routerPatternString, "/") {
routerPatternString += "/"
}
localHandlerMap.addPatternString(routerPatternString)
for _, h := range r.handlerRepository.getHandlers() {
if !h.all && h.owner == r {
localHandlerMap.addPatternString(h.pattern.String())
}
}
for _, handler := range r.handlerRepository.getHandlers() {
if handler.all && (handler.owner == r || r.hasAncestor(handler.owner)) {
localHandlerMap.addHandlerFuncToAll(handler.f)
} else if !handler.all && handler.owner == r {
patternString := handler.pattern.String()
localHandlerMap.addHandlerFuncToPatternString(patternString, handler.f)
}
}
writeHandlerMap(targetHttpHandlerMap, localHandlerMap)
for _, subRouter := range r.subRouters {
subRouter.setupHttpHandlerMap(targetHttpHandlerMap)
}
}