deduplicate

This commit is contained in:
6543 2022-11-12 14:08:08 +01:00
parent 5f2f073a73
commit 8e6c0c6c9f
No known key found for this signature in database
GPG key ID: C99B82E40B027BAE

View file

@ -88,6 +88,7 @@ func Handler(mainDomainSuffix, rawDomain string,
}
// Prepare request information to Gitea
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
targetOptions := &upstream.Options{
TryIndexPages: true,
}
@ -100,25 +101,24 @@ func Handler(mainDomainSuffix, rawDomain string,
targetOptions.TryIndexPages = false
targetOptions.ServeRaw = true
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
if len(pathElements) < 2 {
// https://{RawDomain}/{owner}/{repo}[/@{branch}]/{path} is required
ctx.Redirect(rawInfoPage, http.StatusTemporaryRedirect)
return
}
targetOwner := pathElements[0]
targetRepo := pathElements[1]
// raw.codeberg.org/example/myrepo/@main/index.html
if len(pathElements) > 2 && strings.HasPrefix(pathElements[2], "@") {
log.Debug().Msg("raw domain preparations, now trying with specified branch")
newPath := path.Join(pathElements[3:]...)
branch := pathElements[2][1:]
if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, branch, newPath, true); works {
repoOwner := pathElements[0]
repo := pathElements[1]
if timestampBranch, works := tryBranch(log, ctx, giteaClient, repoOwner, repo, branch, newPath, true); works {
targetOptions.BranchTimestamp = timestampBranch.Timestamp
log.Debug().Msg("tryBranch, now trying upstream 1")
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, timestampBranch.Branch, newPath, canonicalDomainCache)
targetOptions, repoOwner, repo, timestampBranch.Branch, newPath, canonicalDomainCache)
return
}
log.Debug().Msg("missing branch info")
@ -127,22 +127,24 @@ func Handler(mainDomainSuffix, rawDomain string,
}
log.Debug().Msg("raw domain preparations, now trying with default branch")
repoOwner := pathElements[0]
repo := pathElements[1]
newPath := path.Join(pathElements[2:]...)
if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, "", newPath, true); works {
if timestampBranch, works := tryBranch(log, ctx, giteaClient, repoOwner, repo, "", newPath, true); works {
targetOptions.BranchTimestamp = timestampBranch.Timestamp
log.Debug().Msg("tryBranch, now trying upstream 2")
tryUpstream(ctx, giteaClient, mainDomainSuffix, trimmedHost,
targetOptions, targetOwner, targetRepo, timestampBranch.Branch, newPath, canonicalDomainCache)
} else {
log.Error().Msg("TODO: is this a bug?")
targetOptions, repoOwner, repo, timestampBranch.Branch, newPath, canonicalDomainCache)
return
}
html.ReturnErrorPage(ctx, fmt.Sprintf("raw domain could not find repo '%s/%s' or repo is empty", repoOwner, repo), http.StatusNotFound)
return
} else if strings.HasSuffix(trimmedHost, mainDomainSuffix) {
// Serve pages from subdomains of MainDomainSuffix
log.Debug().Msg("main domain suffix")
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
targetOwner := strings.TrimSuffix(trimmedHost, mainDomainSuffix)
targetRepo := pathElements[0]
@ -245,16 +247,16 @@ func Handler(mainDomainSuffix, rawDomain string,
return
}
pathElements := strings.Split(strings.Trim(ctx.Path(), "/"), "/")
pathParts := pathElements
canonicalLink := false
if strings.HasPrefix(pathElements[0], "@") {
targetBranch = pathElements[0][1:]
pathElements = pathElements[1:]
pathParts = pathElements[1:]
canonicalLink = true
}
// Try to use the given repo on the given branch or the default branch
newPath := path.Join(pathElements...)
newPath := path.Join(pathParts...)
log.Debug().Msg("custom domain preparations, now trying with details from DNS")
if timestampBranch, works := tryBranch(log, ctx, giteaClient, targetOwner, targetRepo, targetBranch, newPath, canonicalLink); works {
targetOptions.BranchTimestamp = timestampBranch.Timestamp