Gzip/Brotli compression middleware for Gin-Gonic
Find a file
Theta-Dev 05a4d62360
All checks were successful
continuous-integration/drone/push Build is passing
remove dead code from example
2021-12-17 19:08:08 +01:00
example remove dead code from example 2021-12-17 19:08:08 +01:00
.drone.yml Initial commit 2021-12-17 14:05:22 +01:00
.editorconfig Initial commit 2021-12-17 14:05:22 +01:00
.golangci.yaml Initial commit 2021-12-17 14:05:22 +01:00
.pre-commit-config.yaml Initial commit 2021-12-17 14:05:22 +01:00
ginzip.go use map for ignored extensions 2021-12-17 14:18:40 +01:00
ginzip_test.go Initial commit 2021-12-17 14:05:22 +01:00
go.mod Initial commit 2021-12-17 14:05:22 +01:00
go.sum Initial commit 2021-12-17 14:05:22 +01:00
LICENSE Initial commit 2021-12-17 14:05:22 +01:00
README.md Initial commit 2021-12-17 14:05:22 +01:00

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).