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,
|
||||
// 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
|
||||
Input inputHolder
|
||||
handlers []HandlerFunc
|
||||
index int8
|
||||
|
||||
Keys map[string]interface{}
|
||||
Errors errorMsgs
|
||||
accepted []string
|
||||
}
|
||||
|
||||
|
@ -38,20 +41,6 @@ type Context struct {
|
|||
/********** 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
|
||||
|
|
21
gin.go
21
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}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue