-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
context.go
57 lines (50 loc) · 1.4 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
package golax
import "net/http"
// Context is a space to store information to be passed between interceptors and
// the final handler.
type Context struct {
Request *http.Request
Response *ExtendedWriter
Parameter string
Parameters map[string]string
LastError *ContextError
Scope map[string]interface{}
PathHandlers string
afters []Handler
deepInterceptors []*Interceptor
}
// ContextError is the error passed back when context.Error is called. It can be
// used inside an interceptor or a handler
type ContextError struct {
StatusCode int `json:"status_code"`
ErrorCode int `json:"error_code"`
Description string `json:"description_code"`
}
// NewContext instances and initializes a Context
func NewContext() *Context {
return &Context{
LastError: nil,
Parameters: map[string]string{},
Scope: map[string]interface{}{},
afters: []Handler{},
deepInterceptors: []*Interceptor{},
}
}
func (c *Context) Error(s int, d string) *ContextError {
c.Response.WriteHeader(s)
e := &ContextError{
StatusCode: s,
Description: d,
}
c.LastError = e
return e
}
// Set stores a key-value tuple inside a Context
func (c *Context) Set(k string, v interface{}) {
c.Scope[k] = v
}
// Get retrieves a value from a Context
func (c *Context) Get(k string) (interface{}, bool) {
a, b := c.Scope[k]
return a, b
}