-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
104 lines (82 loc) · 2.72 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
package gimlet
import (
"net/http"
"strings"
)
type GroupHandler func(r Router)
type router struct {
routes []*Route
engine *Engine
rootPattern string
groups []*router
}
type Router interface {
GET(pattern string, handler Handler) *Route
POST(pattern string, handler Handler) *Route
DELETE(pattern string, handler Handler) *Route
PUT(pattern string, handler Handler) *Route
Method(method string, pattern string, handler Handler) *Route
addRoute(method string, pattern string, handler Handler) *Route
Group(pattern string, h GroupHandler)
resolveRequest(w http.ResponseWriter, req *http.Request) (*Route, *ContextParams)
resolveGroups(w http.ResponseWriter, req *http.Request) (*Route, *ContextParams)
}
func NewRouter(engine *Engine) *router {
return &router{
engine: engine,
rootPattern: "",
}
}
func (r *router) GET(pattern string, handler Handler) *Route {
return r.addRoute(http.MethodGet, pattern, handler)
}
func (r *router) POST(pattern string, handler Handler) *Route {
return r.addRoute(http.MethodPost, pattern, handler)
}
func (r *router) DELETE(pattern string, handler Handler) *Route {
return r.addRoute(http.MethodDelete, pattern, handler)
}
func (r *router) PUT(pattern string, handler Handler) *Route {
return r.addRoute(http.MethodPut, pattern, handler)
}
func (r *router) Method(method string, pattern string, handler Handler) *Route {
return r.addRoute(method, pattern, handler)
}
func (r *router) addRoute(method string, pattern string, handler Handler) *Route {
pattern = "/" + strings.Trim(r.rootPattern+"/"+strings.Trim(pattern, "/"), "/")
newRoute := newRoute(method, pattern, handler)
r.routes = append(r.routes, newRoute)
return newRoute
}
func (r *router) Group(pattern string, handler GroupHandler) {
newRouter := NewRouter(r.engine)
newRouter.rootPattern = strings.Trim(r.rootPattern+pattern, "/")
r.groups = append(r.groups, newRouter)
handler(newRouter)
}
func (r *router) resolveRequest(w http.ResponseWriter, req *http.Request) (*Route, *ContextParams) {
var route *Route
for _, currentRoute := range r.routes {
matches, params := currentRoute.matches(req)
if matches {
if currentRoute.method != req.Method && req.Method != http.MethodOptions {
route = currentRoute
continue
}
return currentRoute, NewContextParams(params[0], params[1])
}
}
if route != nil {
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return route, nil
}
return r.resolveGroups(w, req)
}
func (r *router) resolveGroups(w http.ResponseWriter, req *http.Request) (*Route, *ContextParams) {
for _, group := range r.groups {
if route, ctxParams := group.resolveRequest(w, req); route != nil {
return route, ctxParams
}
}
return nil, nil
}