-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d3737dc
commit 39f6423
Showing
5 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
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(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) | ||
} | ||
|
||
func compression(site *site.Descriptor, next http.Handler) http.Handler { | ||
return Compression(next) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package middleware_test | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"compress/gzip" | ||
|
||
"github.com/oursky/pageship/internal/handler/site/middleware" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/andybalholm/brotli" | ||
) | ||
|
||
type mockHandler struct { | ||
executeCount int | ||
} | ||
|
||
func (mh *mockHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
mh.executeCount++ | ||
w.Header().Add("Content-Type", "text/plain") | ||
w.Write([]byte("hello")) | ||
} | ||
|
||
func TestCacheGzip(t *testing.T) { | ||
//Setup | ||
h := middleware.Compression(new(mockHandler)) | ||
|
||
//Act Assert | ||
req, err := http.NewRequest("GET", "endpoint", nil) | ||
assert.Empty(t, err) | ||
req.Header.Add("Accept-Encoding", "gzip") | ||
|
||
rec := httptest.NewRecorder() | ||
h.ServeHTTP(rec, req) | ||
|
||
resp := rec.Result() | ||
assert.Equal(t, "gzip", resp.Header.Get("Content-Encoding")) | ||
|
||
gzreader, err := gzip.NewReader(resp.Body) | ||
defer gzreader.Close() | ||
b, err := io.ReadAll(gzreader) | ||
|
||
assert.Empty(t, err) | ||
assert.Equal(t, "hello", string(b)) | ||
} | ||
|
||
func TestCacheBrotli(t *testing.T) { | ||
//Setup | ||
h := middleware.Compression(new(mockHandler)) | ||
|
||
//Act Assert | ||
req, err := http.NewRequest("GET", "endpoint", nil) | ||
assert.Empty(t, err) | ||
req.Header.Add("Accept-Encoding", "br") | ||
|
||
rec := httptest.NewRecorder() | ||
h.ServeHTTP(rec, req) | ||
|
||
resp := rec.Result() | ||
assert.Equal(t, "br", resp.Header.Get("Content-Encoding")) | ||
|
||
brreader := brotli.NewReader(resp.Body) | ||
defer resp.Body.Close() | ||
b, err := io.ReadAll(brreader) | ||
|
||
assert.Empty(t, err) | ||
assert.Equal(t, "hello", string(b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |