gbrotli

package module
v0.0.0-...-12bf532 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2022 License: MIT Imports: 6 Imported by: 11

README

Brotli gin's middleware

Gin middleware to enable Brotli support.

NOTE: this repo is an adaptation of how gzip middleware is implemented. I'll try to add new features.

Requirements

Install Brotli, see here.

Install brotli package for go (cbrotli). Copy github.com/google/brotli/tree/master/go/cbrotli package into GOPATH/ directory

[Update] gin-brotli does not depend on cbrotli installed. Now it uses brotli from andybalholm/brotli

Install

go get github.com/anargu/gin-brotli

How to use

package main

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

    brotli "github.com/anargu/gin-brotli"
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()
    r.Use(brotli.Brotli(brotli.DefaultCompression))
    r.GET("/hello", func(c *gin.Context) {
        c.String(http.StatusOK, fmt.Sprintf("World at %s", time.Now()))
    })

    // Listen and Server in 0.0.0.0:8080
    r.Run(":8080")
}

Test it

cd example/

go run example.go

In Another terminal

curl -X GET http://localhost:8080/json

TODO

  • Add fallback feature: If brotli is not supported in browser then the request will be handled by gzip compression. And if it's not supported by the browser yet, the request is going to be send as is (without compression).

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultCompression Quality: 4 LGWin: 11
	// from 0-11. 4 will bring the files faster than 11. the higher quality the slower compression time
	DefaultCompression = Options{
		WriterOptions: brotli.WriterOptions{
			Quality: 4,
			LGWin:   11,
		},
		SkipExtensions: []string{".png", ".gif", ".jpeg", ".jpg", ".mp3", ".mp4"},
	}
)

Functions

func Brotli

func Brotli(options Options) gin.HandlerFunc

Brotli is a middleware function

Types

type Options

type Options struct {
	brotli.WriterOptions
	SkipExtensions []string
}

Options is a wrapper for cbrotli.WriterOptions

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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