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 // TODO: make this an MRU cache with a size limit
fileResponseCache := cache.NewKeyValueCache() 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 // Create handler based on settings
handler := server.Handler(mainDomainSuffix, []byte(rawDomain), handler := server.Handler(mainDomainSuffix, []byte(rawDomain),

View file

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

View file

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

View file

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