gzip
The gzip middleware provides Gzip compression to responses for Flame instances.
You can read source code of this middleware on GitHubopen in new window and API documentation on pkg.go.devopen in new window.
Installation
The minimum requirement of Go is 1.16.
go get github.com/flamego/gzip
Usage examples
You should register the gzip.Gzip
open 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:
package main
import (
"github.com/flamego/flamego"
"github.com/flamego/gzip"
)
func main() {
f := flamego.Classic()
f.Use(gzip.Gzip())
f.Get("/", func() string {
return "Hello, Gzip!"
})
f.Run()
}
The gzip.Options
open in new window can be used to further customize the behavior of the middleware:
package main
import (
"github.com/flamego/flamego"
"github.com/flamego/gzip"
)
func main() {
f := flamego.Classic()
f.Use(gzip.Gzip(
gzip.Options{
CompressionLevel: 9, // Best compression
},
))
f.Get("/", func() string {
return "Hello, Gzip!"
})
f.Run()
}