Gzip/Brotli compression middleware for Gin-Gonic
|
|
||
|---|---|---|
| example | ||
| .drone.yml | ||
| .editorconfig | ||
| .golangci.yaml | ||
| .pre-commit-config.yaml | ||
| ginzip.go | ||
| ginzip_test.go | ||
| go.mod | ||
| go.sum | ||
| LICENSE | ||
| README.md | ||
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
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):
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 by appleboy and anargu's gin-brotli (both MIT License).