/
route.go
161 lines (139 loc) · 3.42 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package routes
import (
"github.com/nathanfaucett/ptor"
"reflect"
"regexp"
)
var (
methods = []string{
"GET",
"POST",
"PUT",
"PATCH",
"HEAD",
"OPTIONS",
"DELETE",
}
)
type Route struct {
path string
regex *regexp.Regexp
params []*ptor.Param
stack map[string][]*Handler
methods map[string]bool
}
func NewRoute(path string, sensitive bool) *Route {
this := new(Route)
this.path = path
this.regex, this.params = ptor.PathToRegexp(path, sensitive, true)
this.stack = make(map[string][]*Handler)
this.methods = make(map[string]bool)
return this
}
func (this *Route) Match(path string) map[string]string {
test := this.regex.FindAllStringSubmatch(path, -1)
if len(test) == 0 {
return nil
}
result := test[0]
length := len(result)
if length == 0 {
return nil
}
params := make(map[string]string)
if len(this.params) == 0 {
return params
}
for i := range this.params {
if i < length {
params[this.params[i].Name] = result[i+1]
}
}
return params
}
func (this *Route) Mount(method string, arguments []interface{}) *Route {
has := false
for _, m := range methods {
if method == m {
has = true
}
}
if !has {
panic("Router does not support method " + method)
}
for _, handler := range arguments {
if !this.methods[method] {
this.methods[method] = true
}
this.stack[method] = append(this.stack[method], NewHandler(handler))
}
return this
}
func (this *Route) Unmount(method string, arguments ...interface{}) *Route {
has := false
for _, m := range methods {
if method == m {
has = true
}
}
if !has {
panic("Router does not support method " + method)
}
for i, handler := range this.stack[method] {
for _, function := range arguments {
fn := reflect.ValueOf(function)
if fn == handler.Func() {
this.stack[method] = append(this.stack[method][:i], this.stack[method][i+1:]...)
}
}
}
if len(this.stack[method]) == 0 {
this.methods[method] = false
}
return this
}
func (this *Route) All(arguments ...interface{}) *Route {
for _, handler := range arguments {
fn := NewHandler(handler)
this.stack[GET] = append(this.stack[GET], fn)
this.stack[POST] = append(this.stack[POST], fn)
this.stack[PUT] = append(this.stack[PUT], fn)
this.stack[PATCH] = append(this.stack[PATCH], fn)
this.stack[HEAD] = append(this.stack[HEAD], fn)
this.stack[OPTIONS] = append(this.stack[OPTIONS], fn)
this.stack[DELETE] = append(this.stack[DELETE], fn)
}
this.methods[GET] = true
this.methods[POST] = true
this.methods[PUT] = true
this.methods[PATCH] = true
this.methods[HEAD] = true
this.methods[OPTIONS] = true
this.methods[DELETE] = true
return this
}
func (this *Route) Get(arguments ...interface{}) *Route {
return this.Mount(GET, arguments)
}
func (this *Route) Post(arguments ...interface{}) *Route {
return this.Mount(POST, arguments)
}
func (this *Route) Put(arguments ...interface{}) *Route {
return this.Mount(PUT, arguments)
}
func (this *Route) Patch(arguments ...interface{}) *Route {
return this.Mount(PATCH, arguments)
}
func (this *Route) Update(arguments ...interface{}) *Route {
this.Mount(PUT, arguments)
return this.Mount(PATCH, arguments)
}
func (this *Route) Head(arguments ...interface{}) *Route {
return this.Mount(HEAD, arguments)
}
func (this *Route) Options(arguments ...interface{}) *Route {
return this.Mount(OPTIONS, arguments)
}
func (this *Route) Delete(arguments ...interface{}) *Route {
return this.Mount(DELETE, arguments)
}