-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmux.go
More file actions
173 lines (156 loc) · 3.44 KB
/
Copy pathmux.go
File metadata and controls
173 lines (156 loc) · 3.44 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
package minima
import (
"strings"
)
/**
* @info The Param structure
* @property {string} [name] The name of the param
* @property {bool} [fixed] Whether the param is fixed or not
*/
type param struct {
name string
fixed bool
}
/**
* @info The Route structure
* @property {string} [prefix] The prefix of the route
* @property {[]param} [partnames] The route paths split into parts
* @property {Handler} [function] The handler to be used
*/
type Route struct {
prefix string
partNames []param
function Handler
}
/**
* @info The Routes root structure
* @property {map[string][]Route} [roots] The map of array routes
*/
type Routes struct {
roots map[string][]Route
}
/**
* @info Makes a new Routes instance
* @returns {*Routes}
*/
func NewRoutes() *Routes {
return &Routes{
roots: make(map[string][]Route),
}
}
/**
* @info Adds a new route to the routes table
* @param {string} [path] Path of the route
* @param {Handler} [handler] Handler of the route
*/
func (r *Routes) Add(p string, f Handler) {
var path string
path = p
if p[len(p)-1:] == "/" && p != "/" {
path = strings.TrimSuffix(p, p[len(p)-1:])
}
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 handler and params from the routes table
* @param {string} [path] Path of the route to find
* @returns {Handler, map[string]string, bool}
*/
func (r *Routes) Get(path string) (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 {Handler, map[string]string, bool}
*/
func matchRoutes(path string, routes []Route) (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
}