gzip

The gzip middleware provides Gzip compression to responses for Flame instances.

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

Installation

The minimum requirement of Go is 1.16.

  1. go get github.com/flamego/gzip

Usage examples

You should register the gzip.Gzipgzip - 图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/flamego"
  4. "github.com/flamego/gzip"
  5. )
  6. func main() {
  7. f := flamego.Classic()
  8. f.Use(gzip.Gzip())
  9. f.Get("/", func() string {
  10. return "Hello, Gzip!"
  11. })
  12. f.Run()
  13. }

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

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