Gzip
Middleware gzip provides compress to responses for Macaron Instances. Make sure to register it before other middlewares that write content to response.

Installation

1
go get github.com/go-macaron/gzip
Copied!

Usage

1
package main
2
3
import (
4
"github.com/go-macaron/gzip"
5
"gopkg.in/macaron.v1"
6
)
7
8
func main() {
9
m := macaron.Classic()
10
m.Use(gzip.Gziper())
11
// Register routers.
12
m.Run()
13
}
Copied!
In this case, the static files will not be compressed by Gzip, to compress them:
1
package main
2
3
import (
4
"github.com/go-macaron/gzip"
5
"gopkg.in/macaron.v1"
6
)
7
8
func main() {
9
m := macaron.New()
10
m.Use(macaron.Logger())
11
m.Use(macaron.Recovery())
12
m.Use(gzip.Gziper())
13
m.Use(macaron.Static("public"))
14
// Register routers.
15
m.Run()
16
}
Copied!
Or you can choose to only compress a group of routes' responses:
1
// ...
2
3
func main() {
4
m := macaron.Classic()
5
m.Group("/gzip", func() {
6
// ...
7
}, gzip.Gziper())
8
// ...
9
m.Run()
10
}
Copied!

Options

This service comes with a variety of configuration options(gzip.Options):
1
// ...
2
m.Use(gzip.Gziper(gzip.Options{
3
// Compression level. Can be DefaultCompression(-1), ConstantCompression(-2)
4
// or any integer value between BestSpeed(1) and BestCompression(9) inclusive.
5
// Default is 4.
6
CompressionLevel: 4,
7
}))
8
// ...
Copied!
Last modified 2yr ago
Copy link