mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-01-19 17:07:54 +00:00
fasthttp
This commit is contained in:
parent
a8afb372dd
commit
33298aa8ff
3 changed files with 37 additions and 15 deletions
|
@ -87,19 +87,42 @@ func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource str
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (time.Time, error) {
|
func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (*BranchTimestamp, error) {
|
||||||
|
cacheKey := fmt.Sprintf("%s/%s/%s/%s", branchTimestampCacheKeyPrefix, repoOwner, repoName, branchName)
|
||||||
|
|
||||||
|
if stamp, ok := client.responseCache.Get(cacheKey); ok && stamp != nil {
|
||||||
|
return stamp.(*BranchTimestamp), nil
|
||||||
|
}
|
||||||
|
|
||||||
url := joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName)
|
url := joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName)
|
||||||
res, err := client.do(client.infoTimeout, url)
|
res, err := client.do(client.infoTimeout, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Time{}, err
|
return &BranchTimestamp{}, err
|
||||||
}
|
}
|
||||||
if res.StatusCode() != fasthttp.StatusOK {
|
if res.StatusCode() != fasthttp.StatusOK {
|
||||||
return time.Time{}, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
return &BranchTimestamp{}, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
||||||
}
|
}
|
||||||
return time.Parse(time.RFC3339, fastjson.GetString(res.Body(), "commit", "timestamp"))
|
timestamp, err := time.Parse(time.RFC3339, fastjson.GetString(res.Body(), "commit", "timestamp"))
|
||||||
|
if err != nil {
|
||||||
|
return &BranchTimestamp{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
stamp := &BranchTimestamp{
|
||||||
|
Branch: branchName,
|
||||||
|
Timestamp: timestamp,
|
||||||
|
}
|
||||||
|
|
||||||
|
client.responseCache.Set(cacheKey, stamp, branchExistenceCacheTimeout)
|
||||||
|
return stamp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (string, error) {
|
func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (string, error) {
|
||||||
|
cacheKey := fmt.Sprintf("%s/%s/%s", defaultBranchCacheKeyPrefix, repoOwner, repoName)
|
||||||
|
|
||||||
|
if branch, ok := client.responseCache.Get(cacheKey); ok && branch != nil {
|
||||||
|
return branch.(string), nil
|
||||||
|
}
|
||||||
|
|
||||||
url := joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName)
|
url := joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName)
|
||||||
res, err := client.do(client.infoTimeout, url)
|
res, err := client.do(client.infoTimeout, url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -108,7 +131,10 @@ func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (str
|
||||||
if res.StatusCode() != fasthttp.StatusOK {
|
if res.StatusCode() != fasthttp.StatusOK {
|
||||||
return "", fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
return "", fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
||||||
}
|
}
|
||||||
return fastjson.GetString(res.Body(), "default_branch"), nil
|
|
||||||
|
branch := fastjson.GetString(res.Body(), "default_branch")
|
||||||
|
client.responseCache.Set(cacheKey, branch, defaultBranchCacheTimeout)
|
||||||
|
return branch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) do(timeout time.Duration, url string) (*fasthttp.Response, error) {
|
func (client *Client) do(timeout time.Duration, url string) (*fasthttp.Response, error) {
|
||||||
|
|
|
@ -45,7 +45,7 @@ func tryUpstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client,
|
||||||
targetOptions.TargetPath = targetPath
|
targetOptions.TargetPath = targetPath
|
||||||
|
|
||||||
// Try to request the file from the Gitea API
|
// Try to request the file from the Gitea API
|
||||||
if !targetOptions.Upstream(ctx, giteaClient, branchTimestampCache) {
|
if !targetOptions.Upstream(ctx, giteaClient) {
|
||||||
html.ReturnErrorPage(ctx, ctx.Response.StatusCode())
|
html.ReturnErrorPage(ctx, ctx.Response.StatusCode())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -43,7 +42,7 @@ type Options struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 (o *Options) Upstream(ctx *http.Response, giteaClient *gitea.Client) (final bool) {
|
func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client) (final bool) {
|
||||||
log := log.With().Strs("upstream", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath}).Logger()
|
log := log.With().Strs("upstream", []string{o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath}).Logger()
|
||||||
|
|
||||||
// Check if the branch exists and when it was modified
|
// Check if the branch exists and when it was modified
|
||||||
|
@ -74,14 +73,11 @@ func (o *Options) Upstream(ctx *http.Response, giteaClient *gitea.Client) (final
|
||||||
|
|
||||||
// Make a GET request to the upstream URL
|
// Make a GET request to the upstream URL
|
||||||
uri := o.generateUri()
|
uri := o.generateUri()
|
||||||
var res *fasthttp.Response
|
|
||||||
var cachedResponse gitea.FileResponse
|
var cachedResponse gitea.FileResponse
|
||||||
var err error
|
// if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + o.timestamp()); ok && !cachedValue.(gitea.FileResponse).IsEmpty() {
|
||||||
if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + o.timestamp()); ok && !cachedValue.(gitea.FileResponse).IsEmpty() {
|
// cachedResponse = cachedValue.(gitea.FileResponse)
|
||||||
cachedResponse = cachedValue.(gitea.FileResponse)
|
// } else {
|
||||||
} else {
|
res, err := giteaClient.ServeRawContent(o.generateUriClientArgs())
|
||||||
res, err = giteaClient.ServeRawContent(o.generateUriClientArgs())
|
|
||||||
}
|
|
||||||
log.Debug().Msg("acquisition")
|
log.Debug().Msg("acquisition")
|
||||||
|
|
||||||
// Handle errors
|
// Handle errors
|
||||||
|
|
Loading…
Reference in a new issue