-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
68 lines (61 loc) · 2.04 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
// Copyright 2015 Afshin Darian. All rights reserved.
// Use of this source code is governed by The MIT License
// that can be found in the LICENSE file.
package bear
import "net/http"
// Context is state of each request.
type Context struct {
// Params is a map of string keys with string values that is populated
// by the dynamic URL parameters (if any).
// Wildcard params are accessed by using an asterisk: Params["*"]
Params map[string]string
handler int
mux *Mux
// Request is the same as the *http.Request that all handlers receive
// and is referenced in Context for convenience.
Request *http.Request
// ResponseWriter is the same as the http.ResponseWriter that all handlers
// receive and is referenced in Context for convenience.
ResponseWriter http.ResponseWriter
state map[string]interface{}
tree *tree
}
// Get allows retrieving a state value (interface{})
func (ctx *Context) Get(key string) interface{} {
if nil == ctx.state {
return nil
}
return ctx.state[key]
}
// Next calls the next middleware (if any) that was registered as a handler for
// a particular request pattern.
func (ctx *Context) Next() {
always := len(ctx.mux.always)
handlers := len(ctx.tree.handlers)
ctx.handler++
if always > 0 && ctx.handler < always {
index := ctx.handler
ctx.mux.always[index](ctx.ResponseWriter, ctx.Request, ctx)
return
}
if ctx.handler-always < handlers {
index := ctx.handler - always
ctx.tree.handlers[index](ctx.ResponseWriter, ctx.Request, ctx)
}
}
func (ctx *Context) param(key string, value string, capacity int) {
if nil == ctx.Params {
ctx.Params = make(map[string]string, capacity)
}
ctx.Params[key] = value[:len(value)-1]
}
// Set allows setting an arbitrary value (interface{}) to a string key
// to allow one middleware to pass information to the next.
// It returns a pointer to the current Context to allow chaining.
func (ctx *Context) Set(key string, value interface{}) *Context {
if nil == ctx.state {
ctx.state = make(map[string]interface{})
}
ctx.state[key] = value
return ctx
}