Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.10.2

- feat: support compression of http responses

## 1.10.1

- chore: update dependencies
Expand Down
9 changes: 5 additions & 4 deletions exthttp/exthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package exthttp
import (
"encoding/json"
"fmt"
"github.com/klauspost/compress/gzhttp"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
Expand All @@ -24,14 +25,14 @@ import (

type Handler func(w http.ResponseWriter, r *http.Request, body []byte)

// RegisterHttpHandler registers a handler for the given path. Also adds panic recovery and request logging around the handler.
// RegisterHttpHandler registers a handler for the given path. Also adds panic recovery, gzip compression and request logging around the handler.
func RegisterHttpHandler(path string, handler Handler) {
http.Handle(path, PanicRecovery(LogRequest(handler)))
http.Handle(path, PanicRecovery(gzhttp.GzipHandler(LogRequest(handler))))
}

// RegisterHttpHandlerWithLogLevel registers a handler for the given path. Also adds panic recovery and request logging with a given log level around the handler.
// RegisterHttpHandlerWithLogLevel registers a handler for the given path. Also adds panic recovery, gzip compression and request logging with a given log level around the handler.
func RegisterHttpHandlerWithLogLevel(path string, handler Handler, defaultLevel zerolog.Level) {
http.Handle(path, PanicRecovery(LogRequestWithDefaultLogLevel(handler, defaultLevel)))
http.Handle(path, PanicRecovery(gzhttp.GzipHandler(LogRequestWithDefaultLogLevel(handler, defaultLevel))))
}

// GetterAsHandler turns a getter function into a handler function. Typically used in combination with the RegisterHttpHandler function.
Expand Down
43 changes: 43 additions & 0 deletions exthttp/exthttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
package exthttp

import (
"compress/gzip"
"github.com/klauspost/compress/gzhttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -207,3 +212,41 @@ func TestWriteBody(t *testing.T) {
})
}
}

func TestGzipHandler(t *testing.T) {
largeBody := `{"data":"` + strings.Repeat("x", 1500) + `"}`
inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(largeBody))
})

t.Run("should compress response when Accept-Encoding gzip is set", func(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("Accept-Encoding", "gzip")
rr := httptest.NewRecorder()

gzhttp.GzipHandler(inner).ServeHTTP(rr, req)

assert.Equal(t, 200, rr.Code)
assert.Equal(t, "gzip", rr.Header().Get("Content-Encoding"))

gr, err := gzip.NewReader(rr.Body)
require.NoError(t, err)
defer gr.Close()
body, err := io.ReadAll(gr)
require.NoError(t, err)
assert.Equal(t, largeBody, string(body))
})

t.Run("should not compress response when Accept-Encoding gzip is not set", func(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()

gzhttp.GzipHandler(inner).ServeHTTP(rr, req)

assert.Equal(t, 200, rr.Code)
assert.Empty(t, rr.Header().Get("Content-Encoding"))
assert.Equal(t, largeBody, rr.Body.String())
})
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/elastic/go-sysinfo v1.15.4
github.com/google/uuid v1.6.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/klauspost/compress v1.18.4
github.com/madflojo/testcerts v1.5.0
github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5
github.com/rs/zerolog v1.34.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/klauspost/compress v1.18.4 h1:RPhnKRAQ4Fh8zU2FY/6ZFDwTVTxgJ/EMydqSTzE9a2c=
github.com/klauspost/compress v1.18.4/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand Down