mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-12-02 18:45:29 +00:00
start refactor Upstream func
This commit is contained in:
parent
de4706bf58
commit
e6198e4ddd
@ -126,7 +126,7 @@ func Handler(mainDomainSuffix, rawDomain []byte,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to request the file from the Gitea API
|
// Try to request the file from the Gitea API
|
||||||
if !upstream.Upstream(ctx, targetOwner, targetRepo, targetBranch, targetPath, giteaRoot, giteaApiToken, targetOptions, branchTimestampCache, fileResponseCache) {
|
if !targetOptions.Upstream(ctx, targetOwner, targetRepo, targetBranch, targetPath, giteaRoot, giteaApiToken, branchTimestampCache, fileResponseCache) {
|
||||||
html.ReturnErrorPage(ctx, ctx.Response.StatusCode())
|
html.ReturnErrorPage(ctx, ctx.Response.StatusCode())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ func CheckCanonicalDomain(targetOwner, targetRepo, targetBranch, actualDomain, m
|
|||||||
req.SetRequestURI(giteaRoot + "/api/v1/repos/" + targetOwner + "/" + targetRepo + "/raw/" + targetBranch + "/.domains" + "?access_token=" + giteaApiToken)
|
req.SetRequestURI(giteaRoot + "/api/v1/repos/" + targetOwner + "/" + targetRepo + "/raw/" + targetBranch + "/.domains" + "?access_token=" + giteaApiToken)
|
||||||
res := fasthttp.AcquireResponse()
|
res := fasthttp.AcquireResponse()
|
||||||
|
|
||||||
err := Client.Do(req, res)
|
err := client.Do(req, res)
|
||||||
if err == nil && res.StatusCode() == fasthttp.StatusOK {
|
if err == nil && res.StatusCode() == fasthttp.StatusOK {
|
||||||
for _, domain := range strings.Split(string(res.Body()), "\n") {
|
for _, domain := range strings.Split(string(res.Body()), "\n") {
|
||||||
domain = strings.ToLower(domain)
|
domain = strings.ToLower(domain)
|
||||||
|
@ -10,7 +10,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
|
|
||||||
@ -33,7 +32,7 @@ type Options struct {
|
|||||||
BranchTimestamp time.Time
|
BranchTimestamp time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
var Client = fasthttp.Client{
|
var client = fasthttp.Client{
|
||||||
ReadTimeout: 10 * time.Second,
|
ReadTimeout: 10 * time.Second,
|
||||||
MaxConnDuration: 60 * time.Second,
|
MaxConnDuration: 60 * time.Second,
|
||||||
MaxConnWaitTimeout: 1000 * time.Millisecond,
|
MaxConnWaitTimeout: 1000 * time.Millisecond,
|
||||||
@ -41,7 +40,7 @@ var Client = fasthttp.Client{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Upstream requests a file from the Gitea API at GiteaRoot and writes it to the request context.
|
// Upstream requests a file from the Gitea API at GiteaRoot and writes it to the request context.
|
||||||
func Upstream(ctx *fasthttp.RequestCtx, targetOwner, targetRepo, targetBranch, targetPath, giteaRoot, giteaApiToken string, options *Options, branchTimestampCache, fileResponseCache cache.SetGetKey) (final bool) {
|
func (options *Options) Upstream(ctx *fasthttp.RequestCtx, targetOwner, targetRepo, targetBranch, targetPath, giteaRoot, giteaApiToken string, branchTimestampCache, fileResponseCache cache.SetGetKey) (final bool) {
|
||||||
log := log.With().Strs("upstream", []string{targetOwner, targetRepo, targetBranch, targetPath}).Logger()
|
log := log.With().Strs("upstream", []string{targetOwner, targetRepo, targetBranch, targetPath}).Logger()
|
||||||
|
|
||||||
if options.ForbiddenMimeTypes == nil {
|
if options.ForbiddenMimeTypes == nil {
|
||||||
@ -87,7 +86,7 @@ func Upstream(ctx *fasthttp.RequestCtx, targetOwner, targetRepo, targetBranch, t
|
|||||||
req.SetRequestURI(giteaRoot + "/api/v1/repos/" + uri + "?access_token=" + giteaApiToken)
|
req.SetRequestURI(giteaRoot + "/api/v1/repos/" + uri + "?access_token=" + giteaApiToken)
|
||||||
res = fasthttp.AcquireResponse()
|
res = fasthttp.AcquireResponse()
|
||||||
res.SetBodyStream(&strings.Reader{}, -1)
|
res.SetBodyStream(&strings.Reader{}, -1)
|
||||||
err = Client.Do(req, res)
|
err = client.Do(req, res)
|
||||||
}
|
}
|
||||||
log.Debug().Msg("acquisition")
|
log.Debug().Msg("acquisition")
|
||||||
|
|
||||||
@ -99,7 +98,7 @@ func Upstream(ctx *fasthttp.RequestCtx, targetOwner, targetRepo, targetBranch, t
|
|||||||
optionsForIndexPages.TryIndexPages = false
|
optionsForIndexPages.TryIndexPages = false
|
||||||
optionsForIndexPages.AppendTrailingSlash = true
|
optionsForIndexPages.AppendTrailingSlash = true
|
||||||
for _, indexPage := range upstreamIndexPages {
|
for _, indexPage := range upstreamIndexPages {
|
||||||
if Upstream(ctx, targetOwner, targetRepo, targetBranch, strings.TrimSuffix(targetPath, "/")+"/"+indexPage, giteaRoot, giteaApiToken, &optionsForIndexPages, branchTimestampCache, fileResponseCache) {
|
if optionsForIndexPages.Upstream(ctx, targetOwner, targetRepo, targetBranch, strings.TrimSuffix(targetPath, "/")+"/"+indexPage, giteaRoot, giteaApiToken, branchTimestampCache, fileResponseCache) {
|
||||||
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(options.BranchTimestamp.Unix(), 10), fileResponse{
|
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(options.BranchTimestamp.Unix(), 10), fileResponse{
|
||||||
exists: false,
|
exists: false,
|
||||||
}, FileCacheTimeout)
|
}, FileCacheTimeout)
|
||||||
@ -109,7 +108,7 @@ func Upstream(ctx *fasthttp.RequestCtx, targetOwner, targetRepo, targetBranch, t
|
|||||||
// compatibility fix for GitHub Pages (/example → /example.html)
|
// compatibility fix for GitHub Pages (/example → /example.html)
|
||||||
optionsForIndexPages.AppendTrailingSlash = false
|
optionsForIndexPages.AppendTrailingSlash = false
|
||||||
optionsForIndexPages.RedirectIfExists = string(ctx.Request.URI().Path()) + ".html"
|
optionsForIndexPages.RedirectIfExists = string(ctx.Request.URI().Path()) + ".html"
|
||||||
if Upstream(ctx, targetOwner, targetRepo, targetBranch, targetPath+".html", giteaRoot, giteaApiToken, &optionsForIndexPages, branchTimestampCache, fileResponseCache) {
|
if optionsForIndexPages.Upstream(ctx, targetOwner, targetRepo, targetBranch, targetPath+".html", giteaRoot, giteaApiToken, branchTimestampCache, fileResponseCache) {
|
||||||
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(options.BranchTimestamp.Unix(), 10), fileResponse{
|
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(options.BranchTimestamp.Unix(), 10), fileResponse{
|
||||||
exists: false,
|
exists: false,
|
||||||
}, FileCacheTimeout)
|
}, FileCacheTimeout)
|
||||||
|
Loading…
Reference in New Issue
Block a user