Created
February 17, 2021 02:59
-
-
Save tobychui/e31ca5be46e266cf52fc247dd38c9181 to your computer and use it in GitHub Desktop.
Golang gzip middleware module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gzipmiddleware | |
import ( | |
"compress/gzip" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"strings" | |
"sync" | |
) | |
/* | |
This module handles gzip on http.Handler and http.HandleFunc | |
author: tobychui | |
*/ | |
var gzPool = sync.Pool{ | |
New: func() interface{} { | |
w := gzip.NewWriter(ioutil.Discard) | |
gzip.NewWriterLevel(w, gzip.BestCompression) | |
return w | |
}, | |
} | |
type gzipResponseWriter struct { | |
io.Writer | |
http.ResponseWriter | |
} | |
func (w *gzipResponseWriter) WriteHeader(status int) { | |
w.Header().Del("Content-Length") | |
w.ResponseWriter.WriteHeader(status) | |
} | |
func (w *gzipResponseWriter) Write(b []byte) (int, error) { | |
return w.Writer.Write(b) | |
} | |
/* | |
Compresstion function for http.FileServer | |
*/ | |
func Compress(h http.Handler) http.Handler { | |
return http.HandlerFunc(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 := gzPool.Get().(*gzip.Writer) | |
defer gzPool.Put(gz) | |
gz.Reset(w) | |
defer gz.Close() | |
h.ServeHTTP(&gzipResponseWriter{ResponseWriter: w, Writer: gz}, r) | |
}) | |
} | |
type gzipFuncResponseWriter struct { | |
io.Writer | |
http.ResponseWriter | |
} | |
func (w gzipFuncResponseWriter) Write(b []byte) (int, error) { | |
return w.Writer.Write(b) | |
} | |
/* | |
Compress Function for http.HandleFunc | |
*/ | |
func CompressFunc(fn http.HandlerFunc) http.HandlerFunc { | |
return func(w http.ResponseWriter, r *http.Request) { | |
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { | |
fn(w, r) | |
return | |
} | |
w.Header().Set("Content-Encoding", "gzip") | |
gz := gzip.NewWriter(w) | |
defer gz.Close() | |
gzr := gzipFuncResponseWriter{Writer: gz, ResponseWriter: w} | |
fn(gzr, r) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment