Compress is a middleware for Fox that compresses responses with zstd, brotli
or gzip, depending on the client Accept-Encoding.
The current API is not yet stabilized. Breaking changes may occur before v1.0.0 and will be noted on the release note.
go get -u github.com/fox-toolkit/compress- zstd, brotli and gzip, or your own encoder.
- Compress only bodies above a minimum size.
- Allowlist or denylist of content types.
- Choose the encoder order when the client has no preference.
- Works with streaming responses, like server-sent events or a reverse proxy.
package main
import (
"errors"
"log"
"net/http"
"os"
"github.com/fox-toolkit/compress"
"github.com/fox-toolkit/fox"
)
func main() {
mw, err := compress.Middleware(compress.WithMinSize(512))
if err != nil {
log.Fatalln(err)
}
f := fox.MustRouter(fox.WithMiddleware(mw))
f.MustAdd(fox.MethodGet, "/*{filepath}", fox.WrapH(http.FileServerFS(os.DirFS("public"))))
if err := http.ListenAndServe(":8080", f); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalln(err)
}
}