Allow configuration of gzip compression level

This commit is contained in:
Andrew Gaul 2017-01-15 10:30:41 -08:00 committed by Barna Csorogi
parent b23411caf2
commit ca005f7c97
2 changed files with 9 additions and 4 deletions

View file

@ -20,6 +20,7 @@ var (
jpeg = flag.Int("jpeg", 50, "jpeg quality (1-100, 0 to disable)") jpeg = flag.Int("jpeg", 50, "jpeg quality (1-100, 0 to disable)")
gif = flag.Bool("gif", true, "transcode gifs into static images") gif = flag.Bool("gif", true, "transcode gifs into static images")
gzip = flag.Int("gzip", -1, "gzip compression level (0-9, default 6)")
png = flag.Bool("png", true, "transcode png") png = flag.Bool("png", true, "transcode png")
minify = flag.Bool("minify", false, "minify css/html/js - WARNING: tends to break the web") minify = flag.Bool("minify", false, "minify css/html/js - WARNING: tends to break the web")
) )
@ -55,9 +56,9 @@ func main() {
var ttc proxy.Transcoder var ttc proxy.Transcoder
if *minify { if *minify {
ttc = &tc.Gzip{tc.NewMinifier(), false} ttc = &tc.Gzip{tc.NewMinifier(), *gzip, false}
} else { } else {
ttc = &tc.Gzip{&tc.Identity{}, true} ttc = &tc.Gzip{&tc.Identity{}, *gzip, true}
} }
p.AddTranscoder("text/css", ttc) p.AddTranscoder("text/css", ttc)

View file

@ -9,7 +9,8 @@ import (
type Gzip struct { type Gzip struct {
proxy.Transcoder proxy.Transcoder
SkipGzipped bool CompressionLevel int
SkipGzipped bool
} }
func (t *Gzip) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, headers http.Header) error { func (t *Gzip) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, headers http.Header) error {
@ -33,7 +34,10 @@ func (t *Gzip) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader, heade
} }
if shouldGzip && compress(r) { if shouldGzip && compress(r) {
gzw := gzip.NewWriter(w.Writer) gzw, err := gzip.NewWriterLevel(w.Writer, t.CompressionLevel)
if err != nil {
return err
}
defer gzw.Flush() defer gzw.Flush()
w.Writer = gzw w.Writer = gzw
w.Header().Set("Content-Encoding", "gzip") w.Header().Set("Content-Encoding", "gzip")