brotli

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2020 License: MIT Imports: 10 Imported by: 0

README

gin-cbrotli

Build Status codecov Go Report Card GoDoc

Google Brotli Gin Middleware

Dependency

Debian/Ubuntu

sudo apt-get install libbrotli-dev

RHEL/CentOS

yum install brotli-devel

Alpine

apk add brotli-dev

MacOS (with homebrew)

brew install brotli

Windows (with msys2)

pacman -S mingw-w64-x86_64-brotli

OTHERS

Usage

Download and install it:

go get github.com/freyo/gin-cbrotli

Import it in your code:

import "github.com/freyo/gin-cbrotli"

Canonical example:

package main

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

	"github.com/freyo/gin-cbrotli"
	"github.com/gin-gonic/gin"
	"github.com/google/brotli/go/cbrotli"
)

func main() {
	r := gin.Default()
	r.Use(brotli.Brotli(cbrotli.WriterOptions{Quality: 5}))
	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
	r.Run(":8080")
}

Work with Gzip example:

package main

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

	"github.com/freyo/gin-cbrotli"
	"github.com/gin-contrib/gzip"
	"github.com/gin-gonic/gin"
	"github.com/google/brotli/go/cbrotli"
)

func main() {
	r := gin.Default()
	r.Use(func(context *gin.Context) {
		ae := context.Request.Header.Get("Accept-Encoding")
		switch true {
		case strings.Contains(ae, "br"):
			context.Request.Header.Set("Accept-Encoding", "br")
		case strings.Contains(ae, "gzip"):
			context.Request.Header.Set("Accept-Encoding", "gzip")
		}
		context.Next()
	})
	r.Use(gzip.Gzip(gzip.DefaultCompression))
	r.Use(brotli.Brotli(cbrotli.WriterOptions{Quality: 5}))
	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
	r.Run(":8080")
}

Documentation

Index

Constants

This section is empty.

Variables

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

Functions

func Brotli

func Brotli(brOption cbrotli.WriterOptions, options ...Option) gin.HandlerFunc

func DefaultDecompressHandle

func DefaultDecompressHandle(c *gin.Context)

Types

type ExcludedExtensions

type ExcludedExtensions map[string]bool

Using map for better lookup performance

func NewExcludedExtensions

func NewExcludedExtensions(extensions []string) ExcludedExtensions

func (ExcludedExtensions) Contains

func (e ExcludedExtensions) Contains(target string) bool

type ExcludedPathesRegexs

type ExcludedPathesRegexs []*regexp.Regexp

func NewExcludedPathesRegexs

func NewExcludedPathesRegexs(regexs []string) ExcludedPathesRegexs

func (ExcludedPathesRegexs) Contains

func (e ExcludedPathesRegexs) Contains(requestURI string) bool

type ExcludedPaths

type ExcludedPaths []string

func NewExcludedPaths

func NewExcludedPaths(paths []string) ExcludedPaths

func (ExcludedPaths) Contains

func (e ExcludedPaths) Contains(requestURI string) bool

type Option

type Option func(*Options)

func WithDecompressFn

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

func WithExcludedExtensions

func WithExcludedExtensions(args []string) Option

func WithExcludedPaths

func WithExcludedPaths(args []string) Option

func WithExcludedPathsRegexs

func WithExcludedPathsRegexs(args []string) Option

type Options

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