Handler wraps the provided http.Handler and provides transparent body compression based on a Request's "Accept-Encoding" header. Each handler instance pools its own compressors.
(next http.Handler)
| 24 | // |
| 25 | // Each handler instance pools its own compressors. |
| 26 | func Handler(next http.Handler) http.Handler { |
| 27 | h := handler{ |
| 28 | next: next, |
| 29 | } |
| 30 | h.zstd.New = func() interface{} { |
| 31 | w, err := zstd.NewWriter(nil, zstd.WithEncoderLevel(zstd.SpeedFastest)) |
| 32 | if err != nil { |
| 33 | panic(err) |
| 34 | } |
| 35 | return w |
| 36 | } |
| 37 | h.gzip.New = func() interface{} { |
| 38 | w, err := gzip.NewWriterLevel(nil, gzip.BestSpeed) |
| 39 | if err != nil { |
| 40 | panic(err) |
| 41 | } |
| 42 | return w |
| 43 | } |
| 44 | h.flate.New = func() interface{} { |
| 45 | w, err := flate.NewWriter(nil, flate.BestSpeed) |
| 46 | if err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | return w |
| 50 | } |
| 51 | |
| 52 | return &h |
| 53 | } |
| 54 | |
| 55 | var _ http.Handler = (*handler)(nil) |
| 56 |
no outgoing calls