Skip to content

Commit

Permalink
refactor refreshing config
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrSaber committed Jul 9, 2022
1 parent 8fbb057 commit 3359d7d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
26 changes: 5 additions & 21 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,7 @@ func (c *Config) Validate() error {
func (c *Config) GetRedirect(host string) *Redirect {
// Refresh the config if it's stale
if c.Source == SOURCE_URL && time.Since(c.LoadedAt) >= c.UrlConfigRefresh.CacheTTL {
if err := c.Load(); err != nil {
logger.Err.Printf("Could not refresh config from URL: %s", err)
} else {
logger.Std.Printf("Refreshed config from URL, new config:\n\n%s\n", c)
}
refreshConfig(c)
}

matchedRedirect := matchDomain(host, c.Redirects, func(r Redirect) string { return r.From })
Expand All @@ -197,40 +193,28 @@ func (c *Config) GetRedirect(host string) *Redirect {
if matchedRefreshDomain != nil {
if matchedRefreshDomain.RefreshOn == "hit" && matchedRedirect != nil {
logger.Std.Printf("Refreshing config due to match with refresh domain %q and a redirect was found", matchedRefreshDomain.Domain)
if err := c.Load(); err != nil {
logger.Err.Printf("Could not refresh config from URL: %s", err)
}

refreshConfig(c)
return
}

if matchedRefreshDomain.RefreshOn == "miss" && matchedRedirect == nil {
logger.Std.Printf("Refreshing config due to match with refresh domain %q and no redirect was found", matchedRefreshDomain.Domain)
if err := c.Load(); err != nil {
logger.Err.Printf("Could not refresh config from URL: %s", err)
}

refreshConfig(c)
return
}
}

// Refresh config if refresh-on-hit is set and a redirect was found
if c.UrlConfigRefresh.RefreshOnHit && matchedRedirect != nil {
logger.Std.Printf("Refreshing config due to refresh-on-hit and a redirect was found")
if err := c.Load(); err != nil {
logger.Err.Printf("Could not refresh config from URL: %s", err)
}

refreshConfig(c)
return
}

// Refresh config if refresh-on-miss is set and no redirect was found
if c.UrlConfigRefresh.RefreshOnMiss && matchedRedirect == nil {
logger.Std.Printf("Refreshing config due to refresh-on-miss and no redirect was found")
if err := c.Load(); err != nil {
logger.Err.Printf("Could not refresh config from URL: %s", err)
}

refreshConfig(c)
return
}
}()
Expand Down
10 changes: 10 additions & 0 deletions src/config/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package config
import (
"regexp"
"strings"

"github.com/AmrSaber/redirector/src/logger"
)

// Returns a pointer to the element of the list that matched the domain after mapping it with the given mapper
Expand Down Expand Up @@ -47,3 +49,11 @@ func matchDomain[T any](domain string, list []T, mapper func(T) string) *T {

return nil
}

func refreshConfig(c *Config) {
if err := c.Load(); err != nil {
logger.Err.Printf("Could not refresh config from URL: %s", err)
} else {
logger.Std.Printf("Refreshed config from URL, new config:\n\n%s\n", c)
}
}

0 comments on commit 3359d7d

Please sign in to comment.