fix lint issues and tests

This commit is contained in:
6543 2022-08-28 16:53:30 +02:00
parent dc023b81a8
commit ea13ac0e92
No known key found for this signature in database
GPG key ID: C99B82E40B027BAE
7 changed files with 27 additions and 33 deletions

View file

@ -146,8 +146,7 @@ func Serve(ctx *cli.Context) error {
// Start the web fastServer
log.Info().Msgf("Start listening on %s", listener.Addr())
http.Serve(listener, fastServer)
if err != nil {
if err := http.Serve(listener, fastServer); err != nil {
log.Panic().Err(err).Msg("Couldn't start fastServer")
}

View file

@ -19,5 +19,5 @@ func ReturnErrorPage(ctx *context.Context, msg string, code int) {
msg = errorBody(code)
}
io.Copy(ctx.RespWriter, strings.NewReader(msg))
_, _ = io.Copy(ctx.RespWriter, strings.NewReader(msg))
}

View file

@ -42,7 +42,7 @@ func (c *Context) String(raw string, status ...int) {
code = status[0]
}
c.RespWriter.WriteHeader(code)
io.Copy(c.RespWriter, strings.NewReader(raw))
_, _ = io.Copy(c.RespWriter, strings.NewReader(raw))
}
func (c *Context) IsMethod(m string) bool {

View file

@ -30,8 +30,8 @@ var (
// fileCacheTimeout specifies the timeout for the file content cache - you might want to make this quite long, depending
// on your available memory.
// TODO: move as option into cache interface
fileCacheTimeout = 5 * time.Minute
// fileCacheTimeout = 5 * time.Minute
// fileCacheSizeLimit limits the maximum file size that will be cached, and is set to 1 MB by default.
fileCacheSizeLimit = 1024 * 1024
// fileCacheSizeLimit = 1024 * 1024
)

View file

@ -139,7 +139,9 @@ func (client *Client) GiteaGetRepoBranchTimestamp(repoOwner, repoName, branchNam
Timestamp: branch.Commit.Timestamp,
}
client.responseCache.Set(cacheKey, stamp, branchExistenceCacheTimeout)
if err := client.responseCache.Set(cacheKey, stamp, branchExistenceCacheTimeout); err != nil {
log.Error().Err(err).Msgf("error on store of repo branch timestamp [%s/%s@%s]", repoOwner, repoName, branchName)
}
return stamp, nil
}
@ -159,6 +161,8 @@ func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (str
}
branch := repo.DefaultBranch
client.responseCache.Set(cacheKey, branch, defaultBranchCacheTimeout)
if err := client.responseCache.Set(cacheKey, branch, defaultBranchCacheTimeout); err != nil {
log.Error().Err(err).Msgf("error on store of repo default branch [%s/%s]", repoOwner, repoName)
}
return branch, nil
}

View file

@ -3,42 +3,42 @@
package server
import (
"fmt"
"net/http/httptest"
"testing"
"time"
"github.com/valyala/fasthttp"
"codeberg.org/codeberg/pages/server/cache"
"codeberg.org/codeberg/pages/server/gitea"
"github.com/rs/zerolog/log"
)
func TestHandlerPerformance(t *testing.T) {
giteaRoot := "https://codeberg.org"
giteaClient, _ := gitea.NewClient(giteaRoot, "", cache.NewKeyValueCache(), false, false)
testHandler := Handler(
[]byte("codeberg.page"), []byte("raw.codeberg.org"),
"codeberg.page", "raw.codeberg.org",
giteaClient,
giteaRoot, "https://docs.codeberg.org/pages/raw-content/",
[][]byte{[]byte("/.well-known/acme-challenge/")},
[][]byte{[]byte("raw.codeberg.org"), []byte("fonts.codeberg.org"), []byte("design.codeberg.org")},
[]string{"/.well-known/acme-challenge/"},
[]string{"raw.codeberg.org", "fonts.codeberg.org", "design.codeberg.org"},
cache.NewKeyValueCache(),
cache.NewKeyValueCache(),
)
testCase := func(uri string, status int) {
ctx := &fasthttp.RequestCtx{
Request: *fasthttp.AcquireRequest(),
Response: *fasthttp.AcquireResponse(),
}
ctx.Request.SetRequestURI(uri)
fmt.Printf("Start: %v\n", time.Now())
req := httptest.NewRequest("GET", uri, nil)
w := httptest.NewRecorder()
log.Printf("Start: %v\n", time.Now())
start := time.Now()
testHandler(ctx)
testHandler(w, req)
end := time.Now()
fmt.Printf("Done: %v\n", time.Now())
if ctx.Response.StatusCode() != status {
t.Errorf("request failed with status code %d", ctx.Response.StatusCode())
log.Printf("Done: %v\n", time.Now())
resp := w.Result()
if resp.StatusCode != status {
t.Errorf("request failed with status code %d", resp.StatusCode)
} else {
t.Logf("request took %d milliseconds", end.Sub(start).Milliseconds())
}

View file

@ -3,7 +3,6 @@ package upstream
import (
"mime"
"path"
"strconv"
"strings"
"codeberg.org/codeberg/pages/server/gitea"
@ -44,14 +43,6 @@ func (o *Options) getMimeTypeByExtension() string {
return mimeType
}
func (o *Options) generateUri() string {
return path.Join(o.TargetOwner, o.TargetRepo, "raw", o.TargetBranch, o.TargetPath)
}
func (o *Options) generateUriClientArgs() (targetOwner, targetRepo, ref, resource string) {
return o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath
}
func (o *Options) timestamp() string {
return strconv.FormatInt(o.BranchTimestamp.Unix(), 10)
}