gzip

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2022 License: MIT Imports: 9 Imported by: 1

README

GZIP zoox's middleware

Run Tests codecov Go Report Card GoDoc

Zoox middleware to enable GZIP support.

Usage

Download and install it:

go get github.com/go-zoox/gzip

Import it in your code:

import "github.com/go-zoox/gzip"

Canonical example:

package main

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

  "github.com/go-zoox/gzip"
  "github.com/go-zoox/zoox"
)

func main() {
  r := zoox.New()
  r.Use(gzip.Gzip(gzip.DefaultCompression))
  r.Get("/ping", func(c *zoox.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/go-zoox/gzip"
  "github.com/go-zoox/zoox"
)

func main() {
  r := zoox.Default()
  r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedExtensions([]string{".pdf", ".mp4"})))
  r.Get("/ping", func(c *zoox.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/go-zoox/gzip"
  "github.com/go-zoox/zoox"
)

func main() {
  r := zoox.Default()
  r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPaths([]string{"/api/"})))
  r.Get("/ping", func(c *zoox.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/go-zoox/gzip"
  "github.com/go-zoox/zoox"
)

func main() {
  r := zoox.Default()
  r.Use(gzip.Gzip(gzip.DefaultCompression, gzip.WithExcludedPathsRegexs([]string{".*"})))
  r.Get("/ping", func(c *zoox.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)
  }
}

Thanks

  • This project is modified from gin-gzip.

License

GoZoox is released under the MIT License.

Documentation

Index

Constants

View Source
const (
	// BestCompression is the level of best compression.
	BestCompression = gzip.BestCompression
	// BestSpeed is the level of best speed.
	BestSpeed = gzip.BestSpeed
	// DefaultCompression is the default compression level.
	DefaultCompression = gzip.DefaultCompression
	// NoCompression is the level of no compression.
	NoCompression = gzip.NoCompression
)

Variables

View Source
var (
	// DefaultExcludedExtentions is the default excluded extensions.
	DefaultExcludedExtentions = NewExcludedExtensions([]string{
		".png", ".gif", ".jpeg", ".jpg",
	})
	// DefaultOptions is the default options for gzip middleware.
	DefaultOptions = &Options{
		ExcludedExtensions: DefaultExcludedExtentions,
	}
)
View Source
var Version = "1.0.0"

Version is the current version of the package.

Functions

func DefaultDecompressHandle

func DefaultDecompressHandle(c *zoox.Context)

DefaultDecompressHandle is the default decompress handle.

func Gzip

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

Gzip is a gzip middleware for zoox

Types

type ExcludedExtensions

type ExcludedExtensions map[string]bool

ExcludedExtensions is extensions map, using map for better lookup performance

func NewExcludedExtensions

func NewExcludedExtensions(extensions []string) ExcludedExtensions

NewExcludedExtensions creates a new ExcludedExtensions map.

func (ExcludedExtensions) Contains

func (e ExcludedExtensions) Contains(target string) bool

Contains returns true if the extension is excluded.

type ExcludedPathesRegexs

type ExcludedPathesRegexs []*regexp.Regexp

ExcludedPathesRegexs ...

func NewExcludedPathesRegexs

func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs

NewExcludedPathesRegexs creates a new ExcludedPathesRegexs.

func (ExcludedPathesRegexs) Contains

func (e ExcludedPathesRegexs) Contains(requestURI string) bool

Contains returns true if the path is excluded.

type ExcludedPaths

type ExcludedPaths []string

ExcludedPaths ...

func NewExcludedPaths

func NewExcludedPaths(paths []string) ExcludedPaths

NewExcludedPaths creates a new ExcludedPaths.

func (ExcludedPaths) Contains

func (e ExcludedPaths) Contains(requestURI string) bool

Contains returns true if the path is excluded.

type Option

type Option func(*Options)

Option is the type of function that can be passed to WithExcludedExtensions, WithExcludedPaths, WithExcludedPathsRegexs, WithDecompressFn.

func WithDecompressFn

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

WithDecompressFn is an option for setting a custom decompress function.

func WithExcludedExtensions

func WithExcludedExtensions(args []string) Option

WithExcludedExtensions is an option for excluding compression for certain file extensions.

func WithExcludedPaths

func WithExcludedPaths(args []string) Option

WithExcludedPaths is an option for excluding compression for certain paths.

func WithExcludedPathsRegexs

func WithExcludedPathsRegexs(args []string) Option

WithExcludedPathsRegexs is an option for excluding compression for certain paths.

type Options

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

Options is the options for gzip middleware.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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