diff --git a/.circleci/config.yml b/.circleci/config.yml index 52c6d249..9a53eb9f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -5,6 +5,41 @@ workflows: build: jobs: - checkout_code + - lint: + requires: + - checkout_code + go_version: "1.12" + vips_version: "8.7" + - build: + name: go1.11_vips8.7 + requires: + - checkout_code + go_version: "1.12" + vips_version: "8.7" + - build: + name: go1.11_vips8.6 + requires: + - checkout_code + go_version: "1.12" + vips_version: "8.6" + - build: + name: go1.11_vips8.5 + requires: + - checkout_code + go_version: "1.12" + vips_version: "8.5" + - build: + name: go1.11_vips8.4 + requires: + - checkout_code + go_version: "1.12" + vips_version: "8.4" + - build: + name: go1.11_vips8.3 + requires: + - checkout_code + go_version: "1.12" + vips_version: "8.3" - build: name: go1.11_vips8.7 requires: @@ -108,6 +143,32 @@ jobs: root: . paths: [.] + lint: + docker: + - image: "darthsim/imgproxy-circleci:latest" + working_directory: /go/src/imgproxy + environment: + BASH_ENV: "/root/.bashrc" + parameters: + go_version: + type: string + vips_version: + type: string + steps: + - attach_workspace: + at: . + - run: + name: Build imgproxy + command: | + gvm install go<< parameters.go_version >> -B + gvm use go<< parameters.go_version >> + export GOPATH=/go + export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/vips/<< parameters.vips_version >>/lib + export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/root/vips/<< parameters.vips_version >>/lib/pkgconfig + export CGO_LDFLAGS_ALLOW="-s|-w" + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin v1.16.0 + golangci-lint run . + build: docker: - image: "darthsim/imgproxy-circleci:latest" diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..911fc54e --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,22 @@ +linters: + fast: true + enable: + - golint + - govet + - gosimple + - goconst + - goimports + - staticcheck + disable: + - errcheck + +issues: + exclude-rules: + - path: _test\.go + linters: + - goconst + + # False positives on CGO generated code + - linters: + - staticcheck + text: "SA4000:" diff --git a/gcs_transport.go b/gcs_transport.go index f3ba8906..37e37eda 100644 --- a/gcs_transport.go +++ b/gcs_transport.go @@ -45,7 +45,7 @@ func (t gcsTransport) RoundTrip(req *http.Request) (resp *http.Response, err err ProtoMajor: 1, ProtoMinor: 0, Header: make(http.Header), - ContentLength: reader.Size(), + ContentLength: reader.Attrs.Size, Body: reader, Close: true, Request: req, diff --git a/main.go b/main.go index b0a32cc6..e56ddd86 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "os/signal" "runtime" "runtime/debug" + "syscall" "time" "net/http" @@ -39,7 +40,7 @@ func main() { s := startServer() stop := make(chan os.Signal, 1) - signal.Notify(stop, os.Interrupt, os.Kill) + signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM) <-stop diff --git a/processing_options.go b/processing_options.go index 0cde0352..36ba6c96 100644 --- a/processing_options.go +++ b/processing_options.go @@ -140,8 +140,6 @@ type processingOptions struct { UsedPresets []string } -type applyOptionFunc func(po *processingOptions, args []string) error - const ( imageURLCtxKey = ctxKey("imageUrl") processingOptionsCtxKey = ctxKey("processingOptions") @@ -467,19 +465,19 @@ func applyBackgroundOption(po *processingOptions, args []string) error { case 3: po.Flatten = true - if r, err := strconv.ParseUint(args[0], 10, 8); err == nil && r >= 0 && r <= 255 { + if r, err := strconv.ParseUint(args[0], 10, 8); err == nil && r <= 255 { po.Background.R = uint8(r) } else { return fmt.Errorf("Invalid background red channel: %s", args[0]) } - if g, err := strconv.ParseUint(args[1], 10, 8); err == nil && g >= 0 && g <= 255 { + if g, err := strconv.ParseUint(args[1], 10, 8); err == nil && g <= 255 { po.Background.G = uint8(g) } else { return fmt.Errorf("Invalid background green channel: %s", args[1]) } - if b, err := strconv.ParseUint(args[2], 10, 8); err == nil && b >= 0 && b <= 255 { + if b, err := strconv.ParseUint(args[2], 10, 8); err == nil && b <= 255 { po.Background.B = uint8(b) } else { return fmt.Errorf("Invalid background blue channel: %s", args[2]) diff --git a/processing_options_test.go b/processing_options_test.go index c6466098..f8f47777 100644 --- a/processing_options_test.go +++ b/processing_options_test.go @@ -16,12 +16,14 @@ import ( type ProcessingOptionsTestSuite struct{ MainTestSuite } func (s *ProcessingOptionsTestSuite) getRequest(url string) *fasthttp.RequestCtx { - req := fasthttp.Request{} - req.SetRequestURI(url) - return &fasthttp.RequestCtx{ - Request: req, + ctx := fasthttp.RequestCtx{ + Request: fasthttp.Request{}, Response: fasthttp.Response{}, } + + ctx.Request.SetRequestURI(url) + + return &ctx } func (s *ProcessingOptionsTestSuite) TestParseBase64URL() { diff --git a/s3transport.go b/s3transport.go index 4903f38b..b727950d 100644 --- a/s3transport.go +++ b/s3transport.go @@ -25,7 +25,10 @@ func newS3Transport() http.RoundTripper { s3Conf.S3ForcePathStyle = aws.Bool(true) } - sess := session.New() + sess, err := session.NewSession() + if err != nil { + logFatal("Can't create S3 session: %s", err) + } if sess.Config.Region == nil || len(*sess.Config.Region) == 0 { sess.Config.Region = aws.String("us-west-1") diff --git a/server.go b/server.go index ac3b5762..8601c698 100644 --- a/server.go +++ b/server.go @@ -262,7 +262,7 @@ func (h *httpHandler) ServeHTTP(rctx *fasthttp.RequestCtx) { panic(errInvalidMethod) } - if bytes.Compare(rctx.RequestURI(), healthPath) == 0 { + if bytes.Equal(rctx.RequestURI(), healthPath) { rctx.SetStatusCode(200) rctx.SetBody(imgproxyIsRunningMsg) return @@ -316,7 +316,7 @@ func (h *httpHandler) ServeHTTP(rctx *fasthttp.RequestCtx) { rctx.Response.Header.SetBytesV("ETag", eTag) - if bytes.Compare(eTag, rctx.Request.Header.Peek("If-None-Match")) == 0 { + if bytes.Equal(eTag, rctx.Request.Header.Peek("If-None-Match")) { respondWithNotModified(reqID, rctx) return } diff --git a/webp.go b/webp.go index b2b10ddd..b6181490 100644 --- a/webp.go +++ b/webp.go @@ -91,7 +91,6 @@ func decodeWebpConfig(r io.Reader) (image.Config, error) { const alphaBit = 1 << 4 - wantAlpha = buf[0] != alphaBit widthMinusOne := uint32(buf[4]) | uint32(buf[5])<<8 | uint32(buf[6])<<16 heightMinusOne := uint32(buf[7]) | uint32(buf[8])<<8 | uint32(buf[9])<<16