From d00fd5dbbee9ee4c2e2312beea5960fde7147689 Mon Sep 17 00:00:00 2001 From: "Gustavo L de Mello (Guz)" Date: Wed, 18 Dec 2024 16:03:31 -0300 Subject: [PATCH] fix: support trailing syntax in router --- groute/router/router.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/groute/router/router.go b/groute/router/router.go index c76d526..eb30b41 100644 --- a/groute/router/router.go +++ b/groute/router/router.go @@ -186,6 +186,10 @@ func (r defaultRouter) handleRoute(rt Route) { p = fmt.Sprintf("%s/", p) } + if strings.HasSuffix(p, "...}/") { + p = strings.TrimSuffix(p, "/") + } + r.routes[p] = rt r.mux.Handle(p, rt.Handler) } @@ -199,19 +203,6 @@ func (r *defaultRouter) parsePattern(pattern string) (method, host, p string) { p = path.Join("/", strings.Join(ps[1:], "/")) - // path.Join adds a trailing slash, if the original pattern doesn't has one, the parsed - // path shouldn't also - if !strings.HasSuffix(pattern, "/") { - p = strings.TrimSuffix(p, "/") - } - - // Since path.Join adds a trailing slash, it can break the {pattern...} syntax. - // So we check if it has the suffix "...}/" to see if it ends in "/{pattern...}/" - if strings.HasSuffix(p, "...}/") { - // If it does, we remove the any possible trailing slash - p = strings.TrimSuffix(p, "/") - } - // If "[METHOD ][HOST]" is empty, we just have the path and can send it back if ps[0] == "" { return "", "", p