gzip

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2021 License: MIT Imports: 9 Imported by: 620

README

GZIP gin's middleware

Run Tests codecov Go Report Card GoDoc

Gin middleware to enable GZIP support.

Usage

Download and install it:

go get github.com/gin-contrib/gzip

Import it in your code:

import "github.com/gin-contrib/gzip"

Canonical example:

package main

import (
  "fmt"
  "net/http"
  "time"

  "github.com/gin-contrib/gzip"
  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()
  r.Use(gzip.Gzip(gzip.DefaultCompression))
  r.GET("/ping", func(c *gin.Context) {
    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
  })

  // Listen and Server in 0.0.0.0:8080
  if err := r.Run(":8080"); err != nil {
    log.Fatal(err)
  }
}

Customized Excluded Extensions

package main

import (
  "fmt"
  "net/http"
  "time"

  "github.com/gin-contrib/gzip"
  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()
  r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedExtensions([]string{".pdf", ".mp4"})))
  r.GET("/ping", func(c *gin.Context) {
    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
  })

  // Listen and Server in 0.0.0.0:8080
  if err := r.Run(":8080"); err != nil {
    log.Fatal(err)
  }
}

Customized Excluded Paths

package main

import (
  "fmt"
  "net/http"
  "time"

  "github.com/gin-contrib/gzip"
  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()
  r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{"/api/"})))
  r.GET("/ping", func(c *gin.Context) {
    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
  })

  // Listen and Server in 0.0.0.0:8080
  if err := r.Run(":8080"); err != nil {
    log.Fatal(err)
  }
}

Customized Excluded Paths

package main

import (
  "fmt"
  "net/http"
  "time"

  "github.com/gin-contrib/gzip"
  "github.com/gin-gonic/gin"
)

func main() {
  r := gin.Default()
  r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPathsRegexs([]string{".*"})))
  r.GET("/ping", func(c *gin.Context) {
    c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
  })

  // Listen and Server in 0.0.0.0:8080
  if err := r.Run(":8080"); err != nil {
    log.Fatal(err)
  }
}

Documentation

Index

Constants

View Source
const (
	BestCompression    = gzip.BestCompression
	BestSpeed          = gzip.BestSpeed
	DefaultCompression = gzip.DefaultCompression
	NoCompression      = gzip.NoCompression
)

Variables

View Source
var (
	DefaultExcludedExtentions = NewExcludedExtensions([]string{
		".png", ".gif", ".jpeg", ".jpg",
	})
	DefaultOptions = &Options{
		ExcludedExtensions: DefaultExcludedExtentions,
	}
)

Functions

func DefaultDecompressHandle added in v0.0.2

func DefaultDecompressHandle(c *gin.Context)

func Gzip

func Gzip(level int, options ...Option) gin.HandlerFunc

Types

type ExcludedExtensions added in v0.0.2

type ExcludedExtensions map[string]bool

Using map for better lookup performance

func NewExcludedExtensions added in v0.0.2

func NewExcludedExtensions(extensions []string) ExcludedExtensions

func (ExcludedExtensions) Contains added in v0.0.2

func (e ExcludedExtensions) Contains(target string) bool

type ExcludedPathesRegexs added in v0.0.2

type ExcludedPathesRegexs []*regexp.Regexp

func NewExcludedPathesRegexs added in v0.0.2

func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs

func (ExcludedPathesRegexs) Contains added in v0.0.2

func (e ExcludedPathesRegexs) Contains(requestURI string) bool

type ExcludedPaths added in v0.0.2

type ExcludedPaths []string

func NewExcludedPaths added in v0.0.2

func NewExcludedPaths(paths []string) ExcludedPaths

func (ExcludedPaths) Contains added in v0.0.2

func (e ExcludedPaths) Contains(requestURI string) bool

type Option added in v0.0.2

type Option func(*Options)

func WithDecompressFn added in v0.0.2

func WithDecompressFn(decompressFn func(c *gin.Context)) Option

func WithExcludedExtensions added in v0.0.2

func WithExcludedExtensions(args []string) Option

func WithExcludedPaths added in v0.0.2

func WithExcludedPaths(args []string) Option

func WithExcludedPathsRegexs added in v0.0.2

func WithExcludedPathsRegexs(args []string) Option

type Options added in v0.0.2

type Options struct {
	ExcludedExtensions   ExcludedExtensions
	ExcludedPaths        ExcludedPaths
	ExcludedPathesRegexs ExcludedPathesRegexs
	DecompressFn         func(c *gin.Context)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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