Move to []byte for caching and make it compile

This commit is contained in:
Moritz Marquardt 2024-03-26 07:38:15 +01:00
parent 5b6eecc75f
commit c4181d1206
11 changed files with 63 additions and 43 deletions

View file

@ -20,7 +20,7 @@ const canonicalDomainConfig = ".domains"
func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain, mainDomainSuffix string, canonicalDomainCache cache.ICache) (domain string, valid bool) {
// Check if this request is cached.
if cachedValue, ok := canonicalDomainCache.Get(o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch); ok {
domains := strings.Split(cachedValue, "\n")
domains := strings.Split(string(cachedValue), "\n")
for _, domain := range domains {
if domain == actualDomain {
valid = true
@ -33,7 +33,7 @@ func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain,
body, err := giteaClient.GiteaRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, canonicalDomainConfig)
if err != nil && !errors.Is(err, gitea.ErrorNotFound) {
log.Error().Err(err).Msgf("could not read %s of %s/%s", canonicalDomainConfig, o.TargetOwner, o.TargetRepo)
// TODO: WTF we just continue?!
// TODO: WTF we just continue?! Seems fine as body is empty... :/
}
var domains []string
@ -63,7 +63,7 @@ func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain,
}
// Add result to cache.
_ = canonicalDomainCache.Set(o.TargetOwner+"/"+o.TargetRepo+"/"+o.TargetBranch, strings.Join(domains, "\n"), canonicalDomainCacheTimeout)
_ = canonicalDomainCache.Set(o.TargetOwner+"/"+o.TargetRepo+"/"+o.TargetBranch, []byte(strings.Join(domains, "\n")), canonicalDomainCacheTimeout)
// Return the first domain from the list and return if any of the domains
// matched the requested domain.

View file

@ -31,7 +31,7 @@ func (o *Options) getRedirects(giteaClient *gitea.Client, redirectsCache cache.I
// Check for cached redirects
if cachedValue, ok := redirectsCache.Get(cacheKey); ok {
redirects := []Redirect{}
err := json.Unmarshal([]byte(cachedValue), redirects)
err := json.Unmarshal(cachedValue, redirects)
if err != nil {
log.Error().Err(err).Msgf("could not parse redirects for key %s", cacheKey)
// It's okay to continue, the array stays empty.
@ -68,7 +68,7 @@ func (o *Options) getRedirects(giteaClient *gitea.Client, redirectsCache cache.I
if err != nil {
log.Error().Err(err).Msgf("could not store redirects for key %s", cacheKey)
} else {
_ = redirectsCache.Set(cacheKey, string(redirectsJson), redirectsCacheTimeout)
_ = redirectsCache.Set(cacheKey, redirectsJson, redirectsCacheTimeout)
}
}
return redirects