style: fix lint errors

This commit is contained in:
Kevin Franklin Kim 2021-11-15 16:03:59 +01:00
parent 212b0a9273
commit 6282d281fc
7 changed files with 51 additions and 17 deletions

View File

@ -29,10 +29,8 @@ linters:
- bodyclose - bodyclose
- deadcode - deadcode
- dogsled - dogsled
- dupl
- exhaustive - exhaustive
- exportloopref - exportloopref
- gci
- goconst - goconst
- gofmt - gofmt
- gofumpt - gofumpt
@ -43,7 +41,6 @@ linters:
- ineffassign - ineffassign
- misspell - misspell
- nakedret - nakedret
- noctx
- nolintlint - nolintlint
- prealloc - prealloc
- rowserrcheck - rowserrcheck
@ -67,6 +64,9 @@ linters:
- exportloopref - exportloopref
# unused # unused
# - gci
# - noctx
# - dupl
# - godot # - godot
# - gocognit # - gocognit
# - nlreturn # - nlreturn

31
.goreleaser.yml Normal file
View File

@ -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"

View File

@ -4,7 +4,7 @@ import "go.uber.org/zap"
const ( const (
ServiceRoutineKey = "service_routine" ServiceRoutineKey = "service_routine"
CacheIdKey = "cache_id" CacheIDKey = "cache_id"
URLKey = "url" URLKey = "url"
NumberOfWaitingClientsKey = "num_waiting_clients" NumberOfWaitingClientsKey = "num_waiting_clients"
) )
@ -13,8 +13,8 @@ func FServiceRoutine(name string) zap.Field {
return zap.String(ServiceRoutineKey, name) return zap.String(ServiceRoutineKey, name)
} }
func FCacheId(name string) zap.Field { func FCacheID(name string) zap.Field {
return zap.String(CacheIdKey, name) return zap.String(CacheIDKey, name)
} }
func FURL(name string) zap.Field { func FURL(name string) zap.Field {

View File

@ -1,7 +1,7 @@
package proxy package proxy
import ( import (
"crypto/md5" "crypto/md5" // nolint:gosec
"encoding/hex" "encoding/hex"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -67,10 +67,11 @@ func (c *Cache) callWebHooks() {
for _, url := range c.webHooks() { for _, url := range c.webHooks() {
go func(url string, l *zap.Logger) { go func(url string, l *zap.Logger) {
l.Info("call webhook") l.Info("call webhook")
_, err := http.Get(url) resp, err := http.Get(url) // nolint:gosec
if err != nil { if err != nil {
l.Error("error while calling webhook", zap.Error(err)) l.Error("error while calling webhook", zap.Error(err))
} }
defer resp.Body.Close()
}(url, c.l.With(log.FURL(url))) }(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 it here maybe, to keep it shorter
hash := md5.New() hash := md5.New() // nolint:gosec
hash.Write([]byte(id)) hash.Write([]byte(id))
id = hex.EncodeToString(hash.Sum(nil)) id = hex.EncodeToString(hash.Sum(nil))
return cacheID(id) return cacheID(id)

View File

@ -26,7 +26,7 @@ func getJobRunner(c *Cache, backendURL func() string, chanJobDone chan requestJo
return return
} }
for k, v := range job.request.Header { for k, v := range job.request.Header {
req.Header.Set(k, string(v[0])) req.Header.Set(k, v[0])
} }
client := http.Client{} client := http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"net/http" "net/http"
"github.com/foomo/contentfulproxy/packages/go/log" "github.com/foomo/contentfulproxy/packages/go/log"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -59,9 +58,9 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return return
} }
cachedResponse = jobDone.cachedResponse 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 { } 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 key, values := range cachedResponse.header {
for _, value := range values { for _, value := range values {
@ -70,7 +69,7 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
_, err := w.Write(cachedResponse.response) _, err := w.Write(cachedResponse.response)
if err != nil { 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: default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed) http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
@ -114,11 +113,11 @@ func getLoop(
pendingRequests[cacheID] = append(pendingRequests[cacheID], nextJob.chanDone) pendingRequests[cacheID] = append(pendingRequests[cacheID], nextJob.chanDone)
requests := pendingRequests[cacheID] requests := pendingRequests[cacheID]
if len(requests) == 1 { 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) go jobRunner(nextJob, cacheID)
} }
case jobDone := <-chanJobDone: 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] { for _, chanPending := range pendingRequests[jobDone.id] {
chanPending <- jobDone chanPending <- jobDone
} }

View File

@ -22,6 +22,7 @@ const (
type getStats func(path string) int type getStats func(path string) int
//
func GetBackend(t *testing.T) (getStats, http.HandlerFunc) { func GetBackend(t *testing.T) (getStats, http.HandlerFunc) {
stats := map[string]int{} stats := map[string]int{}
statLock := sync.RWMutex{} statLock := sync.RWMutex{}
@ -136,7 +137,9 @@ func TestProxy(t *testing.T) {
// assert.NoError(t, err) // 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) time.Sleep(time.Second * 1)