https://github.com/vicanso/elton-compress
Compress middleware for elton.
https://github.com/vicanso/elton-compress
compress elton lz4 middleware snappy zstandard
Last synced: 8 months ago
JSON representation
Compress middleware for elton.
- Host: GitHub
- URL: https://github.com/vicanso/elton-compress
- Owner: vicanso
- License: mit
- Created: 2019-02-22T13:19:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-05T06:10:50.000Z (over 3 years ago)
- Last Synced: 2024-12-27T19:45:11.468Z (over 1 year ago)
- Topics: compress, elton, lz4, middleware, snappy, zstandard
- Language: Go
- Homepage:
- Size: 129 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# elton-compress
[](https://github.com/vicanso/elton-compress/blob/master/LICENSE)
[](https://github.com/vicanso/elton-compress/actions)
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.
```go
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)
}
}
```
## Example
[middleware example](./example/main.go)
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