-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.go
64 lines (56 loc) · 1.63 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"compress/gzip"
"io"
"log"
"net/http"
"strings"
)
func cacheHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "private, max-age=604800")
next.ServeHTTP(w, r)
})
}
type gzipResponseWriter struct {
io.Writer
http.ResponseWriter
}
func (w gzipResponseWriter) Write(b []byte) (int, error) {
return w.Writer.Write(b)
}
// type zstdResponseWriter struct {
// io.Writer
// http.ResponseWriter
// }
// func (w zstdResponseWriter) Write(b []byte) (int, error) {
// return w.Writer.Write(b)
// }
func compressionHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// prefer zstd over gzip, but there seems to be a memory leak in zstd?
c := r.Header.Get("Accept-Encoding")
/* if strings.Contains(c, "zstd") {
w.Header().Set("Content-Encoding", "zstd")
zw, err := zstd.NewWriter(w, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
if err != nil {
log.Printf("failed to instantiate zstd middleware: %v", err.Error())
next.ServeHTTP(w, r)
}
defer zw.Close()
next.ServeHTTP(zstdResponseWriter{Writer: zw, ResponseWriter: w}, r)
} else */if strings.Contains(c, "gzip") {
w.Header().Set("Content-Encoding", "gzip")
gz, err := gzip.NewWriterLevel(w, gzip.BestCompression)
if err != nil {
log.Printf("failed to instantiate gzip middleware: %v", err.Error())
next.ServeHTTP(w, r)
}
defer gz.Close()
next.ServeHTTP(gzipResponseWriter{Writer: gz, ResponseWriter: w}, r)
} else {
next.ServeHTTP(w, r)
return
}
})
}