forked from haruyama/golang-goji-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzip.go
33 lines (29 loc) · 736 Bytes
/
gzip.go
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
package system
import (
"io"
"net/http"
"github.com/zenazn/goji/web"
"strings"
"compress/gzip"
)
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
func (application *Application) ApplyGzip(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
h.ServeHTTP(w, r)
return
}
w.Header().Set("Content-Encoding", "gzip")
gz := gzip.NewWriter(w)
defer gz.Close()
gzr := gzipResponseWriter{Writer: gz, ResponseWriter: w}
h.ServeHTTP(gzr, r)
}
return http.HandlerFunc(fn)
}