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 // Start the web fastServer
log.Info().Msgf("Start listening on %s", listener.Addr()) log.Info().Msgf("Start listening on %s", listener.Addr())
http.Serve(listener, fastServer) if err := http.Serve(listener, fastServer); err != nil {
if err != nil {
log.Panic().Err(err).Msg("Couldn't start fastServer") 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) 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] code = status[0]
} }
c.RespWriter.WriteHeader(code) 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 { 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 // fileCacheTimeout specifies the timeout for the file content cache - you might want to make this quite long, depending
// on your available memory. // on your available memory.
// TODO: move as option into cache interface // 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 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, 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 return stamp, nil
} }
@ -159,6 +161,8 @@ func (client *Client) GiteaGetRepoDefaultBranch(repoOwner, repoName string) (str
} }
branch := repo.DefaultBranch 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 return branch, nil
} }

View file

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

View file

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