Add option for Gzip again, I removed it after v3 but seems users wants it back

This commit is contained in:
Gerasimos Maropoulos 2016-07-19 06:50:49 +03:00
parent 084b689d37
commit 6f91e6c588
4 changed files with 15 additions and 8 deletions

View File

@ -25,8 +25,8 @@ A **Response Engine** gives you the freedom to create/change the render/response
**Small changes** **Small changes**
- `iris.Config.Charset`, before alpha.3 was `iris.Config.Rest.Charset` & `iris.Config.Render.Template.Charset`, but you can override it at runtime by passinth a map `iris.RenderOptions` on the `context.Render` call . - `iris.Config.Charset`, before alpha.3 was `iris.Config.Rest.Charset` & `iris.Config.Render.Template.Charset`, but you can override it at runtime by passinth a map `iris.RenderOptions` on the `context.Render` call .
- `iris.Config.IsDevelopment` , before alpha.1 was `iris.Config.Render.Template.IsDevelopment` - `iris.Config.IsDevelopment`, before alpha.1 was `iris.Config.Render.Template.IsDevelopment`
- `iris.Config.Gzip`, enables gzip compression on your Render actions, this includes any type of render, templates and pure/raw content. If you don't want to enable it globaly, you could just use the third parameter on context.Render("myfileOrResponse", structBinding{}, iris.RenderOptions{"gzip": true}). It defaults to false
**Websockets changes** **Websockets changes**

View File

@ -94,6 +94,11 @@ type (
// defaults to "UTF-8" // defaults to "UTF-8"
Charset string Charset string
// Gzip enables gzip compression on your Render actions, this includes any type of render, templates and pure/raw content
// If you don't want to enable it globaly, you could just use the third parameter on context.Render("myfileOrResponse", structBinding{}, iris.RenderOptions{"gzip": true})
// defaults to false
Gzip bool
// Sessions contains the configs for sessions // Sessions contains the configs for sessions
Sessions Sessions Sessions Sessions
@ -114,6 +119,7 @@ func Default() Iris {
DisableTemplateEngines: false, DisableTemplateEngines: false,
IsDevelopment: false, IsDevelopment: false,
Charset: DefaultCharset, Charset: DefaultCharset,
Gzip: false,
ProfilePath: "", ProfilePath: "",
Sessions: DefaultSessions(), Sessions: DefaultSessions(),
Websocket: DefaultWebsocket(), Websocket: DefaultWebsocket(),

View File

@ -135,10 +135,10 @@ func (r *responseEngineMap) render(ctx *Context, obj interface{}, options ...map
finalResult = append(finalResult, result...) finalResult = append(finalResult, result...)
} }
gzipEnabled := false gzipEnabled := ctx.framework.Config.Gzip
charset := ctx.framework.Config.Charset charset := ctx.framework.Config.Charset
if len(options) > 0 { if len(options) > 0 {
gzipEnabled = getGzipOption(options[0]) // located to the template.go below the RenderOptions gzipEnabled = getGzipOption(ctx, options[0]) // located to the template.go below the RenderOptions
if chs := getCharsetOption(options[0]); chs != "" { if chs := getCharsetOption(options[0]); chs != "" {
charset = chs charset = chs
} }

View File

@ -71,12 +71,12 @@ func (t TemplateFuncs) IsFree(key string) bool {
return true return true
} }
func getGzipOption(options map[string]interface{}) bool { func getGzipOption(ctx *Context, options map[string]interface{}) bool {
gzipOpt := options["gzip"] // we only need that, so don't create new map to keep the options. gzipOpt := options["gzip"] // we only need that, so don't create new map to keep the options.
if b, isBool := gzipOpt.(bool); isBool { if b, isBool := gzipOpt.(bool); isBool {
return b return b
} }
return false return ctx.framework.Config.Gzip
} }
func getCharsetOption(options map[string]interface{}) string { func getCharsetOption(options map[string]interface{}) string {
@ -166,10 +166,11 @@ func (t *templateEngineWrapper) execute(ctx *Context, filename string, binding i
} }
// we do all these because we don't want to initialize a new map for each execution... // we do all these because we don't want to initialize a new map for each execution...
gzipEnabled := false gzipEnabled := ctx.framework.Config.Gzip
charset := ctx.framework.Config.Charset charset := ctx.framework.Config.Charset
if len(options) > 0 { if len(options) > 0 {
gzipEnabled = getGzipOption(options[0]) gzipEnabled = getGzipOption(ctx, options[0])
if chs := getCharsetOption(options[0]); chs != "" { if chs := getCharsetOption(options[0]); chs != "" {
charset = chs charset = chs
} }