Add gzip
This commit is contained in:
49
server/gzipresponsewriter.go
Executable file
49
server/gzipresponsewriter.go
Executable file
@@ -0,0 +1,49 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type GZipResponseWriter struct {
|
||||
w http.ResponseWriter
|
||||
gz *gzip.Writer
|
||||
}
|
||||
|
||||
func NewGZipResponseWriter(w http.ResponseWriter) *GZipResponseWriter {
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
w.Header().Add("Content-Encoding", "gzip")
|
||||
return &GZipResponseWriter{
|
||||
w: w,
|
||||
gz: gzip.NewWriter(w),
|
||||
}
|
||||
}
|
||||
|
||||
func (gz *GZipResponseWriter) Header() http.Header {
|
||||
return gz.w.Header()
|
||||
}
|
||||
|
||||
func (gz *GZipResponseWriter) WriteHeader(statusCode int) {
|
||||
gz.w.WriteHeader(statusCode)
|
||||
}
|
||||
|
||||
func (gz *GZipResponseWriter) Write(b []byte) (int, error) {
|
||||
return gz.gz.Write(b)
|
||||
}
|
||||
|
||||
func (gz *GZipResponseWriter) Close() error {
|
||||
return gz.gz.Close()
|
||||
}
|
||||
|
||||
func (s *Server) gzip(h http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if encoding, _ := r.Header["Accept-Encoding"]; strings.Contains(fmt.Sprint(encoding), "gzip") {
|
||||
gz := NewGZipResponseWriter(w)
|
||||
defer gz.Close()
|
||||
w = gz
|
||||
}
|
||||
h(w, r)
|
||||
}
|
||||
}
|
||||
34
server/gzipresponsewriter_test.go
Executable file
34
server/gzipresponsewriter_test.go
Executable file
@@ -0,0 +1,34 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGZipResponseWriter(t *testing.T) {
|
||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gz := NewGZipResponseWriter(w)
|
||||
defer gz.Close()
|
||||
fmt.Fprintln(gz, "Hello world")
|
||||
}))
|
||||
defer s.Close()
|
||||
|
||||
req := httptest.NewRequest("GET", s.URL, nil)
|
||||
req.RequestURI = ""
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatal(resp.StatusCode)
|
||||
}
|
||||
if b, err := ioutil.ReadAll(resp.Body); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if s := string(b); s != "Hello world\n" {
|
||||
t.Fatalf("%q", s)
|
||||
}
|
||||
}
|
||||
@@ -18,27 +18,27 @@ func (s *Server) Routes() error {
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("notes/%s%s", wildcard, wildcard),
|
||||
handler: s.authenticate(s.notes),
|
||||
handler: s.gzip(s.authenticate(s.notes)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("edit/%s%s", wildcard, wildcard),
|
||||
handler: s.authenticate(s.edit),
|
||||
handler: s.gzip(s.authenticate(s.edit)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("delete/%s%s", wildcard, wildcard),
|
||||
handler: s.authenticate(s.delete),
|
||||
handler: s.gzip(s.authenticate(s.delete)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("submit/%s%s", wildcard, wildcard),
|
||||
handler: s.authenticate(s.submit),
|
||||
handler: s.gzip(s.authenticate(s.submit)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("create/%s%s", wildcard, wildcard),
|
||||
handler: s.authenticate(s.create),
|
||||
handler: s.gzip(s.authenticate(s.create)),
|
||||
},
|
||||
{
|
||||
path: fmt.Sprintf("search"),
|
||||
handler: s.authenticate(s.search),
|
||||
handler: s.gzip(s.authenticate(s.search)),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user