From 3e0441211399ef870828997e91e2fec2c452571f Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Thu, 26 May 2022 11:33:17 -0400 Subject: [PATCH] add some more logging so i can see where stuff is failing --- server/handler.go | 5 +++-- server/upstream/gitea.go | 2 ++ server/upstream/helper.go | 5 +++++ server/upstream/upstream.go | 4 +++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/server/handler.go b/server/handler.go index 8445850..b6d5d42 100644 --- a/server/handler.go +++ b/server/handler.go @@ -89,6 +89,7 @@ func Handler(mainDomainSuffix, rawDomain []byte, branchTimestampResult := upstream.GetBranchTimestamp(targetOwner, repo, branch, giteaRoot, giteaAPIToken, branchTimestampCache) if branchTimestampResult == nil { // branch doesn't exist + log.Debug().Msgf("branch %s doesn't exist for %s/%s", branch, targetOwner, repo) return false } @@ -111,7 +112,8 @@ func Handler(mainDomainSuffix, rawDomain []byte, return true } - log.Debug().Msg("preparations") + log.Debug().Msgf("host %s, domain %s", trimmedHost, rawDomain) + if rawDomain != nil && bytes.Equal(trimmedHost, rawDomain) { // Serve raw content from RawDomain log.Debug().Msg("raw domain") @@ -168,7 +170,6 @@ func Handler(mainDomainSuffix, rawDomain []byte, targetPath = strings.Trim(strings.Join(pathElements[1:], "/"), "/") if targetOwner == "www" { - // www.codeberg.page redirects to codeberg.page // TODO: rm hardcoded - use cname? ctx.Redirect("https://"+string(mainDomainSuffix[1:])+string(ctx.Path()), fasthttp.StatusPermanentRedirect) return } diff --git a/server/upstream/gitea.go b/server/upstream/gitea.go index eeeb0a6..34653c4 100644 --- a/server/upstream/gitea.go +++ b/server/upstream/gitea.go @@ -6,6 +6,7 @@ import ( "path" "time" + "github.com/rs/zerolog/log" "github.com/valyala/fasthttp" "github.com/valyala/fastjson" ) @@ -54,6 +55,7 @@ func giteaGetRepoDefaultBranch(giteaRoot, repoOwner, repoName, giteaAPIToken str req := fasthttp.AcquireRequest() req.SetRequestURI(path.Join(giteaRoot, giteaAPIRepos, repoOwner, repoName)) + log.Debug().Msgf("trying repo %s/%s", repoOwner, repoName) req.Header.Set(fasthttp.HeaderAuthorization, giteaAPIToken) res := fasthttp.AcquireResponse() diff --git a/server/upstream/helper.go b/server/upstream/helper.go index 3b51479..0803baf 100644 --- a/server/upstream/helper.go +++ b/server/upstream/helper.go @@ -1,6 +1,7 @@ package upstream import ( + "github.com/rs/zerolog/log" "time" "codeberg.org/codeberg/pages/server/cache" @@ -14,6 +15,8 @@ type branchTimestamp struct { // GetBranchTimestamp finds the default branch (if branch is "") and returns the last modification time of the branch // (or nil if the branch doesn't exist) func GetBranchTimestamp(owner, repo, branch, giteaRoot, giteaAPIToken string, branchTimestampCache cache.SetGetKey) *branchTimestamp { + log.Debug().Msgf("looking up timestamp for %s/%s %s branch", owner, repo, branch) + if result, ok := branchTimestampCache.Get(owner + "/" + repo + "/" + branch); ok { if result == nil { return nil @@ -27,6 +30,7 @@ func GetBranchTimestamp(owner, repo, branch, giteaRoot, giteaAPIToken string, br // Get default branch defaultBranch, err := giteaGetRepoDefaultBranch(giteaRoot, owner, repo, giteaAPIToken) if err != nil { + log.Err(err) _ = branchTimestampCache.Set(owner+"/"+repo+"/", nil, defaultBranchCacheTimeout) return nil } @@ -35,6 +39,7 @@ func GetBranchTimestamp(owner, repo, branch, giteaRoot, giteaAPIToken string, br timestamp, err := giteaGetRepoBranchTimestamp(giteaRoot, owner, repo, branch, giteaAPIToken) if err != nil { + log.Err(err) return nil } result.Timestamp = timestamp diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index 33c80e9..a85762a 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -55,6 +55,8 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st o.ForbiddenMimeTypes = map[string]struct{}{} } + log.Debug().Msgf("owner %s, repo %s, branch %s", o.TargetOwner, o.TargetRepo, o.TargetBranch) + // Check if the branch exists and when it was modified if o.BranchTimestamp.IsZero() { branch := GetBranchTimestamp(o.TargetOwner, o.TargetRepo, o.TargetBranch, giteaRoot, giteaAPIToken, branchTimestampCache) @@ -79,7 +81,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaRoot, giteaAPIToken st return true } } - log.Debug().Msg("preparations") + log.Debug().Msg("preparations (in upstream.go)") // Make a GET request to the upstream URL uri := path.Join(o.TargetOwner, o.TargetRepo, "raw", o.TargetBranch, o.TargetPath)