diff options
author | Nejc Habjan <nejc.habjan@siemens.com> | 2022-06-09 16:38:28 +0300 |
---|---|---|
committer | Nejc Habjan <nejc.habjan@siemens.com> | 2022-06-27 11:36:53 +0300 |
commit | 39b7e2c35b2f609623ae483ee3f607b3da42808e (patch) | |
tree | 1dfd076dff5dad0a86064edb36eb9097ccde2815 | |
parent | 31b7783c26879de6f170e40777c799dfd797213b (diff) |
Apply review suggestions
-rw-r--r-- | app.go | 3 | ||||
-rw-r--r-- | internal/redirects/matching.go | 2 | ||||
-rw-r--r-- | internal/redirects/redirects.go | 17 | ||||
-rw-r--r-- | internal/redirects/validations.go | 2 |
4 files changed, 15 insertions, 9 deletions
@@ -32,6 +32,7 @@ import ( "gitlab.com/gitlab-org/gitlab-pages/internal/httperrors" "gitlab.com/gitlab-org/gitlab-pages/internal/logging" "gitlab.com/gitlab-org/gitlab-pages/internal/netutil" + "gitlab.com/gitlab-org/gitlab-pages/internal/redirects" "gitlab.com/gitlab-org/gitlab-pages/internal/rejectmethods" "gitlab.com/gitlab-org/gitlab-pages/internal/request" "gitlab.com/gitlab-org/gitlab-pages/internal/routing" @@ -349,6 +350,8 @@ func (a *theApp) listenMetrics(eg *errgroup.Group, config cfg.Metrics) *http.Ser } func runApp(config *cfg.Config) error { + redirects.SetConfig(config.Redirects) + source, err := gitlab.New(&config.GitLab) if err != nil { return fmt.Errorf("could not create domains config source: %w", err) diff --git a/internal/redirects/matching.go b/internal/redirects/matching.go index 0b4d26e0..25e2ce2e 100644 --- a/internal/redirects/matching.go +++ b/internal/redirects/matching.go @@ -103,7 +103,7 @@ func matchesRule(rule *netlifyRedirects.Rule, path string) (bool, string) { // If no rule matches, this function returns `nil` and an empty string func (r *Redirects) match(path string) (*netlifyRedirects.Rule, string) { for i := range r.rules { - if i >= cfg.Redirects.MaxRuleCount { + if i >= cfg.MaxRuleCount { // do not process any more rules return nil, "" } diff --git a/internal/redirects/redirects.go b/internal/redirects/redirects.go index 3c214073..04815404 100644 --- a/internal/redirects/redirects.go +++ b/internal/redirects/redirects.go @@ -26,8 +26,7 @@ const ( ) var ( - // hack, todo: properly pass config context to internals - cfg, _ = config.LoadConfig() + cfg = config.Redirects{} // ErrNoRedirect is the error thrown when a no redirect rule matches while trying to Rewrite URL. // This means that no redirect applies to the URL and you can fallback to serving actual content instead. ErrNoRedirect = errors.New("no redirect found") @@ -44,10 +43,14 @@ var ( errNoParams = errors.New("params not supported") errUnsupportedStatus = errors.New("status not supported") errNoForce = errors.New("force! not supported") - errTooManyPathSegments = fmt.Errorf("url path cannot contain more than %d forward slashes", cfg.Redirects.MaxConfigSize) + errTooManyPathSegments = fmt.Errorf("url path cannot contain more than %d forward slashes", cfg.MaxConfigSize) regexpPlaceholder = regexp.MustCompile(`(?i)/:[a-z]+`) ) +func SetConfig(redirectsConfig config.Redirects) { + cfg = redirectsConfig +} + type Redirects struct { rules []netlifyRedirects.Rule error error @@ -63,13 +66,13 @@ func (r *Redirects) Status() string { messages = append(messages, fmt.Sprintf("%d rules", len(r.rules))) for i, rule := range r.rules { - if i >= cfg.Redirects.MaxConfigSize { + if i >= cfg.MaxConfigSize { messages = append([]string{ fmt.Sprintf( "The _redirects file contains (%d) rules, more than the maximum of %d rules. Only the first %d rules will be processed.", len(r.rules), - cfg.Redirects.MaxRuleCount, - cfg.Redirects.MaxRuleCount, + cfg.MaxRuleCount, + cfg.MaxRuleCount, )}, messages..., ) @@ -120,7 +123,7 @@ func ParseRedirects(ctx context.Context, root vfs.Root) *Redirects { return &Redirects{error: errNeedRegularFile} } - if int(fi.Size()) > cfg.Redirects.MaxConfigSize { + if int(fi.Size()) > cfg.MaxConfigSize { return &Redirects{error: errFileTooLarge} } diff --git a/internal/redirects/validations.go b/internal/redirects/validations.go index 4e5eb86a..c37b79ec 100644 --- a/internal/redirects/validations.go +++ b/internal/redirects/validations.go @@ -43,7 +43,7 @@ func validateURL(urlText string) error { // Limit the number of path segments a rule can contain. // This prevents the matching logic from generating regular // expressions that are too large/complex. - if strings.Count(url.Path, "/") > cfg.Redirects.MaxPathSegments { + if strings.Count(url.Path, "/") > cfg.MaxPathSegments { return errTooManyPathSegments } } else { |