From 801c4fd736ebe599e5819f7054aad6b556a845c3 Mon Sep 17 00:00:00 2001 From: Yader Velasquez Date: Thu, 15 Feb 2024 14:57:38 -0600 Subject: [PATCH] Fix invalid domain handler References: https://todo.code.netlandish.com/~netlandish/links/48 --- core/routes.go | 37 +++++++++++++++++++++++++++------- domain/middleware.go | 8 ++++---- templates/inactive_domain.html | 4 +++- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/core/routes.go b/core/routes.go index 6cf08ea..f75115e 100644 --- a/core/routes.go +++ b/core/routes.go @@ -37,7 +37,7 @@ type Service struct { // RegisterRoutes ... func (s *Service) RegisterRoutes() { s.eg.GET("/", s.Homepage).Name = "index" - s.eg.GET("/inactive", s.InactiveDomain).Name = s.RouteName("inactive_domain") + s.eg.GET("/invalid/:d", s.InvalidDomain).Name = s.RouteName("invalid_domain") s.eg.GET("/:slug", s.OrgLinksList).Name = s.RouteName("org_link_list") s.eg.GET("/recent", s.OrgLinksList).Name = s.RouteName("recent_link_list") s.eg.GET("/popular", s.PopularLinkList).Name = s.RouteName("popular_link_list") @@ -91,15 +91,38 @@ func (s *Service) RegisterRoutes() { s.eg.GET("/tag-autocomplete", s.TagAutocomplete).Name = s.RouteName("tag_autocomplete") } -func (s *Service) InactiveDomain(c echo.Context) error { +func (s *Service) InvalidDomain(c echo.Context) error { + d := c.Param("d") + if d == "" { + return echo.NotFoundHandler(c) + } + gctx := c.(*server.Context) lt := localizer.GetSessionLocalizer(c) - pd := localizer.NewPageData(lt.Translate("Inactive Domain")) - pd.Data["msg"] = lt.Translate("The domain is currently inactive") + pd := localizer.NewPageData(lt.Translate("Invalid Domain")) + gmap := gobwebs.Map{"pd": pd} + + opts := &database.FilterOptions{ + Filter: sq.And{ + sq.Eq{"d.lookup_name": d}, + sq.Eq{"d.is_active": false}, + }, + } + domains, err := models.GetDomains(c.Request().Context(), opts) + if err != nil { + return err + } + if len(domains) != 1 { + pd.Data["msg"] = lt.Translate("The %s domain is invalid", d) + return gctx.Render(http.StatusOK, "inactive_domain.html", gmap) + } + domain := domains[0] + pd.Data["msg"] = lt.Translate("The %s domain is currently inactive", d) pd.Data["please_upgrade"] = lt.Translate("Please upgrade your account to reactivate it") - slug := links.PullOrgSlug(c) - gMap := gobwebs.Map{"pd": pd, "slug": slug} - return gctx.Render(http.StatusOK, "inactive_domain.html", gMap) + if domain.OrgSlug.Valid { + gmap["orgSlug"] = domain.OrgSlug.String + } + return gctx.Render(http.StatusOK, "inactive_domain.html", gmap) } func (s *Service) Homepage(c echo.Context) error { diff --git a/domain/middleware.go b/domain/middleware.go index af9986b..c6f48b0 100644 --- a/domain/middleware.go +++ b/domain/middleware.go @@ -33,7 +33,7 @@ func ForContext(ctx context.Context) *models.Domain { return domain } -func badDomainRedirect(c echo.Context) error { +func badDomainRedirect(c echo.Context, d string) error { gctx := c.(*server.Context) mainDomain, ok := gctx.Server.Config.File.Get("links", "links-service-domain") if !ok { @@ -42,7 +42,7 @@ func badDomainRedirect(c echo.Context) error { nextURL := &url.URL{ Scheme: gctx.Server.Config.Scheme, Host: mainDomain, - Path: c.Echo().Reverse("core:inactive_domain"), + Path: c.Echo().Reverse("core:invalid_domain", d), } return c.Redirect(http.StatusMovedPermanently, nextURL.String()) @@ -63,7 +63,7 @@ func DomainContext(service int) echo.MiddlewareFunc { gctx.Echo().Logger.Printf( "domain.DomainContext: Multiple active domain records for %s\n", req.Host) } - return badDomainRedirect(c) + return badDomainRedirect(c, req.Host) } domain := domains[0] @@ -76,7 +76,7 @@ func DomainContext(service int) echo.MiddlewareFunc { return next(c) } // If the domain is disabled - return badDomainRedirect(c) + return badDomainRedirect(c, req.Host) } } } diff --git a/templates/inactive_domain.html b/templates/inactive_domain.html index c24f29b..249edf0 100644 --- a/templates/inactive_domain.html +++ b/templates/inactive_domain.html @@ -4,6 +4,8 @@

{{.pd.Data.msg}}

-

{{.pd.Data.please_upgrade}}

+ {{ if .orgSlug }} +

{{.pd.Data.please_upgrade}}

+ {{end}}
{{template "base_footer" .}} -- 2.45.2