~netlandish/links

801c4fd736ebe599e5819f7054aad6b556a845c3 — Yader Velasquez 9 months ago 010ad9f
Fix invalid domain handler

References: https://todo.code.netlandish.com/~netlandish/links/48
3 files changed, 37 insertions(+), 12 deletions(-)

M core/routes.go
M domain/middleware.go
M templates/inactive_domain.html
M core/routes.go => core/routes.go +30 -7
@@ 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 {

M domain/middleware.go => domain/middleware.go +4 -4
@@ 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)
		}
	}
}

M templates/inactive_domain.html => templates/inactive_domain.html +3 -1
@@ 4,6 4,8 @@
</section>
<section class="card shadow-card">
  <p class="text-center">{{.pd.Data.msg}}</p>
  <p class="text-center"><a href={{reverse "billing:create_subscription" .slug}}>{{.pd.Data.please_upgrade}}</a></p>
  {{ if .orgSlug }}
      <p class="text-center"><a href={{reverse "billing:create_subscription" .orgSlug}}>{{.pd.Data.please_upgrade}}</a></p>
  {{end}}
</section>
{{template "base_footer" .}}