brotli

The brotli middleware provides Brotli compression to responses for Flame instances.

You can read source code of this middleware on GitHubbrotli - 图1open in new window and API documentation on pkg.go.devbrotli - 图2open in new window.

Installation

The minimum requirement of Go is 1.16.

  1. go get github.com/flamego/brotli

Usage examples

You should register the brotli.Brotlibrotli - 图3open in new window before all other middleware or handlers that would write response to clients, and it works out-of-the-box with the default settings:

  1. package main
  2. import (
  3. "github.com/flamego/brotli"
  4. "github.com/flamego/flamego"
  5. )
  6. func main() {
  7. f := flamego.Classic()
  8. f.Use(brotli.Brotli())
  9. f.Get("/", func() string {
  10. return "Hello, Brotli!"
  11. })
  12. f.Run()
  13. }

The brotli.Optionsbrotli - 图4open in new window can be used to further customize the behavior of the middleware:

  1. package main
  2. import (
  3. "github.com/flamego/brotli"
  4. "github.com/flamego/flamego"
  5. )
  6. func main() {
  7. f := flamego.Classic()
  8. f.Use(brotli.Brotli(
  9. brotli.Options{
  10. CompressionLevel: 11, // Best compression
  11. },
  12. ))
  13. f.Get("/", func() string {
  14. return "Hello, Brotli!"
  15. })
  16. f.Run()
  17. }