mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2024-11-23 06:18:59 +00:00
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:
parent
913f762eb0
commit
cc32bab31f
@ -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),
|
||||||
|
@ -4,7 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/valyala/fasthttp"
|
"github.com/valyala/fasthttp"
|
||||||
@ -29,18 +29,33 @@ type FileResponse struct {
|
|||||||
Body []byte
|
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 (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{
|
return &Client{
|
||||||
giteaRoot: giteaRoot,
|
giteaRoot: giteaRoot,
|
||||||
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) {
|
||||||
|
@ -1,23 +1,23 @@
|
|||||||
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 := ""
|
||||||
assert.EqualValues(t, "", url)
|
assert.EqualValues(t, "/", joinURL(baseURL))
|
||||||
|
assert.EqualValues(t, "/", joinURL(baseURL, "", ""))
|
||||||
|
|
||||||
url = joinURL("", "", "")
|
baseURL = "http://wwow.url.com"
|
||||||
assert.EqualValues(t, "", url)
|
assert.EqualValues(t, "http://wwow.url.com/a/b/c/d", joinURL(baseURL, "a", "b/c/", "d"))
|
||||||
|
|
||||||
url = joinURL("http://wwow.url.com", "a", "b/c/", "d")
|
baseURL = "http://wow.url.com/subpath/2"
|
||||||
// assert.EqualValues(t, "http://wwow.url.com/a/b/c/d", url)
|
assert.EqualValues(t, "http://wow.url.com/subpath/2/content.pdf", joinURL(baseURL, "/content.pdf"))
|
||||||
assert.EqualValues(t, "http://wwow.url.coma/b/c/d", url)
|
assert.EqualValues(t, "http://wow.url.com/subpath/2/wonderful.jpg", joinURL(baseURL, "wonderful.jpg"))
|
||||||
|
assert.EqualValues(t, "http://wow.url.com/subpath/2/raw/wonderful.jpg?ref=main", joinURL(baseURL, "raw", "wonderful.jpg"+"?ref="+url.QueryEscape("main")))
|
||||||
url = joinURL("h:://wrong", "acdc")
|
assert.EqualValues(t, "http://wow.url.com/subpath/2/raw/wonderful.jpg%3Fref=main", joinURL(baseURL, "raw", "wonderful.jpg%3Fref=main"))
|
||||||
// assert.EqualValues(t, "h:://wrong/acdc", url)
|
|
||||||
assert.EqualValues(t, "h:://wrongacdc", url)
|
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
|
Loading…
Reference in New Issue
Block a user