mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-05 14:07:01 +00:00
deduplicate
This commit is contained in:
parent
5f2f073a73
commit
8e6c0c6c9f
1 changed files with 15 additions and 13 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue