Gzip
Gzip 压缩很简单。
想要为所有响应和模板引擎启用 Gzip 压缩,仅需设置 iris.Config.Gzip = true
或 iris.New(iris.OptionGzip(true))
或 iris.Set(OptionGzip(true))
,你也可以为特定的 Render()
启用 Gzip 压缩:
//...
context.Render("mytemplate.html", bindingStruct{}, iris.RenderOptions{"gzip": false})
context.Render("my-custom-response", iris.Map{"anything":"everything"} , iris.RenderOptions{"gzip": false})
// WriteGzip accepts bytes, which are compressed to gzip format and sent to the client.
// returns the number of bytes written and an error ( if the client doesn' supports gzip compression)
WriteGzip(b []byte) (int, error)
// TryWriteGzip accepts bytes, which are compressed to gzip format and sent to the client.
// If client does not supprots gzip then the contents are written as they are, uncompressed.
TryWriteGzip(b []byte) (int, error)
如何用:
iris.Get("/", func(ctx *iris.Context){
ctx.WriteGzip([]byte("my gziped compressed content here"))
})
其它