Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compression middleware #18

Closed
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*.test
*.out
go.work

*.local
.env
dist/
/book
/book
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ require (
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0 // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/aws/aws-sdk-go v1.44.314 // indirect
github.com/aws/aws-sdk-go-v2 v1.20.0 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.11 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0=
github.com/alexflint/go-filemutex v1.1.0/go.mod h1:7P4iRhttt/nUvUOrYIhcpMzv2G6CY9UnI16Z+UJqRyk=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apache/arrow/go/arrow v0.0.0-20210818145353-234c94e4ce64/go.mod h1:2qMFB56yOP3KzkB3PbYZ4AlUFg3a88F67TIx5lB/WwY=
github.com/apache/arrow/go/arrow v0.0.0-20211013220434-5962184e7a30/go.mod h1:Q7yQnSMnLvcXlZ8RV+jwz/6y1rQTqbX6C82SndT52Zs=
Expand Down
18 changes: 18 additions & 0 deletions internal/handler/site/middleware/compression.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package middleware

import (
"io"
"net/http"

"github.com/andybalholm/brotli"
"github.com/go-chi/chi/v5/middleware"
"github.com/oursky/pageship/internal/site"
)

func Compression(site *site.Descriptor, next http.Handler) http.Handler {
c := middleware.NewCompressor(5)
c.SetEncoder("br", func(w io.Writer, level int) io.Writer {
return brotli.NewWriterV2(w, level)
})
return c.Handler(next)
}
79 changes: 79 additions & 0 deletions internal/handler/site/middleware/compression_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package middleware_test //black box testing

import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/oursky/pageship/internal/config"
"github.com/oursky/pageship/internal/handler/site/middleware"
"github.com/oursky/pageship/internal/site"
"github.com/stretchr/testify/assert"
)

type mockHandler struct {
executeCount int
}

func (mh *mockHandler) serve(w http.ResponseWriter, r *http.Request) {
mh.executeCount++
w.Write([]byte("hello"))
}

type mockFS struct{}

func (m mockFS) Stat(path string) (*site.FileInfo, error) {
return &site.FileInfo{
IsDir: false,
ModTime: time.Now(),
Size: 0,
ContentType: "",
Hash: "",
}, nil
}

type mockRSCloser struct {
io.ReadSeeker
}

func (m mockRSCloser) Close() error {
return nil
}

func (m mockFS) Open(ctx context.Context, path string) (io.ReadSeekCloser, error) {
return mockRSCloser{bytes.NewReader([]byte{})}, nil
}

func TestCache(t *testing.T) {
//Setup
mh := mockHandler{}
sc := config.DefaultSiteConfig()
mockSiteDescriptor := site.Descriptor{
ID: "",
Domain: "",
Config: &sc,
FS: mockFS{},
}
h := middleware.Compression(&mockSiteDescriptor, http.HandlerFunc(mh.serve))

//Act Assert
req, err := http.NewRequest("GET", "endpoint", bytes.NewBuffer([]byte("body")))
assert.Empty(t, err)
req.Header.Add("Accept-Encoding", "gzip")
rec := httptest.NewRecorder()
rec.Header().Add("Content-Type", "text/plain")
h.ServeHTTP(rec, req)
assert.Equal(t, "gzip", rec.Result().Header.Get("Content-Encoding"))

req, err = http.NewRequest("GET", "endpoint", bytes.NewBuffer([]byte("body")))
assert.Empty(t, err)
req.Header.Add("Accept-Encoding", "br")
rec = httptest.NewRecorder()
rec.Header().Add("Content-Type", "text/plain")
h.ServeHTTP(rec, req)
assert.Equal(t, "br", rec.Result().Header.Get("Content-Encoding"))
}
5 changes: 4 additions & 1 deletion internal/handler/site/middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package middleware

import "github.com/oursky/pageship/internal/handler/site"
import (
"github.com/oursky/pageship/internal/handler/site"
)

var Default = []site.Middleware{
RedirectCustomDomain,
CanonicalizePath,
RouteSPA,
IndexPage,
Compression,
}
1 change: 1 addition & 0 deletions internal/handler/site/site_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (h *SiteHandler) serveFile(w http.ResponseWriter, r *http.Request) {
}
if info.Hash != "" {
w.Header().Set("ETag", fmt.Sprintf(`"%s"`, info.Hash))
w.Header().Set("Cache-Control", "public, max-age=31536000, no-cache")
}

reader := &lazyReader{
Expand Down