Serving gzipped content for Go
Solution 1
There is no “out of the box” support for gzip-compressed HTTP responses yet. But adding it is pretty trivial. Have a look at
https://gist.github.com/the42/1956518
also
https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/cgUp8_ATNtc
Solution 2
The New York Times have released their gzip middleware package for Go.
You just pass your http.HandlerFunc
through their GzipHandler
and you're done. It looks like this:
package main
import (
"io"
"net/http"
"github.com/nytimes/gziphandler"
)
func main() {
withoutGz := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, "Hello, World")
})
withGz := gziphandler.GzipHandler(withoutGz)
http.Handle("/", withGz)
http.ListenAndServe("0.0.0.0:8000", nil)
}
Solution 3
For the sake of completeness, I eventually answered my own question with a handler that is simple and specialises in solving this issue.
This serves static files from a Go http server, including the asked-for performance-enhancing features. It is based on the standard net/http ServeFiles, with gzip/brotli and cache performance enhancements.
Related videos on Youtube
Rick-777
I work as a contract Java enterprise developer, also including a breadth of related technologies (Groovy, Scala etc). I have worked in a variety of application areas and have particularly enjoyed the challenges of concurrency.
Updated on July 09, 2022Comments
-
Rick-777 almost 2 years
I'm starting to write server-side applications in Go. I'd like to use the
Accept-Encoding
request header to determine whether to compress the response entity usingGZIP
. I had hoped to find a way to do this directly using thehttp.Serve
orhttp.ServeFile
methods.This is quite a general requirement; did I miss something or do I need to roll my own solution?
-
miku over 11 yearsExample of a homebrew solution: gist.github.com/982674#file-webserver-go-L191
-
-
Rick-777 over 11 yearsIt would also be 'nice to have' if the http.ServeFile method would check for the presence of xxx.gz and serve it if present and the accept header includes gzip. This is a trick that nginx does and it greatly accelerates the serving of static css, js etc files because they can be compressed just once during the build process. There's the added benefit too that they are smaller files to read, as well as needing fewer bytes on the wire.
-
Jo Liss over 10 yearsI believe the Gist, as well as the httpgzip package, might break on partial responses (GitHub issue) and would need a Vary header (GitHub issue).
-
CommonSenseCode almost 6 yearstheres already an official golang gzip lib: golang.org/pkg/compress/gzip
-
Bryan almost 6 years@commonSenseCode I can't see where that does http ?
-
MCCCS almost 4 years