-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcontext.go
134 lines (117 loc) · 3.62 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package neko
import (
"errors"
"github.com/julienschmidt/httprouter"
"github.com/rocwong/neko/render"
"log"
"math"
"net/http"
)
type Context struct {
Writer ResponseWriter
Req *http.Request
Session Session
Keys map[string]interface{}
Params routerParams
Engine *Engine
writer writer
handlers []HandlerFunc
index int8
HtmlEngine
}
const (
abortIndex = math.MaxInt8 / 2
)
// Next should be used only in the middlewares.
// It executes the pending handlers in the chain inside the calling handler.
func (c *Context) Next() {
c.index++
s := int8(len(c.handlers))
for ; c.index < s; c.index++ {
c.handlers[c.index](c)
}
}
// Sets a new pair key/value just for the specified context.
func (c *Context) Set(key string, item interface{}) {
if c.Keys == nil {
c.Keys = make(map[string]interface{})
}
c.Keys[key] = item
}
// Get returns the value for the given key or an error if the key does not exist.
func (c *Context) Get(key string) (interface{}, error) {
if c.Keys != nil {
value, ok := c.Keys[key]
if ok {
return value, nil
}
}
return nil, errors.New("Key does not exist.")
}
// MustGet returns the value for the given key or panics if the value doesn't exist.
func (c *Context) MustGet(key string) interface{} {
value, err := c.Get(key)
if err != nil || value == nil {
log.Panicf("Key %s doesn't exist", value)
}
return value
}
// SetHeader sets a response header.
func (c *Context) SetHeader(key, value string) {
c.Writer.Header().Set(key, value)
}
// Forces the system to do not continue calling the pending handlers in the chain.
func (c *Context) Abort() {
c.index = abortIndex
}
// Redirect returns a HTTP redirect to the specific location. default for 302
func (c *Context) Redirect(location string, status ...int) {
c.SetHeader("Location", location)
if status != nil {
http.Redirect(c.Writer, c.Req, location, status[0])
} else {
http.Redirect(c.Writer, c.Req, location, 302)
}
}
// Serializes the given struct as JSON into the response body in a fast and efficient way.
// It also sets the Content-Type as "application/json".
func (c *Context) Json(data interface{}, status ...int) {
c.executeRender(data, c.Writer, render.JSON{}, status...)
}
// Serializes the given struct as JSONP into the response body in a fast and efficient way.
// It also sets the Content-Type as "application/javascript".
func (c *Context) Jsonp(callback string, data interface{}, status ...int) {
c.executeRender(data, c.Writer, render.JSONP{Callback: callback}, status...)
}
// Serializes the given struct as XML into the response body in a fast and efficient way.
// It also sets the Content-Type as "application/xml".
func (c *Context) Xml(data interface{}, status ...int) {
c.executeRender(data, c.Writer, render.XML{}, status...)
}
// Writes the given string into the response body and sets the Content-Type to "text/plain".
func (c *Context) Text(data string, status ...int) {
c.executeRender(data, c.Writer, render.TEXT{}, status...)
}
func (c *Context) executeRender(data interface{}, w http.ResponseWriter, render render.Render, status ...int) {
if status != nil {
c.Writer.WriteHeader(status[0])
}
if err := render.Render(data, w); err != nil {
c.Writer.WriteHeader(500)
c.Abort()
}
}
func (c *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context {
ctx := c.pool.Get().(*Context)
ctx.Writer = &ctx.writer
ctx.Req = req
ctx.Keys = nil
ctx.Params = routerParams{req: req, params: params}
ctx.handlers = handlers
ctx.writer.reset(w)
ctx.index = -1
return ctx
}
func (c *Engine) reuseContext(ctx *Context) {
c.pool.Put(ctx)
}