Skip to content

Commit

Permalink
Move defer statement into http handler function (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
robbawebba authored and nilslice committed Jan 8, 2018
1 parent 7194ae2 commit c8640a8
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions system/api/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -43,7 +45,6 @@ type gzipResponseWriter struct {
}

func (gzw gzipResponseWriter) Write(p []byte) (int, error) {
defer gzw.gw.Close()
return gzw.gw.Write(p)
}

Expand Down

0 comments on commit c8640a8

Please sign in to comment.