compress

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2022 License: BSD-3-Clause Imports: 9 Imported by: 0

README

compress

Go Report Card

an easy use compress middleware for gin.

usage

Use compress.New() to create a compress middleware, it's accept an option, you can use compress.UseAllBestSpeed() to use all method and set them for best speed, or use compress.UseAllBestCompression() to use all method but set them for best compression. if you want highly customizable, you can define your own Option.
By default, the methods order is br,gzip,deflate, if you like to change the order, you can define Option and change Option.EnableMethods.

example

package main

import (
    "github.com/gin-gonic/gin"
    "github.com/zigitn/compress"
)

func main() {
    router := gin.Default()
    router.Use(compress.New(compress.UseAllBestSpeed()))
    router.GET("/", func(c *gin.Context) {
        c.String(200, "Hello World")
    })
    router.Run()
}

thanks

gin-contrib/gzip
anargu/gin-brotli

Documentation

Index

Constants

View Source
const (
	UseBrotli  = "br"      // br
	UseGzip    = "gzip"    // gzip
	UseDeflate = "deflate" // deflate
)

Variables

This section is empty.

Functions

func New

func New(option Option) gin.HandlerFunc

New a compress middleware, will use Option to config

Types

type DeflateOption added in v1.1.0

type DeflateOption struct {
	Level int
	Dict  []byte // leave empty for default
}

DeflateOption is used to config deflate

type Option added in v1.1.0

type Option struct {
	EnableMethods    []string // enabled compress methods, will be matched in order
	GzipLevel        int
	BrotliOption     brotli.WriterOptions
	DeflateOption    DeflateOption
	HeadFilter       map[string]string
	ExtensionsFilter []string
	PathFilter       []string
	PathRegexFilter  []string
	CustomFilter     []func(c *gin.Context) bool // return true to exclude anything you want
}

Option can enable and config each compress method for simple needs please use UseAllBestSpeed or UseAllBestBestCompression

func UseAllBestBestCompression added in v1.1.0

func UseAllBestBestCompression() Option

UseAllBestBestCompression will enable all compress methods with best compression

func UseAllBestSpeed added in v1.1.0

func UseAllBestSpeed() Option

UseAllBestSpeed will enable all compress methods with best speed

type ResponseWriter

type ResponseWriter struct {
	gin.ResponseWriter
	ComPressWriter io.WriteCloser
}

ResponseWriter is a generic response writer

func (*ResponseWriter) Close

func (r *ResponseWriter) Close() error

Close closes the writer

func (*ResponseWriter) Write

func (r *ResponseWriter) Write(data []byte) (int, error)

Write data to response

func (*ResponseWriter) WriteHeader

func (r *ResponseWriter) WriteHeader(statusCode int)

WriteHeader writes the header

func (*ResponseWriter) WriteString

func (r *ResponseWriter) WriteString(s string) (int, error)

WriteString writes string data to response

Jump to

Keyboard shortcuts

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