diff --git a/server/gitea/cache.go b/server/gitea/cache.go new file mode 100644 index 0000000..932ff3c --- /dev/null +++ b/server/gitea/cache.go @@ -0,0 +1,12 @@ +package gitea + +type FileResponse struct { + Exists bool + ETag []byte + MimeType string + Body []byte +} + +func (f FileResponse) IsEmpty() bool { + return len(f.Body) != 0 +} diff --git a/server/gitea/client.go b/server/gitea/client.go index 26cafa4..68782a8 100644 --- a/server/gitea/client.go +++ b/server/gitea/client.go @@ -2,34 +2,6 @@ package gitea import ( "errors" - "strings" ) -const giteaAPIRepos = "/api/v1/repos/" - var ErrorNotFound = errors.New("not found") - -type FileResponse struct { - Exists bool - ETag []byte - MimeType string - Body []byte -} - -// TODO: once golang v1.19 is min requirement, we can switch to 'JoinPath()' of 'net/url' package -func joinURL(baseURL string, paths ...string) string { - p := make([]string, 0, len(paths)) - for i := range paths { - path := strings.TrimSpace(paths[i]) - path = strings.Trim(path, "/") - if len(path) != 0 { - p = append(p, path) - } - } - - return baseURL + "/" + strings.Join(p, "/") -} - -func (f FileResponse) IsEmpty() bool { - return len(f.Body) != 0 -} diff --git a/server/gitea/client_fasthttp.go b/server/gitea/client_fasthttp.go index c667e0a..5f95ba4 100644 --- a/server/gitea/client_fasthttp.go +++ b/server/gitea/client_fasthttp.go @@ -10,8 +10,12 @@ import ( "github.com/valyala/fasthttp" "github.com/valyala/fastjson" + + "codeberg.org/codeberg/pages/server/cache" ) +const giteaAPIRepos = "/api/v1/repos/" + type Client struct { giteaRoot string giteaAPIToken string @@ -36,24 +40,15 @@ func NewClient(giteaRoot, giteaAPIToken string, fileResponseCache cache.SetGetKe } func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) { - url := joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref)) - res, err := client.do(client.contentTimeout, url) + resp, err := client.ServeRawContent(targetOwner, targetRepo, ref, resource) if err != nil { return nil, err } - - switch res.StatusCode() { - case fasthttp.StatusOK: - return res.Body(), nil - case fasthttp.StatusNotFound: - return nil, ErrorNotFound - default: - return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode()) - } + return resp.Body(), nil } -func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) { - url := joinURL(client.giteaRoot, giteaAPIRepos, uri) +func (client *Client) ServeRawContent(targetOwner, targetRepo, ref, resource string) (*fasthttp.Response, error) { + url := joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref)) res, err := client.do(client.contentTimeout, url) if err != nil { return nil, err @@ -104,3 +99,17 @@ func (client *Client) do(timeout time.Duration, url string) (*fasthttp.Response, return res, err } + +// TODO: once golang v1.19 is min requirement, we can switch to 'JoinPath()' of 'net/url' package +func joinURL(baseURL string, paths ...string) string { + p := make([]string, 0, len(paths)) + for i := range paths { + path := strings.TrimSpace(paths[i]) + path = strings.Trim(path, "/") + if len(path) != 0 { + p = append(p, path) + } + } + + return baseURL + "/" + strings.Join(p, "/") +} diff --git a/server/gitea/client_test.go b/server/gitea/client_fasthttp_test.go similarity index 100% rename from server/gitea/client_test.go rename to server/gitea/client_fasthttp_test.go diff --git a/server/gitea/client_std.go b/server/gitea/client_std.go index 4bfc480..12645f7 100644 --- a/server/gitea/client_std.go +++ b/server/gitea/client_std.go @@ -12,6 +12,7 @@ import ( "time" "code.gitea.io/sdk/gitea" + "codeberg.org/codeberg/pages/server/cache" ) diff --git a/server/upstream/helper.go b/server/upstream/helper.go index 5bbe833..0714dcd 100644 --- a/server/upstream/helper.go +++ b/server/upstream/helper.go @@ -67,6 +67,10 @@ func (o *Options) generateUri() string { return path.Join(o.TargetOwner, o.TargetRepo, "raw", o.TargetBranch, o.TargetPath) } +func (o *Options) generateUriClientArgs() (targetOwner, targetRepo, ref, resource string) { + return o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath +} + func (o *Options) timestamp() string { return strconv.FormatInt(o.BranchTimestamp.Unix(), 10) } diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index 9b7464e..6ca7b4c 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -80,7 +80,7 @@ func (o *Options) Upstream(ctx *fasthttp.RequestCtx, giteaClient *gitea.Client, if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + o.timestamp()); ok && !cachedValue.(gitea.FileResponse).IsEmpty() { cachedResponse = cachedValue.(gitea.FileResponse) } else { - res, err = giteaClient.ServeRawContent(uri) + res, err = giteaClient.ServeRawContent(o.generateUriClientArgs()) } log.Debug().Msg("acquisition")