From 6f91e6c588398f5ee25732c28e2a990ca1c92373 Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Tue, 19 Jul 2016 06:50:49 +0300 Subject: [PATCH] Add option for Gzip again, I removed it after v3 but seems users wants it back --- HISTORY.md | 4 ++-- config/iris.go | 6 ++++++ response.go | 4 ++-- template.go | 9 +++++---- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index f9b58054..f1f70843 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -25,8 +25,8 @@ A **Response Engine** gives you the freedom to create/change the render/response **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.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** diff --git a/config/iris.go b/config/iris.go index e30c6d5f..f31083d7 100644 --- a/config/iris.go +++ b/config/iris.go @@ -94,6 +94,11 @@ type ( // defaults to "UTF-8" 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 Sessions @@ -114,6 +119,7 @@ func Default() Iris { DisableTemplateEngines: false, IsDevelopment: false, Charset: DefaultCharset, + Gzip: false, ProfilePath: "", Sessions: DefaultSessions(), Websocket: DefaultWebsocket(), diff --git a/response.go b/response.go index 31d526c1..439af5cf 100644 --- a/response.go +++ b/response.go @@ -135,10 +135,10 @@ func (r *responseEngineMap) render(ctx *Context, obj interface{}, options ...map finalResult = append(finalResult, result...) } - gzipEnabled := false + gzipEnabled := ctx.framework.Config.Gzip charset := ctx.framework.Config.Charset 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 != "" { charset = chs } diff --git a/template.go b/template.go index fc2f818a..cd531e0a 100644 --- a/template.go +++ b/template.go @@ -71,12 +71,12 @@ func (t TemplateFuncs) IsFree(key string) bool { 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. if b, isBool := gzipOpt.(bool); isBool { return b } - return false + return ctx.framework.Config.Gzip } 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... - gzipEnabled := false + gzipEnabled := ctx.framework.Config.Gzip charset := ctx.framework.Config.Charset if len(options) > 0 { - gzipEnabled = getGzipOption(options[0]) + gzipEnabled = getGzipOption(ctx, options[0]) + if chs := getCharsetOption(options[0]); chs != "" { charset = chs }