enhance joinURL and return error on gitea client on start instead while running

This commit is contained in:
6543 2022-06-11 21:42:28 +02:00
parent 35b35c5d67
commit 1596ab9764
4 changed files with 23 additions and 18 deletions

View file

@ -82,7 +82,10 @@ func Serve(ctx *cli.Context) error {
// TODO: make this an MRU cache with a size limit
fileResponseCache := cache.NewKeyValueCache()
giteaClient := gitea.NewClient(giteaRoot, giteaAPIToken)
giteaClient, err := gitea.NewClient(giteaRoot, giteaAPIToken)
if err != nil {
return fmt.Errorf("could not create new gitea client: %v", err)
}
// Create handler based on settings
handler := server.Handler(mainDomainSuffix, []byte(rawDomain),

View file

@ -16,7 +16,7 @@ const giteaAPIRepos = "/api/v1/repos/"
var ErrorNotFound = errors.New("not found")
type Client struct {
giteaRoot string
giteaRoot *url.URL
giteaAPIToken string
fastClient *fasthttp.Client
infoTimeout time.Duration
@ -29,18 +29,22 @@ type FileResponse struct {
Body []byte
}
func joinURL(giteaRoot string, paths ...string) string { return giteaRoot + path.Join(paths...) }
func joinURL(baseURL *url.URL, paths ...string) string {
baseURL.Path = path.Join(append([]string{baseURL.Path}, paths...)...)
return baseURL.String()
}
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)
return &Client{
giteaRoot: giteaRoot,
giteaRoot: rootURL,
giteaAPIToken: giteaAPIToken,
infoTimeout: 5 * time.Second,
contentTimeout: 10 * time.Second,
fastClient: getFastHTTPClient(),
}
}, err
}
func (client *Client) GiteaRawContent(targetOwner, targetRepo, ref, resource string) ([]byte, error) {

View file

@ -1,23 +1,21 @@
package gitea
import (
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestJoinURL(t *testing.T) {
url := joinURL("")
assert.EqualValues(t, "", url)
baseURL, _ := url.Parse("")
u := joinURL(baseURL)
assert.EqualValues(t, "", u)
url = joinURL("", "", "")
assert.EqualValues(t, "", url)
u = joinURL(baseURL, "", "")
assert.EqualValues(t, "", u)
url = joinURL("http://wwow.url.com", "a", "b/c/", "d")
// assert.EqualValues(t, "http://wwow.url.com/a/b/c/d", url)
assert.EqualValues(t, "http://wwow.url.coma/b/c/d", url)
url = joinURL("h:://wrong", "acdc")
// assert.EqualValues(t, "h:://wrong/acdc", url)
assert.EqualValues(t, "h:://wrongacdc", url)
baseURL, _ = url.Parse("http://wwow.url.com")
u = joinURL(baseURL, "a", "b/c/", "d")
assert.EqualValues(t, "http://wwow.url.com/a/b/c/d", u)
}

View file

@ -13,7 +13,7 @@ import (
func TestHandlerPerformance(t *testing.T) {
giteaRoot := "https://codeberg.org"
giteaClient := gitea.NewClient(giteaRoot, "")
giteaClient, _ := gitea.NewClient(giteaRoot, "")
testHandler := Handler(
[]byte("codeberg.page"), []byte("raw.codeberg.org"),
giteaClient,