mirror of https://github.com/gin-gonic/gin.git
Refactors Context initialization
This commit is contained in:
parent
df3ed787e1
commit
4a37b0808b
23
context.go
23
context.go
|
@ -22,15 +22,18 @@ 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
|
Params httprouter.Params
|
||||||
Engine *Engine
|
Input inputHolder
|
||||||
handlers []HandlerFunc
|
handlers []HandlerFunc
|
||||||
index int8
|
index int8
|
||||||
|
|
||||||
|
Keys map[string]interface{}
|
||||||
|
Errors errorMsgs
|
||||||
accepted []string
|
accepted []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,20 +41,6 @@ type Context struct {
|
||||||
/********** 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
|
||||||
|
|
21
gin.go
21
gin.go
|
@ -68,12 +68,27 @@ func Default() *Engine {
|
||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) allocateContext() (c *Context) {
|
func (engine *Engine) allocateContext() (context *Context) {
|
||||||
c = &Context{Engine: engine}
|
context = &Context{Engine: engine}
|
||||||
c.Writer = &c.writermem
|
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}
|
||||||
|
|
|
@ -10,16 +10,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTMLDebugRender struct {
|
type HTMLDebugRender struct {
|
||||||
files []string
|
Files []string
|
||||||
globs []string
|
Glob 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...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 len(r.Files) > 0 {
|
||||||
if _, err := t.ParseFiles(r.files...); err != nil {
|
if _, err := t.ParseFiles(r.Files...); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, glob := range r.globs {
|
if len(r.Glob) > 0 {
|
||||||
if _, err := t.ParseGlob(glob); err != nil {
|
if _, err := t.ParseGlob(r.Glob); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue