mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-24 22:06:57 +00:00
dedup code
This commit is contained in:
parent
d1d27f01c7
commit
7eeafdbc5f
1 changed files with 40 additions and 43 deletions
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
|
@ -19,6 +18,8 @@ var ErrorNotFound = errors.New("not found")
|
||||||
type Client struct {
|
type Client struct {
|
||||||
giteaRoot string
|
giteaRoot string
|
||||||
giteaAPIToken string
|
giteaAPIToken string
|
||||||
|
infoTimeout time.Duration
|
||||||
|
contentTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileResponse struct {
|
type FileResponse struct {
|
||||||
|
@ -34,23 +35,19 @@ func (f FileResponse) IsEmpty() bool { return len(f.Body) != 0 }
|
||||||
func NewClient(giteaRoot, giteaAPIToken string) *Client {
|
func NewClient(giteaRoot, giteaAPIToken string) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
giteaRoot: giteaRoot,
|
giteaRoot: giteaRoot,
|
||||||
giteaAPIToken: "token " + giteaAPIToken,
|
giteaAPIToken: giteaAPIToken,
|
||||||
|
infoTimeout: 5 * time.Second,
|
||||||
|
contentTimeout: 10 * time.Second,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODOs:
|
|
||||||
// * handle 404 -> page will show 500 atm
|
|
||||||
|
|
||||||
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {
|
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {
|
||||||
req := fasthttp.AcquireRequest()
|
url := joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref))
|
||||||
|
res, err := client.do(client.contentTimeout, url)
|
||||||
req.SetRequestURI(joinURL(client.giteaRoot, giteaAPIRepos, targetOwner, targetRepo, "raw", resource+"?ref="+url.QueryEscape(ref)))
|
if err != nil {
|
||||||
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
|
|
||||||
res := fasthttp.AcquireResponse()
|
|
||||||
|
|
||||||
if err := getFastHTTPClient(10*time.Second).Do(req, res); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch res.StatusCode() {
|
switch res.StatusCode() {
|
||||||
case fasthttp.StatusOK:
|
case fasthttp.StatusOK:
|
||||||
return res.Body(), nil
|
return res.Body(), nil
|
||||||
|
@ -62,38 +59,31 @@ func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource str
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) {
|
func (client *Client) ServeRawContent(uri string) (*fasthttp.Response, error) {
|
||||||
fastClient := getFastHTTPClient(10 * time.Second)
|
url := joinURL(client.giteaRoot, giteaAPIRepos, uri)
|
||||||
|
res, err := client.do(client.contentTimeout, url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// resp.SetBodyStream(&strings.Reader{}, -1)
|
||||||
|
|
||||||
req := fasthttp.AcquireRequest()
|
if err != nil {
|
||||||
req.SetRequestURI(joinURL(client.giteaRoot, giteaAPIRepos, uri))
|
|
||||||
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
|
|
||||||
|
|
||||||
resp := fasthttp.AcquireResponse()
|
|
||||||
resp.SetBodyStream(&strings.Reader{}, -1)
|
|
||||||
|
|
||||||
if err := fastClient.Do(req, resp); err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch resp.StatusCode() {
|
switch res.StatusCode() {
|
||||||
case fasthttp.StatusOK:
|
case fasthttp.StatusOK:
|
||||||
return resp, nil
|
return res, nil
|
||||||
case fasthttp.StatusNotFound:
|
case fasthttp.StatusNotFound:
|
||||||
return nil, ErrorNotFound
|
return nil, ErrorNotFound
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unexpected status code '%d'", resp.StatusCode())
|
return nil, fmt.Errorf("unexpected status code '%d'", res.StatusCode())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (time.Time, error) {
|
func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchName string) (time.Time, error) {
|
||||||
fastClient := getFastHTTPClient(5 * time.Second)
|
url := joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName)
|
||||||
|
res, err := client.do(client.infoTimeout, url)
|
||||||
req := fasthttp.AcquireRequest()
|
if err != nil {
|
||||||
req.SetRequestURI(joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName, "branches", branchName))
|
|
||||||
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
|
|
||||||
res := fasthttp.AcquireResponse()
|
|
||||||
|
|
||||||
if err := fastClient.Do(req, res); err != nil {
|
|
||||||
return time.Time{}, err
|
return time.Time{}, err
|
||||||
}
|
}
|
||||||
if res.StatusCode() != fasthttp.StatusOK {
|
if res.StatusCode() != fasthttp.StatusOK {
|
||||||
|
@ -103,14 +93,9 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (string, error) {
|
func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (string, error) {
|
||||||
fastClient := getFastHTTPClient(5 * time.Second)
|
url := joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName)
|
||||||
|
res, err := client.do(client.infoTimeout, url)
|
||||||
req := fasthttp.AcquireRequest()
|
if err != nil {
|
||||||
req.SetRequestURI(joinURL(client.giteaRoot, giteaAPIRepos, repoOwner, repoName))
|
|
||||||
req.Header.Set(fasthttp.HeaderAuthorization, client.giteaAPIToken)
|
|
||||||
res := fasthttp.AcquireResponse()
|
|
||||||
|
|
||||||
if err := fastClient.Do(req, res); err != nil {
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if res.StatusCode() != fasthttp.StatusOK {
|
if res.StatusCode() != fasthttp.StatusOK {
|
||||||
|
@ -118,3 +103,15 @@ func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (str
|
||||||
}
|
}
|
||||||
return fastjson.GetString(res.Body(), "default_branch"), nil
|
return fastjson.GetString(res.Body(), "default_branch"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (client *Client) do(timeout time.Duration, url string) (*fasthttp.Response, error) {
|
||||||
|
req := fasthttp.AcquireRequest()
|
||||||
|
|
||||||
|
req.SetRequestURI(url)
|
||||||
|
req.Header.Set(fasthttp.HeaderAuthorization, "token "+client.giteaAPIToken)
|
||||||
|
res := fasthttp.AcquireResponse()
|
||||||
|
|
||||||
|
err := getFastHTTPClient(timeout).Do(req, res)
|
||||||
|
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue