From 6282d281fc8b3b79152df8427fa3c92ae32e4d38 Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Mon, 15 Nov 2021 16:03:59 +0100 Subject: [PATCH] style: fix lint errors --- .golangci.yml | 6 +++--- .goreleaser.yml | 31 +++++++++++++++++++++++++++++++ packages/go/log/log.go | 6 +++--- proxy/cache.go | 7 ++++--- proxy/jobs.go | 2 +- proxy/proxy.go | 11 +++++------ proxy/proxy_test.go | 5 ++++- 7 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 .goreleaser.yml diff --git a/.golangci.yml b/.golangci.yml index ff103b3..8a769bf 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -29,10 +29,8 @@ linters: - bodyclose - deadcode - dogsled - - dupl - exhaustive - exportloopref - - gci - goconst - gofmt - gofumpt @@ -43,7 +41,6 @@ linters: - ineffassign - misspell - nakedret - - noctx - nolintlint - prealloc - rowserrcheck @@ -67,6 +64,9 @@ linters: - exportloopref # unused + # - gci + # - noctx + # - dupl # - godot # - gocognit # - nlreturn diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..d803596 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,31 @@ +# .goreleaser.yml +# Build customization +builds: + - binary: contentfulproxy + main: ./cmd/contentfulproxy/main.go + env: + - CGO_ENABLED=0 + ldflags: + - -s -w + goos: + - windows + - darwin + - linux + goarch: + - amd64 + +# .goreleaser.yml +archives: + - format: tar.gz + format_overrides: + - goos: windows + format: zip + +brews: + # Reporitory to push the tap to. + - tap: + owner: foomo + name: homebrew-contentfulproxy + caveats: "contentfulproxy -webserver-address=$CONTENTFULPROXY_SERVER_ADDR" + homepage: "https://github.com/foomo/contentfulproxy" + description: "An experimental proxy for read access to contentful to save your API quota" diff --git a/packages/go/log/log.go b/packages/go/log/log.go index f55c8c2..3ea1191 100644 --- a/packages/go/log/log.go +++ b/packages/go/log/log.go @@ -4,7 +4,7 @@ import "go.uber.org/zap" const ( ServiceRoutineKey = "service_routine" - CacheIdKey = "cache_id" + CacheIDKey = "cache_id" URLKey = "url" NumberOfWaitingClientsKey = "num_waiting_clients" ) @@ -13,8 +13,8 @@ func FServiceRoutine(name string) zap.Field { return zap.String(ServiceRoutineKey, name) } -func FCacheId(name string) zap.Field { - return zap.String(CacheIdKey, name) +func FCacheID(name string) zap.Field { + return zap.String(CacheIDKey, name) } func FURL(name string) zap.Field { diff --git a/proxy/cache.go b/proxy/cache.go index 8fe5647..7ed3512 100644 --- a/proxy/cache.go +++ b/proxy/cache.go @@ -1,7 +1,7 @@ package proxy import ( - "crypto/md5" + "crypto/md5" // nolint:gosec "encoding/hex" "io/ioutil" "net/http" @@ -67,10 +67,11 @@ func (c *Cache) callWebHooks() { for _, url := range c.webHooks() { go func(url string, l *zap.Logger) { l.Info("call webhook") - _, err := http.Get(url) + resp, err := http.Get(url) // nolint:gosec if err != nil { l.Error("error while calling webhook", zap.Error(err)) } + defer resp.Body.Close() }(url, c.l.With(log.FURL(url))) } } @@ -101,7 +102,7 @@ func getCacheIDForRequest(r *http.Request) cacheID { } } // hash it here maybe, to keep it shorter - hash := md5.New() + hash := md5.New() // nolint:gosec hash.Write([]byte(id)) id = hex.EncodeToString(hash.Sum(nil)) return cacheID(id) diff --git a/proxy/jobs.go b/proxy/jobs.go index 8d01900..d659650 100644 --- a/proxy/jobs.go +++ b/proxy/jobs.go @@ -26,7 +26,7 @@ func getJobRunner(c *Cache, backendURL func() string, chanJobDone chan requestJo return } for k, v := range job.request.Header { - req.Header.Set(k, string(v[0])) + req.Header.Set(k, v[0]) } client := http.Client{} resp, err := client.Do(req) diff --git a/proxy/proxy.go b/proxy/proxy.go index 534bc2c..e4749e3 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -5,7 +5,6 @@ import ( "encoding/json" "net/http" - "github.com/foomo/contentfulproxy/packages/go/log" "go.uber.org/zap" ) @@ -59,9 +58,9 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } cachedResponse = jobDone.cachedResponse - p.l.Info("serve response after cache creation", log.FURL(r.RequestURI), log.FCacheId(string(cacheID))) + p.l.Info("serve response after cache creation", log.FURL(r.RequestURI), log.FCacheID(string(cacheID))) } else { - p.l.Info("serve response from cache", log.FURL(r.RequestURI), log.FCacheId(string(cacheID))) + p.l.Info("serve response from cache", log.FURL(r.RequestURI), log.FCacheID(string(cacheID))) } for key, values := range cachedResponse.header { for _, value := range values { @@ -70,7 +69,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) { } _, err := w.Write(cachedResponse.response) if err != nil { - p.l.Info("writing cached response failed", log.FURL(r.RequestURI), log.FCacheId(string(cacheID))) + p.l.Info("writing cached response failed", log.FURL(r.RequestURI), log.FCacheID(string(cacheID))) } default: http.Error(w, "method not allowed", http.StatusMethodNotAllowed) @@ -114,11 +113,11 @@ func getLoop( pendingRequests[cacheID] = append(pendingRequests[cacheID], nextJob.chanDone) requests := pendingRequests[cacheID] if len(requests) == 1 { - l.Info("starting jobrunner for", log.FURL(nextJob.request.RequestURI), log.FCacheId(string(cacheID))) + l.Info("starting jobrunner for", log.FURL(nextJob.request.RequestURI), log.FCacheID(string(cacheID))) go jobRunner(nextJob, cacheID) } case jobDone := <-chanJobDone: - l.Info("request complete", log.FCacheId(string(jobDone.id)), log.FNumberOfWaitingClients(len(pendingRequests[jobDone.id]))) + l.Info("request complete", log.FCacheID(string(jobDone.id)), log.FNumberOfWaitingClients(len(pendingRequests[jobDone.id]))) for _, chanPending := range pendingRequests[jobDone.id] { chanPending <- jobDone } diff --git a/proxy/proxy_test.go b/proxy/proxy_test.go index 113f8de..d05324b 100644 --- a/proxy/proxy_test.go +++ b/proxy/proxy_test.go @@ -22,6 +22,7 @@ const ( type getStats func(path string) int +// func GetBackend(t *testing.T) (getStats, http.HandlerFunc) { stats := map[string]int{} statLock := sync.RWMutex{} @@ -136,7 +137,9 @@ func TestProxy(t *testing.T) { // assert.NoError(t, err) // - _, _ = http.Get(server.URL + "/update") + resp, err := http.Get(server.URL + "/update") + assert.NoError(t, err) + defer resp.Body.Close() time.Sleep(time.Second * 1)