From 4a37b0808bdfbfc76ba2b727b44d4d147a972f1b Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Tue, 31 Mar 2015 17:39:30 +0200 Subject: [PATCH] Refactors Context initialization --- context.go | 31 ++++++++++--------------------- gin.go | 21 ++++++++++++++++++--- render/html_debug.go | 20 ++++++-------------- 3 files changed, 34 insertions(+), 38 deletions(-) diff --git a/context.go b/context.go index 2b7b1a9b..39f09135 100644 --- a/context.go +++ b/context.go @@ -22,36 +22,25 @@ const AbortIndex = math.MaxInt8 / 2 // Context is the most important part of gin. It allows us to pass variables between middleware, // manage the flow, validate the JSON of a request and render a JSON response for example. type Context struct { + Engine *Engine writermem responseWriter Request *http.Request Writer ResponseWriter - Keys map[string]interface{} - Errors errorMsgs - Params httprouter.Params - Engine *Engine - handlers []HandlerFunc - index int8 - accepted []string + + Params httprouter.Params + Input inputHolder + handlers []HandlerFunc + index int8 + + Keys map[string]interface{} + Errors errorMsgs + accepted []string } /************************************/ /********** CONTEXT CREATION ********/ /************************************/ -func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context { - c := engine.pool.Get().(*Context) - c.reset() - c.writermem.reset(w) - c.Request = req - c.Params = params - c.handlers = handlers - return c -} - -func (engine *Engine) reuseContext(c *Context) { - engine.pool.Put(c) -} - func (c *Context) reset() { c.Keys = nil c.index = -1 diff --git a/gin.go b/gin.go index c126ae69..6fdb1561 100644 --- a/gin.go +++ b/gin.go @@ -68,12 +68,27 @@ func Default() *Engine { return engine } -func (engine *Engine) allocateContext() (c *Context) { - c = &Context{Engine: engine} - c.Writer = &c.writermem +func (engine *Engine) allocateContext() (context *Context) { + context = &Context{Engine: engine} + context.Writer = &context.writermem + context.Input = inputHolder{context: context} return } +func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context { + c := engine.pool.Get().(*Context) + c.reset() + c.writermem.reset(w) + c.Request = req + c.Params = params + c.handlers = handlers + return c +} + +func (engine *Engine) reuseContext(c *Context) { + engine.pool.Put(c) +} + func (engine *Engine) LoadHTMLGlob(pattern string) { if IsDebugging() { r := &render.HTMLDebugRender{Glob: pattern} diff --git a/render/html_debug.go b/render/html_debug.go index 3c6426e7..1edac5df 100644 --- a/render/html_debug.go +++ b/render/html_debug.go @@ -10,16 +10,8 @@ import ( ) type HTMLDebugRender struct { - files []string - globs []string -} - -func (r *HTMLDebugRender) AddGlob(pattern string) { - r.globs = append(r.globs, pattern) -} - -func (r *HTMLDebugRender) AddFiles(files ...string) { - r.files = append(r.files, files...) + Files []string + Glob string } func (r *HTMLDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { @@ -36,13 +28,13 @@ func (r *HTMLDebugRender) Render(w http.ResponseWriter, code int, data ...interf func (r *HTMLDebugRender) newTemplate() (*template.Template, error) { t := template.New("") - if len(r.files) > 0 { - if _, err := t.ParseFiles(r.files...); err != nil { + if len(r.Files) > 0 { + if _, err := t.ParseFiles(r.Files...); err != nil { return nil, err } } - for _, glob := range r.globs { - if _, err := t.ParseGlob(glob); err != nil { + if len(r.Glob) > 0 { + if _, err := t.ParseGlob(r.Glob); err != nil { return nil, err } }