I'm want to add gzip compression to all handlers. Here how it looks now
func gzipHandler(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()
fn(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
}
}
http.Handle("/", http.FileServer(http.Dir("./index")))
http.HandleFunc("/json", gzipHandler(sendJSONHandler))
http.HandleFunc("/contact", gzipHandler(contactHandler))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
http.ListenAndServe(":80", cacheHandler(http.DefaultServeMux))
I want to do something like in last line with cacheHandler