ginzip

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2021 License: MIT Imports: 9 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(options Options) gin.HandlerFunc

New creates a new Ginzip middleware function. Attach to your Gin router like this:

router.Use(ginzip.New(ginzip.DefaultOptions()))

Or use groups:

ui := r.Group("/", ginzip.New(ginzip.DefaultOptions()))

Types

type Options

type Options struct {
	// Compression levels for gzip/brotli
	// Default setting: ""/"d"/"default"
	// Best speed: "min"/"speed"
	// Best compression: "max"
	// Compression disabled: "false"/"n"/"no"
	// Manual setting: 1-9 (gzip), 0-11 (brotli)
	GzipLevel   string
	BrotliLevel string

	// Filepath extensions where compression should be skipped
	SkipExtensions []string
}

Options object is used to configure Ginzip.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions creates a new config object with default values:

Default compression level, ignore most common binary filetypes

.png, .gif, .jpeg, .jpg, .mp3, .mp4, .ogg, .zip, .exe

func (Options) IgnoreExt

func (o Options) IgnoreExt(exts ...string) Options

IgnoreExt adds more ignored file extension to the Options object.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL