From 361639db688a77a3279c8c1f9c6189fc84be855c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 21 Jul 2022 21:41:56 +0200 Subject: [PATCH 1/2] split to move cache logic into it later --- server/gitea/cache.go | 12 ++++++++++++ server/gitea/client.go | 9 --------- 2 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 server/gitea/cache.go 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 3b9ad6f..943be1f 100644 --- a/server/gitea/client.go +++ b/server/gitea/client.go @@ -23,13 +23,6 @@ type Client struct { contentTimeout time.Duration } -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)) @@ -44,8 +37,6 @@ func joinURL(baseURL string, paths ...string) string { return baseURL + "/" + strings.Join(p, "/") } -func (f FileResponse) IsEmpty() bool { return len(f.Body) != 0 } - func NewClient(giteaRoot, giteaAPIToken string) (*Client, error) { rootURL, err := url.Parse(giteaRoot) giteaRoot = strings.Trim(rootURL.String(), "/") From 61b959a93b1de987679a6d515d2db413c5fba5a1 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 21 Jul 2022 21:53:22 +0200 Subject: [PATCH 2/2] refactor client api --- server/gitea/client.go | 17 ++++------------- server/upstream/helper.go | 4 ++++ server/upstream/upstream.go | 2 +- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/server/gitea/client.go b/server/gitea/client.go index 943be1f..b918235 100644 --- a/server/gitea/client.go +++ b/server/gitea/client.go @@ -51,24 +51,15 @@ func NewClient(giteaRoot, giteaAPIToken string) (*Client, error) { } 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 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")