From ca005f7c972548aea330af39c974e60a077c5d5b Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Sun, 15 Jan 2017 10:30:41 -0800 Subject: [PATCH] Allow configuration of gzip compression level --- compy.go | 5 +++-- transcoder/gzip.go | 8 ++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/compy.go b/compy.go index 98c3c1f..1654813 100644 --- a/compy.go +++ b/compy.go @@ -20,6 +20,7 @@ var ( jpeg = flag.Int("jpeg", 50, "jpeg quality (1-100, 0 to disable)") 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") 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 if *minify { - ttc = &tc.Gzip{tc.NewMinifier(), false} + ttc = &tc.Gzip{tc.NewMinifier(), *gzip, false} } else { - ttc = &tc.Gzip{&tc.Identity{}, true} + ttc = &tc.Gzip{&tc.Identity{}, *gzip, true} } p.AddTranscoder("text/css", ttc) diff --git a/transcoder/gzip.go b/transcoder/gzip.go index 0017aaf..3aa65b9 100644 --- a/transcoder/gzip.go +++ b/transcoder/gzip.go @@ -9,7 +9,8 @@ import ( type Gzip struct { proxy.Transcoder - SkipGzipped bool + CompressionLevel int + SkipGzipped bool } 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) { - gzw := gzip.NewWriter(w.Writer) + gzw, err := gzip.NewWriterLevel(w.Writer, t.CompressionLevel) + if err != nil { + return err + } defer gzw.Flush() w.Writer = gzw w.Header().Set("Content-Encoding", "gzip")