Enhance joinURL and return error on gitea client on start instead while running (#88)

Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/88
This commit is contained in:
6543 2022-06-13 20:07:32 +02:00
parent 913f762eb0
commit cc32bab31f
4 changed files with 35 additions and 17 deletions

View file

@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"net/url"
"path"
"strings"
"time"
"github.com/valyala/fasthttp"
@ -29,18 +29,33 @@ type FileResponse struct {
Body []byte
}
func joinURL(giteaRoot string, paths ...string) string { return giteaRoot + path.Join(paths...) }
// 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 }
func NewClient(giteaRoot, giteaAPIToken string) *Client {
func NewClient(giteaRoot, giteaAPIToken string) (*Client, error) {
rootURL, err := url.Parse(giteaRoot)
giteaRoot = strings.Trim(rootURL.String(), "/")
return &Client{
giteaRoot: giteaRoot,
giteaAPIToken: giteaAPIToken,
infoTimeout: 5 * time.Second,
contentTimeout: 10 * time.Second,
fastClient: getFastHTTPClient(),
}
}, err
}
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {