From ea13ac0e927ae54cb833f110736606ccce85ef39 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 28 Aug 2022 16:53:30 +0200 Subject: [PATCH] fix lint issues and tests --- cmd/main.go | 3 +-- html/error_std.go | 2 +- server/context/context.go | 2 +- server/gitea/cache.go | 4 ++-- server/gitea/client_std.go | 8 ++++++-- server/handler_test.go | 32 ++++++++++++++++---------------- server/upstream/helper.go | 9 --------- 7 files changed, 27 insertions(+), 33 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index ed8f864..bd9e4ad 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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") } diff --git a/html/error_std.go b/html/error_std.go index 802cfdc..4d745d4 100644 --- a/html/error_std.go +++ b/html/error_std.go @@ -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)) } diff --git a/server/context/context.go b/server/context/context.go index 7aa4f01..c84a953 100644 --- a/server/context/context.go +++ b/server/context/context.go @@ -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 { diff --git a/server/gitea/cache.go b/server/gitea/cache.go index 4cded96..12303c8 100644 --- a/server/gitea/cache.go +++ b/server/gitea/cache.go @@ -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 ) diff --git a/server/gitea/client_std.go b/server/gitea/client_std.go index 7e1194e..67722b5 100644 --- a/server/gitea/client_std.go +++ b/server/gitea/client_std.go @@ -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 } diff --git a/server/handler_test.go b/server/handler_test.go index 8452dc8..4bfbad7 100644 --- a/server/handler_test.go +++ b/server/handler_test.go @@ -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()) } diff --git a/server/upstream/helper.go b/server/upstream/helper.go index 12eda80..7dfbc17 100644 --- a/server/upstream/helper.go +++ b/server/upstream/helper.go @@ -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) -}