diff options
author | Rob Weber <robertweber95@gmail.com> | 2018-01-08 08:31:03 -0800 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2018-01-08 09:31:03 -0700 |
commit | c8640a82dd704ce51676a457b4a2ec72ec67e316 (patch) | |
tree | b1ac52c44f43f8840bc05406a64e9741c1a3f177 /system | |
parent | 7194ae217d76436003ec03dab368c273ce3f2b5f (diff) |
Move defer statement into http handler function (#219)
Diffstat (limited to 'system')
-rw-r--r-- | system/api/gzip.go | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/system/api/gzip.go b/system/api/gzip.go index be5a51b..02f1535 100644 --- a/system/api/gzip.go +++ b/system/api/gzip.go @@ -20,11 +20,13 @@ func Gzip(next http.HandlerFunc) http.HandlerFunc { if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") { // gzip response data res.Header().Set("Content-Encoding", "gzip") + gzWriter := gzip.NewWriter(res) + defer gzWriter.Close() var gzres gzipResponseWriter if pusher, ok := res.(http.Pusher); ok { - gzres = gzipResponseWriter{res, pusher, gzip.NewWriter(res)} + gzres = gzipResponseWriter{res, pusher, gzWriter} } else { - gzres = gzipResponseWriter{res, nil, gzip.NewWriter(res)} + gzres = gzipResponseWriter{res, nil, gzWriter} } next.ServeHTTP(gzres, req) @@ -43,7 +45,6 @@ type gzipResponseWriter struct { } func (gzw gzipResponseWriter) Write(p []byte) (int, error) { - defer gzw.gw.Close() return gzw.gw.Write(p) } |