-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
113 lines (95 loc) · 2.68 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
package cart
type (
HandlerRoute func(*Router)
method struct {
key string
handler HandlerCompose
}
Router struct {
Engine *Engine
Path string
composed HandlerCompose
methods []method
}
)
func (r *Router) getMethod(httpMethod string) (HandlerCompose, bool) {
for _, entry := range r.methods {
if entry.key == httpMethod {
return entry.handler, true
}
}
return nil, false
}
func (r *Router) use(absolutePath string, handler HandlerCompose) *Router {
next, find := r.Engine.getRouter(absolutePath)
if _, composed := r.Engine.mixComposed(absolutePath); composed != nil {
next.composed = compose(composed, handler)
} else {
next.composed = compose(handler)
}
if !find {
r.Engine.addRoute(next)
}
return next
}
func (r *Router) handle(httpMethod, absolutePath string, handler HandlerCompose) *Router {
next, find := r.Engine.getRouter(absolutePath)
if _, composed := r.Engine.mixComposed(absolutePath); composed != nil {
next.composed = compose(composed)
}
method := method{key: httpMethod, handler: handler}
next.methods = append(next.methods, method)
if !find {
r.Engine.addRoute(next)
}
return next
}
func (r *Router) Route(relativePath string, handles ...HandlerRoute) *Router {
absolutePath := joinPaths(r.Path, relativePath)
next, _ := r.Engine.getRouter(absolutePath)
for _, handle := range handles {
handle(next)
}
return next
}
func (r *Router) Use(relativePath string, handles ...Handler) *Router {
absolutePath := joinPaths(r.Path, relativePath)
next := r.use(absolutePath, makeCompose(handles...))
return next
}
func (r *Router) ANY(handler Handler) *Router {
return r.handle("ANY", r.Path, makeCompose(handler))
}
func (r *Router) Handle(httpMethod string, handler HandlerFinal) *Router {
tempHandler := func(c *Context, next Next) {
handler(c)
}
return r.handle(httpMethod, r.Path, makeCompose(tempHandler))
}
func (r *Router) GET(handler HandlerFinal) *Router {
return r.Handle("GET", handler)
}
func (r *Router) POST(handler HandlerFinal) *Router {
return r.Handle("POST", handler)
}
func (r *Router) PUT(handler HandlerFinal) *Router {
return r.Handle("PUT", handler)
}
func (r *Router) PATCH(handler HandlerFinal) *Router {
return r.Handle("PATCH", handler)
}
func (r *Router) HEAD(handler HandlerFinal) *Router {
return r.Handle("HEAD", handler)
}
func (r *Router) OPTIONS(handler HandlerFinal) *Router {
return r.Handle("OPTIONS", handler)
}
func (r *Router) DELETE(handler HandlerFinal) *Router {
return r.Handle("DELETE", handler)
}
func (r *Router) CONNECT(handler HandlerFinal) *Router {
return r.Handle("CONNECT", handler)
}
func (r *Router) TRACE(handler HandlerFinal) *Router {
return r.Handle("TRACE", handler)
}