blob: c3bb5ed465b517f634cb32268925a02cb721d43c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package main
import (
"compress/gzip"
"io"
"net/http"
"strings"
"github.com/alexedwards/stack"
)
// Gzip Compression
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func Gzip(ctx *stack.Context, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if !strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") ||
ctx.Get("handled").(bool) == true {
// move on to next handler in the chain
next.ServeHTTP(w, req)
return
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzw := gzipResponseWriter{Writer: gz, ResponseWriter: w}
ctx.Put("handled", true)
defer ctx.Delete("handled")
next.ServeHTTP(gzw, req)
})
}
|