More compressor for elton compress middleware.
SnappyCompressor
Snappy compression algorithm is fast, but not aim for maximum compression. It's useful for Intranet. Not support compress level.ZstdCompressor
Zstandard is a real-time compression algorithm, providing high compression ratios. Compress level is 1-2, default 0(2).Lz4Compressor
LZ4 is lossless compression algorithm, providing compression speed > 500 MB/s per core, scalable with multi-cores CPU. Compress level higher is better, use 0 for fastest compression.
package main
import (
"bytes"
"io/ioutil"
"net/http"
"github.com/vicanso/elton"
compress "github.com/vicanso/elton-compress"
"github.com/vicanso/elton/middleware"
)
func main() {
e := elton.New()
// 需要注意添加的顺序,压缩是按添加的选择顺序选择适合的压缩方式
// 此处只是示例所有的压缩器,正常使用时,按需使用1,2个压缩方式则可
config := middleware.NewCompressConfig(
&middleware.BrCompressor{
MinLength: 1024,
},
new(middleware.GzipCompressor),
new(compress.SnappyCompressor),
new(compress.ZstdCompressor),
new(compress.S2Compressor),
&compress.Lz4Compressor{
MinLength: 10 * 1024,
},
)
e.Use(middleware.NewCompress(config))
e.GET("/", func(c *elton.Context) (err error) {
resp, err := http.Get("https://code.jquery.com/jquery-3.4.1.min.js")
if err != nil {
return
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
c.SetContentTypeByExt(".js")
c.BodyBuffer = bytes.NewBuffer(buf)
return
})
err := e.ListenAndServe(":3000")
if err != nil {
panic(err)
}
}
jquery-3.4.1.min.js(88145 bytes)
compression | size | ratio | level |
---|---|---|---|
gzip | 30827 | 2.859 | 6 |
br | 29897 | 2.948 | 6 |
snappy | 47709 | 1.847 | - |
zstd | 32816 | 2.686 | 2 |
lz4 | 39434 | 2.235 | 6 |