fix: support trailing syntax in router

This commit is contained in:
Guz
2024-12-18 16:03:31 -03:00
parent c0854dea2c
commit d00fd5dbbe

View File

@@ -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