Browse Source

Refactors Context initialization

Manu Mtz-Almeida 10 years ago
parent
commit
4a37b0808b
3 changed files with 34 additions and 38 deletions
  1. 10 21
      context.go
  2. 18 3
      gin.go
  3. 6 14
      render/html_debug.go

+ 10 - 21
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,
 // 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.
 // manage the flow, validate the JSON of a request and render a JSON response for example.
 type Context struct {
 type Context struct {
+	Engine    *Engine
 	writermem responseWriter
 	writermem responseWriter
 	Request   *http.Request
 	Request   *http.Request
 	Writer    ResponseWriter
 	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 ********/
 /********** 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() {
 func (c *Context) reset() {
 	c.Keys = nil
 	c.Keys = nil
 	c.index = -1
 	c.index = -1

+ 18 - 3
gin.go

@@ -68,12 +68,27 @@ func Default() *Engine {
 	return 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
 	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) {
 func (engine *Engine) LoadHTMLGlob(pattern string) {
 	if IsDebugging() {
 	if IsDebugging() {
 		r := &render.HTMLDebugRender{Glob: pattern}
 		r := &render.HTMLDebugRender{Glob: pattern}

+ 6 - 14
render/html_debug.go

@@ -10,16 +10,8 @@ import (
 )
 )
 
 
 type HTMLDebugRender struct {
 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 {
 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) {
 func (r *HTMLDebugRender) newTemplate() (*template.Template, error) {
 	t := template.New("")
 	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
 			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
 			return nil, err
 		}
 		}
 	}
 	}