forked from mirror/gin
Refactors Context initialization
This commit is contained in:
parent
df3ed787e1
commit
4a37b0808b
31
context.go
31
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
|
||||
|
|
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