summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-11-27 11:19:29 +0800
committerEltrac <hi@guhub.cn>2025-11-27 11:19:29 +0800
commit21660af7bef5e7a0e679fcbd8bee7352642b24f8 (patch)
tree2ea68589c16fc4eca98c0e2efc3650fe8588c3e7
parentc6f91b272056255edbacf0d81b75b5995febf175 (diff)
fix(router): `/path` and `/path/` were not the same route
-rw-r--r--router.go8
1 files changed, 6 insertions, 2 deletions
diff --git a/router.go b/router.go
index 193d076..86d706a 100644
--- a/router.go
+++ b/router.go
@@ -19,13 +19,17 @@ func NewRouter() *Router {
return &Router{}
}
+func (r *Router) splitPath(path string) []string {
+ return strings.Split(strings.Trim(path, "/"), "/")
+}
+
func (r *Router) Add(method string, path string, h Handler) {
- parts := strings.Split(path, "/")
+ parts := r.splitPath(path)
r.routes = append(r.routes, route{method, parts, h})
}
func (r *Router) Find(method string, path string) (Handler, map[string]string) {
- parts := strings.Split(path, "/")
+ parts := r.splitPath(path)
for _, rt := range r.routes {
// match HTTP method