-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
150 lines (120 loc) · 3.21 KB
/
routes.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
// Copyright 2013 Justin Wilson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dingo
import (
"reflect"
"regexp"
)
/*----------static----------*/
type route struct {
path string
handler Handler
}
// NewSRoute returns a new static route for the given path/handler.
func NewSRoute(path string, handler Handler) Route {
rt := new(route)
rt.path = path
rt.handler = handler
//return *rt
return rt
}
// Returns the path of the route.
func (r *route) Path() string {
return r.path
}
// IsCanonical returns if the route is canonical.
func (r *route) IsCanonical() bool {
return true
}
// Matches returns wether this route matches the given path.
func (r *route) Matches(path string) bool {
return r.path == path
}
// Execute invokes the handler for this route, writing it's response.
func (r *route) Execute(ctx Context) {
r.handler(ctx)
}
/*----------regexp----------*/
type reRoute struct {
path string
expr *regexp.Regexp
handler Handler
}
// NewReRoute returns a new RegEx route for the given path/handler.
func NewReRoute(re string, handler Handler) Route {
rt := new(reRoute)
rt.path = re
rt.expr = regexp.MustCompile(re)
rt.handler = handler
return rt
}
func (r *reRoute) data(url string) map[string]string {
data := make(map[string]string)
//matches := r.expr.FindAllStringSubmatch(url, -1)
matches := r.expr.FindStringSubmatch(url)
for i, n := range r.expr.SubexpNames() {
if i == 0 {
continue
}
data[n] = matches[i]
}
return data
}
// Returns the path of the route.
func (r *reRoute) Path() string {
return r.path
}
// IsCanonical returns if the route is canonical.
func (r *reRoute) IsCanonical() bool {
return true
}
// Matches returns wether this route matches the given path.
func (r *reRoute) Matches(url string) bool {
return r.expr.MatchString(url)
}
// Execute invokes the handler for this route, writing it's response.
func (r *reRoute) Execute(ctx Context) {
ctx.RouteData = r.data(ctx.URL.Path)
r.handler(ctx)
}
/*---------regexp2----------*/
type rRoute struct {
path string
expr *regexp.Regexp
handler reflect.Value
}
// NewRRoute returns a new RegEx route for the given path/handler.
func NewRRoute(re string, handler interface{}) Route {
r := new(rRoute)
r.path = re
r.expr = regexp.MustCompile(re)
if fn, ok := handler.(reflect.Value); ok {
r.handler = fn
} else {
r.handler = reflect.ValueOf(handler)
}
return r
}
// Returns the path of the route.
func (r *rRoute) Path() string {
return r.path
}
// IsCanonical returns if the route is canonical.
func (r *rRoute) IsCanonical() bool {
return true
}
// Matches returns wether this route matches the given path.
func (r *rRoute) Matches(url string) bool {
return r.expr.MatchString(url)
}
// Execute invokes the handler, passing in the RegEx groups, for this route, writing it's response.
func (r *rRoute) Execute(ctx Context) {
// TODO it would be nice if we could detect numbers and cast them as such prior to invoking the func
args := []reflect.Value{reflect.ValueOf(ctx)}
matches := r.expr.FindStringSubmatch(ctx.URL.Path)
for _, a := range matches[1:] {
args = append(args, reflect.ValueOf(a))
}
r.handler.Call(args)
}