64 lines
1.6 KiB
Markdown
64 lines
1.6 KiB
Markdown
# Ginzip
|
|
|
|
Ginzip is a compression middleware for the Gin web framework.
|
|
|
|
It compresses any data with gzip/brotli compression, depending on
|
|
the encodings accepted by the client.
|
|
|
|
## Example
|
|
|
|
```go
|
|
import (
|
|
"code.thetadev.de/TSGRain/ginzip"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func main() {
|
|
router := gin.Default()
|
|
|
|
ui := router.Group("/", ginzip.New(ginzip.DefaultOptions()))
|
|
ui.GET("/", getTestHandler("Hello World (should be compressed)"))
|
|
|
|
api := router.Group("/api")
|
|
api.GET("/", getTestHandler("Hello API (should be uncompressed)"))
|
|
|
|
_ = router.Run(":8080")
|
|
}
|
|
|
|
func getTestHandler(msg string) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.String(200, msg)
|
|
}
|
|
}
|
|
```
|
|
|
|
## Configuration
|
|
|
|
Gzip and brotli compression level can be configured separately with the configuration object.
|
|
|
|
- Default setting: `""`/`"d"`/`"default"`
|
|
- Best speed: `"min"`/`"speed"`
|
|
- Best compression: `"max"`
|
|
- Compression disabled: `"false"`/`"n"`/`"no"`
|
|
- Manual setting: `"1"` - `"9"` (gzip), `"0"` - `"11"` (brotli)
|
|
|
|
Additionally you can specify a list of path extensions where compression should be disabled
|
|
(for example for incompressible image, audio and video files).
|
|
|
|
Example (brotli disabled, maximum gzip compression, add ".bin" to ignorelist):
|
|
|
|
```go
|
|
options := ginzip.DefaultOptions()
|
|
options.BrotliLevel = "no"
|
|
options.GzipLevel = "max"
|
|
|
|
options.IgnoreExt(".bin")
|
|
|
|
ginzip.New(options)
|
|
```
|
|
|
|
## Credits
|
|
|
|
This middleware is based on the code of
|
|
Gin's [gzip middleware](https://github.com/gin-gonic/contrib/tree/master/gzip) by appleboy
|
|
and anargu's [gin-brotli](https://github.com/anargu/gin-brotli) (both MIT License).
|