From 9b44f4eb81c34144b7bbaea3aa06d96b3f574915 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Wed, 14 Feb 2024 17:34:28 -0600 Subject: [PATCH] More domain middleware work --- billing/routes.go | 14 +++--------- domain/middleware.go | 52 +++++++++++++++++++++++++------------------- list/routes.go | 9 ++++---- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/billing/routes.go b/billing/routes.go index a857477..bc0ca41 100644 --- a/billing/routes.go +++ b/billing/routes.go @@ -102,16 +102,12 @@ func (s *Service) SubscriptionPortal(c echo.Context) error { if err != nil { return err } - mainSchema, ok := gctx.Server.Config.File.Get("gobwebs", "scheme") - if !ok { - return fmt.Errorf("schema not found") - } mainDomain, ok := gctx.Server.Config.File.Get("links", "links-service-domain") if !ok { return fmt.Errorf("main domain not found") } returnURL := &url.URL{ - Scheme: mainSchema, + Scheme: gctx.Server.Config.Scheme, Host: mainDomain, Path: c.Echo().Reverse("core:org_list"), } @@ -522,21 +518,17 @@ func (s *Service) CreateSubscription(c echo.Context) error { return gctx.Render(http.StatusOK, "billing_create_subscription.html", gmap) } - mainSchema, ok := gctx.Server.Config.File.Get("gobwebs", "scheme") - if !ok { - return fmt.Errorf("schema not found") - } mainDomain, ok := gctx.Server.Config.File.Get("links", "links-service-domain") if !ok { return fmt.Errorf("main domain not found") } successURL := &url.URL{ - Scheme: mainSchema, + Scheme: gctx.Server.Config.Scheme, Host: mainDomain, Path: c.Echo().Reverse(s.RouteName("success")), } cancelURL := &url.URL{ - Scheme: mainSchema, + Scheme: gctx.Server.Config.Scheme, Host: mainDomain, Path: c.Echo().Reverse(s.RouteName("cancel")), } diff --git a/domain/middleware.go b/domain/middleware.go index 392d80c..0d26028 100644 --- a/domain/middleware.go +++ b/domain/middleware.go @@ -35,6 +35,24 @@ func ForContext(ctx context.Context) *models.Domain { return domain } +func badDomainRedirect(c echo.Context) error { + gctx := c.(*server.Context) + mainDomain, ok := gctx.Server.Config.File.Get("links", "links-service-domain") + if !ok { + return fmt.Errorf("main domain not found") + } + nextURL := &url.URL{ + Scheme: gctx.Server.Config.Scheme, + Host: mainDomain, + } + lt := localizer.GetSessionLocalizer(c) + // XXX This needs to change. See: https://todo.code.netlandish.com/~netlandish/links/48 + messages.Error( + c, lt.Translate("The domain is currently inactive. Please subscribe to activate this domain")) + return c.Redirect(http.StatusMovedPermanently, nextURL.String()) + +} + // DomainContext adds the current domain to request context. func DomainContext(service int) echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { @@ -45,8 +63,14 @@ func DomainContext(service int) echo.MiddlewareFunc { return err } if len(domains) != 1 { - return fmt.Errorf("Invalid domain") + if len(domains) > 1 { + gctx := c.(*server.Context) + gctx.Echo().Logger.Printf( + "domain.DomainContext: Multiple active domain records for %s\n", req.Host) + } + return badDomainRedirect(c) } + domain := domains[0] if domain.IsActive { c.SetRequest( @@ -57,20 +81,7 @@ func DomainContext(service int) echo.MiddlewareFunc { return next(c) } // If the domain is disabled - gctx := c.(*server.Context) - mainSchema, ok := gctx.Server.Config.File.Get("gobwebs", "scheme") - if !ok { - return fmt.Errorf("schema not found") - } - mainDomain := gctx.Server.Config.Domain - nextURL := &url.URL{ - Scheme: mainSchema, - Host: mainDomain, - } - lt := localizer.GetSessionLocalizer(c) - messages.Error( - c, lt.Translate("The domain is currently inactive. Please subscribe to activate this domain")) - return c.Redirect(http.StatusMovedPermanently, nextURL.String()) + return badDomainRedirect(c) } } } @@ -132,9 +143,10 @@ func DomainRedirect(next echo.HandlerFunc) echo.HandlerFunc { if !ok { return fmt.Errorf("links-service-domain not found") } - mainDomain = strings.ToLower(mainDomain) + // Remove :PORT if present + host := strings.SplitN(mainDomain, ":", 2)[0] domain := ForContext(c.Request().Context()) - if strings.ToLower(domain.LookupName) != strings.ToLower(mainDomain) && + if strings.ToLower(domain.LookupName) != strings.ToLower(host) && domain.Level == models.DomainLevelSystem { // XXX Populate this from links.InvalidSlugs or better yet, c.Echo().Routes() redirectPaths := []redir{ @@ -147,12 +159,8 @@ func DomainRedirect(next echo.HandlerFunc) echo.HandlerFunc { rPath := req.URL.Path for _, path := range redirectPaths { if path.Redirect(rPath) { - mainSchema, ok := gctx.Server.Config.File.Get("gobwebs", "scheme") - if !ok { - return fmt.Errorf("schema not found") - } nextURL := &url.URL{ - Scheme: mainSchema, + Scheme: gctx.Server.Config.Scheme, Host: mainDomain, Path: rPath, } diff --git a/list/routes.go b/list/routes.go index 175392b..e1ec119 100644 --- a/list/routes.go +++ b/list/routes.go @@ -1491,14 +1491,13 @@ func (r *DetailService) ListDetail(c echo.Context) error { domain := domain.ForContext(c.Request().Context()) if slug == "" && domain.Level == models.DomainLevelSystem { - mainDomain := gctx.Server.Config.Domain - mainSchema, ok := gctx.Server.Config.File.Get("gobwebs", "scheme") + linksDomain, ok := gctx.Server.Config.File.Get("links", "links-service-domain") if !ok { - return fmt.Errorf("schema not found") + return fmt.Errorf("links:links-service-domain not found") } nextURL := &url.URL{ - Scheme: mainSchema, - Host: mainDomain, + Scheme: gctx.Server.Config.Scheme, + Host: linksDomain, } return c.Redirect(http.StatusMovedPermanently, nextURL.String()) } -- 2.45.2