@@ -185,8 +185,18 @@ func (r *Router) Reverse(name string, params ...interface{}) string {
185
185
return uri .String ()
186
186
}
187
187
188
+ func normalizePathSlash (path string ) string {
189
+ if path == "" {
190
+ path = "/"
191
+ } else if path [0 ] != '/' {
192
+ path = "/" + path
193
+ }
194
+ return path
195
+ }
196
+
188
197
func (r * Router ) add (method , path , name string , h HandlerFunc ) * Route {
189
- r .Add (method , path , h )
198
+ path = normalizePathSlash (path )
199
+ r .insert (method , path , h )
190
200
191
201
route := & Route {
192
202
Method : method ,
@@ -199,13 +209,11 @@ func (r *Router) add(method, path, name string, h HandlerFunc) *Route {
199
209
200
210
// Add registers a new route for method and path with matching handler.
201
211
func (r * Router ) Add (method , path string , h HandlerFunc ) {
202
- // Validate path
203
- if path == "" {
204
- path = "/"
205
- }
206
- if path [0 ] != '/' {
207
- path = "/" + path
208
- }
212
+ r .insert (method , normalizePathSlash (path ), h )
213
+ }
214
+
215
+ func (r * Router ) insert (method , path string , h HandlerFunc ) {
216
+ path = normalizePathSlash (path )
209
217
pnames := []string {} // Param names
210
218
ppath := path // Pristine path
211
219
@@ -224,7 +232,7 @@ func (r *Router) Add(method, path string, h HandlerFunc) {
224
232
}
225
233
j := i + 1
226
234
227
- r .insert (method , path [:i ], staticKind , routeMethod {})
235
+ r .insertNode (method , path [:i ], staticKind , routeMethod {})
228
236
for ; i < lcpIndex && path [i ] != '/' ; i ++ {
229
237
}
230
238
@@ -234,21 +242,21 @@ func (r *Router) Add(method, path string, h HandlerFunc) {
234
242
235
243
if i == lcpIndex {
236
244
// path node is last fragment of route path. ie. `/users/:id`
237
- r .insert (method , path [:i ], paramKind , routeMethod {ppath , pnames , h })
245
+ r .insertNode (method , path [:i ], paramKind , routeMethod {ppath , pnames , h })
238
246
} else {
239
- r .insert (method , path [:i ], paramKind , routeMethod {})
247
+ r .insertNode (method , path [:i ], paramKind , routeMethod {})
240
248
}
241
249
} else if path [i ] == '*' {
242
- r .insert (method , path [:i ], staticKind , routeMethod {})
250
+ r .insertNode (method , path [:i ], staticKind , routeMethod {})
243
251
pnames = append (pnames , "*" )
244
- r .insert (method , path [:i + 1 ], anyKind , routeMethod {ppath , pnames , h })
252
+ r .insertNode (method , path [:i + 1 ], anyKind , routeMethod {ppath , pnames , h })
245
253
}
246
254
}
247
255
248
- r .insert (method , path , staticKind , routeMethod {ppath , pnames , h })
256
+ r .insertNode (method , path , staticKind , routeMethod {ppath , pnames , h })
249
257
}
250
258
251
- func (r * Router ) insert (method , path string , t kind , rm routeMethod ) {
259
+ func (r * Router ) insertNode (method , path string , t kind , rm routeMethod ) {
252
260
// Adjust max param
253
261
paramLen := len (rm .pnames )
254
262
if * r .echo .maxParam < paramLen {
0 commit comments