-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmux.go
151 lines (128 loc) · 2.67 KB
/
mux.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
package mux
import (
"net/http"
"strings"
)
type param struct {
name string
fixed bool
}
type Route struct {
prefix string
partNames []param
function http.Handler
}
type Routes struct {
roots map[string][]Route
}
func NewRoutes() *Routes {
return &Routes{
roots: make(map[string][]Route),
}
}
func (r *Routes) Add(path string, f http.Handler) {
parts := strings.Split(path, "/")
var rootParts []string
var varParts []param
var paramsFound bool
for _, p := range parts {
if strings.HasPrefix(p, ":") {
paramsFound = true
}
if paramsFound {
if strings.HasPrefix(p, ":") {
varParts = append(varParts, param{
name: strings.TrimPrefix(p, ":"),
fixed: false,
})
} else {
varParts = append(varParts, param{
name: p,
fixed: true,
})
}
} else {
rootParts = append(rootParts, p)
}
}
root := strings.Join(rootParts, "/")
r.roots[root] = append(r.roots[root], Route{
prefix: root,
partNames: varParts,
function: f,
})
}
/**
@info Gets http.Handler and params from the routes table
@param {string} [path] Path of the route to find
@returns {http.Handler, map[string]string, bool}
*/
func (r *Routes) Get(path string) (http.Handler, map[string]string, bool) {
var routes []Route
remaining := path
for {
var ok bool
routes, ok = r.roots[remaining]
if ok {
return matchRoutes(path, routes)
}
if len(remaining) < 2 {
return nil, nil, false
}
index := strings.LastIndex(remaining, "/")
if index < 0 {
return nil, nil, false
}
if index > 0 {
remaining = remaining[:index]
} else {
remaining = "/"
}
}
}
/**
@info Matches routes to the request
@param {string} [path] Path of the request route to find
@param {[]Route} [routes] The array of routes to match
@returns {http.Handler, map[string]string, bool}
*/
func matchRoutes(path string, routes []Route) (http.Handler, map[string]string, bool) {
outer:
for _, r := range routes {
params := strings.Split(
strings.TrimPrefix(
strings.TrimPrefix(path, r.prefix),
"/"),
"/")
valid := cleanArray(params)
if len(valid) == len(r.partNames) {
paramNames := make(map[string]string)
for i, p := range r.partNames {
if p.fixed {
if params[i] != p.name {
continue outer
} else {
continue
}
}
paramNames[p.name] = params[i]
}
return r.function, paramNames, true
}
}
return nil, nil, false
}
/**
@info Cleans the array and finds non nill values
@param {string} [path] The array of string to slice and clean
@returns {[]string}
*/
func cleanArray(a []string) []string {
var valid []string
for _, s := range a {
if s != "" {
valid = append(valid, s)
}
}
return valid
}