-
Notifications
You must be signed in to change notification settings - Fork 1
/
route.go
134 lines (113 loc) · 3.41 KB
/
route.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
package pi
import "strings"
// Route represents an API Route.
// For example: /user/get/{id}
type Route struct {
RouteURL string
ChildRoutes routes
Methods map[string]HandlerFunction
Interceptors interceptors
}
type routes []*Route
// newRoute returns a new Route.
func newRoute(RouteURL string, ChildRoutes ...*Route) *Route {
return &Route{
RouteURL: RouteURL,
ChildRoutes: ChildRoutes,
Methods: make(map[string]HandlerFunction),
}
}
// Before registers an interceptor to be called before the request is handled.
func (r *Route) Before(handlers ...HandlerFunction) *Route {
for _, handler := range handlers {
r.Interceptors.addBefore(handler)
}
return r
}
// After registers an interceptor to be called after the request has been handled.
func (r *Route) After(handlers ...HandlerFunction) *Route {
for _, handler := range handlers {
r.Interceptors.addAfter(handler)
}
return r
}
// AfterAsync registers an interceptor to be called asynchronously after the request has been handled.
func (r *Route) AfterAsync(handlers ...HandlerFunction) *Route {
for _, handler := range handlers {
r.Interceptors.addAfterAsync(handler)
}
return r
}
func (r *Route) Recover(recoverers ...RecovererFunction) *Route {
for _, rr := range recoverers {
r.Interceptors.addRecoverer(rr)
}
return r
}
// Error registers an interceptor to be called when an error occurs in the request handler or in any Before interceptor.
func (r *Route) Error(handlers ...HandlerErrorFunction) *Route {
for _, handler := range handlers {
r.Interceptors.addError(handler)
}
return r
}
// Any registers an HandlerFunction to handle any requests.
func (r *Route) Any(handlerFunc HandlerFunction) *Route {
r.Get(handlerFunc)
r.Post(handlerFunc)
r.Put(handlerFunc)
r.Delete(handlerFunc)
r.Patch(handlerFunc)
r.Options(handlerFunc)
r.Head(handlerFunc)
return r
}
// Get registers an HandlerFunction to handle GET requests.
func (r *Route) Get(handlerFunc HandlerFunction) *Route {
r.Methods["GET"] = handlerFunc
return r
}
// Post registers an HandlerFunction to handle POST requests.
func (r *Route) Post(handlerFunc HandlerFunction) *Route {
r.Methods["POST"] = handlerFunc
return r
}
// Put registers an HandlerFunction to handle PUT requests.
func (r *Route) Put(handlerFunc HandlerFunction) *Route {
r.Methods["PUT"] = handlerFunc
return r
}
// Delete registers an HandlerFunction to handle DELETE requests.
func (r *Route) Delete(handlerFunc HandlerFunction) *Route {
r.Methods["DELETE"] = handlerFunc
return r
}
// Patch registers an HandlerFunction to handle PATCH requests.
func (r *Route) Patch(handlerFunc HandlerFunction) *Route {
r.Methods["PATCH"] = handlerFunc
return r
}
// Options registers an HandlerFunction to handle OPTIONS requests.
func (r *Route) Options(handlerFunc HandlerFunction) *Route {
r.Methods["OPTIONS"] = handlerFunc
return r
}
// Head registers an HandlerFunction to handle HEAD requests.
func (r *Route) Head(handlerFunc HandlerFunction) *Route {
r.Methods["HEAD"] = handlerFunc
return r
}
// Custom registers an HandlerFunction to handle custom requests.
func (r *Route) Custom(method string, handlerFunc HandlerFunction) *Route {
r.Methods[method] = handlerFunc
return r
}
func (r routes) Len() int {
return len(r)
}
func (r routes) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
func (r routes) Less(i, j int) bool {
return strings.Count(r[i].RouteURL, "/") > strings.Count(r[j].RouteURL, "/")
}