~netlandish/links

9b44f4eb81c34144b7bbaea3aa06d96b3f574915 — Peter Sanchez 9 months ago 1486b52
More domain middleware work
3 files changed, 37 insertions(+), 38 deletions(-)

M billing/routes.go
M domain/middleware.go
M list/routes.go
M billing/routes.go => billing/routes.go +3 -11
@@ 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")),
		}

M domain/middleware.go => domain/middleware.go +30 -22
@@ 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,
					}

M list/routes.go => list/routes.go +4 -5
@@ 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())
	}