-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
186 lines (157 loc) · 4.42 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package chain
import (
"context"
"net/http"
)
type chainContextKey struct{}
type bodyBytesKey struct{}
// ContextKey is the request context key under which URL params are stored.
var ContextKey = chainContextKey{}
// BodyBytesKey indicates a default body bytes key.
var BodyBytesKey = bodyBytesKey{}
// GetContext pulls the URL parameters from a request context, or returns nil if none are present.
func GetContext(ctx context.Context) *Context {
p, _ := ctx.Value(ContextKey).(*Context)
return p
}
// Context represents a request & response Context.
type Context struct {
paramCount int
pathSegmentsCount int
pathSegments [32]int
path string
paramNames [32]string
paramValues [32]string
data map[any]any
handler Handle
router *Router
Route *RouteInfo
Writer http.ResponseWriter
Request *http.Request
Crypto *cryptoImpl
parent *Context
index int
children []*Context
}
// Set define um valor compartilhado no contexto de execução da requisição
func (ctx *Context) Set(key any, value any) {
if ctx.data == nil {
ctx.data = make(map[any]any)
}
ctx.data[key] = value
}
// Get obtém um valor compartilhado no contexto de execução da requisição
func (ctx *Context) Get(key any) (any, bool) {
if ctx.data != nil {
value, exists := ctx.data[key]
if exists {
return value, exists
}
}
if ctx.parent != nil {
return ctx.parent.Get(key)
}
return nil, false
}
func (ctx *Context) Destroy() {
if ctx.parent == nil {
// root context, will be removed automaticaly
return
}
if ctx.parent.children != nil {
ctx.parent.children[ctx.index] = nil
}
ctx.parent = nil
ctx.children = nil
if ctx.router != nil {
ctx.router.poolPutContext(ctx)
}
}
func (ctx *Context) Child() *Context {
var child *Context
if ctx.router != nil {
child = ctx.router.poolGetContext(ctx.Request, ctx.Writer, "")
} else {
child = &Context{
path: ctx.path,
Crypto: crypt,
Writer: ctx.Writer,
Request: ctx.Request,
handler: ctx.handler,
}
}
child.paramCount = ctx.paramCount
child.paramNames = ctx.paramNames
child.paramValues = ctx.paramValues
child.pathSegments = ctx.pathSegments
child.pathSegmentsCount = ctx.pathSegmentsCount
child.Route = ctx.Route
child.parent = ctx
if ctx.children == nil {
ctx.children = make([]*Context, 0)
}
child.index = len(ctx.children)
ctx.children = append(ctx.children, child)
return child
}
// func (ctx *Context) With(key any, value any) *Context {
// }
func (ctx *Context) WithParams(names []string, values []string) *Context {
child := ctx.Child()
child.paramCount = len(names)
child.paramNames = [32]string{}
child.paramValues = [32]string{}
for i, name := range ctx.paramNames {
child.paramNames[i] = name
child.paramValues[i] = ctx.paramValues[i]
}
for i := 0; i < len(names); i++ {
child.paramNames[i] = names[i]
child.paramValues[i] = values[i]
}
return child
}
// NewUID get a new KSUID.
//
// KSUID is for K-Sortable Unique IDentifier. It is a kind of globally unique identifier similar to a RFC 4122 UUID,
// built from the ground-up to be "naturally" sorted by generation timestamp without any special type-aware logic.
//
// See: https://github.com/segmentio/ksuid
func (ctx *Context) NewUID() (uid string) {
return NewUID()
}
// Router get current router reference
func (ctx *Context) Router() *Router {
return ctx.router
}
// BeforeSend Registers a callback to be invoked before the response is sent.
//
// Callbacks are invoked in the reverse order they are defined (callbacks defined first are invoked last).
func (ctx *Context) BeforeSend(callback func()) error {
if spy, is := ctx.Writer.(*ResponseWriterSpy); is {
return spy.beforeWriteHeader(callback)
}
return nil
}
func (ctx *Context) AfterSend(callback func()) error {
if spy, is := ctx.Writer.(*ResponseWriterSpy); is {
return spy.afterWrite(callback)
}
return nil
}
func (ctx *Context) write() {
if spy, is := ctx.Writer.(*ResponseWriterSpy); is {
if !spy.writeStarted {
ctx.WriteHeader(http.StatusOK)
}
}
}
// addParameter adds a new parameter to the Context.
func (ctx *Context) addParameter(name string, value string) {
ctx.paramNames[ctx.paramCount] = name
ctx.paramValues[ctx.paramCount] = value
ctx.paramCount++
}
func (ctx *Context) parsePathSegments() {
ctx.pathSegmentsCount = parsePathSegments(ctx.path, &ctx.pathSegments)
}