From c8373aae813f03a6df597c991493b7b1369aec6a Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Wed, 10 Nov 2021 09:34:48 -0800 Subject: [PATCH] Fix link issue with forum redirects --- src/hmnurl/urls.go | 4 ++-- src/website/requesthandling.go | 21 +++++++++++++++------ src/website/routes.go | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/hmnurl/urls.go b/src/hmnurl/urls.go index 2d269ab4..8cc63eb0 100644 --- a/src/hmnurl/urls.go +++ b/src/hmnurl/urls.go @@ -292,7 +292,7 @@ func BuildProjectNew() string { return Url("/projects/new", nil) } -var RegexPersonalProject = regexp.MustCompile("^/p/(?P[0-9]+)(/(?P[a-zA-Z0-9-]+))?") +var RegexPersonalProject = regexp.MustCompile("^/p/(?P[0-9]+)(/(?P[a-zA-Z0-9-]+))?") func BuildPersonalProject(id int, slug string) string { defer CatchPanic() @@ -452,7 +452,7 @@ func (c *UrlContext) BuildForumThreadWithPostHash(subforums []string, threadId i defer CatchPanic() builder := buildForumThreadPath(subforums, threadId, title, page) - return UrlWithFragment(builder.String(), nil, strconv.Itoa(postId)) + return c.UrlWithFragment(builder.String(), nil, strconv.Itoa(postId)) } var RegexForumPost = regexp.MustCompile(`^/forums(/(?P[^\d/]+(/[^\d]+)*))?/t/(?P\d+)/p/(?P\d+)$`) diff --git a/src/website/requesthandling.go b/src/website/requesthandling.go index 0818a0c4..0ada4e98 100644 --- a/src/website/requesthandling.go +++ b/src/website/requesthandling.go @@ -34,6 +34,14 @@ type Route struct { Handler Handler } +func (r *Route) String() string { + var routeStrings []string + for _, regex := range r.Regexes { + routeStrings = append(routeStrings, regex.String()) + } + return fmt.Sprintf("%s %v", r.Method, routeStrings) +} + type RouteBuilder struct { Router *Router Prefixes []*regexp.Regexp @@ -107,6 +115,12 @@ nextroute: if paramName == "" { continue } + if _, alreadyExists := params[paramName]; alreadyExists { + logging.Warn(). + Str("route", route.String()). + Str("paramName", paramName). + Msg("duplicate names for path parameters; last one wins") + } params[paramName] = paramValue } @@ -118,13 +132,8 @@ nextroute: } } - var routeStrings []string - for _, regex := range route.Regexes { - routeStrings = append(routeStrings, regex.String()) - } - c := &RequestContext{ - Route: fmt.Sprintf("%v", routeStrings), + Route: route.String(), Logger: logging.GlobalLogger(), Req: req, Res: rw, diff --git a/src/website/routes.go b/src/website/routes.go index 79769357..e8c31b4c 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -307,7 +307,7 @@ func NewWebsiteRoutes(longRequestContext context.Context, conn *pgxpool.Pool, pe return c.Redirect(c.UrlContext.RewriteProjectUrl(c.URL()), http.StatusSeeOther) } - if c.PathParams["slug"] != models.GeneratePersonalProjectSlug(p.Project.Name) { + if c.PathParams["projectslug"] != models.GeneratePersonalProjectSlug(p.Project.Name) { return c.Redirect(c.UrlContext.RewriteProjectUrl(c.URL()), http.StatusSeeOther) }