@@ -16,23 +16,27 @@ import (
16
16
* Context
17
17
*/
18
18
type Context struct {
19
- Request * http.Request
20
- Response http.ResponseWriter
21
- Query url.Values
22
- Body map [string ]interface {}
23
- Params map [string ]string
24
- data map [string ]interface {}
25
- err error
26
- status int
27
- found bool
28
- end bool
19
+ Request * http.Request
20
+ Response http.ResponseWriter
21
+ Query url.Values
22
+ Body map [string ]interface {}
23
+ Params map [string ]string
24
+ preSendTasks []func () error
25
+ postSendTasks []func () error
26
+ data map [string ]interface {}
27
+ err error
28
+ status int
29
+ found bool
30
+ end bool
29
31
}
30
32
31
33
/**
32
34
* Initialization of context on every request
33
35
*/
34
36
func (ctx * Context ) init () {
35
37
ctx .data = make (map [string ]interface {})
38
+ ctx .preSendTasks = make ([]func () error , 0 )
39
+ ctx .postSendTasks = make ([]func () error , 0 )
36
40
ctx .status = 200
37
41
ctx .found = false
38
42
ctx .end = false
@@ -134,6 +138,20 @@ func (ctx *Context) Text(data string) {
134
138
ctx .send (body , err )
135
139
}
136
140
141
+ /**
142
+ *
143
+ */
144
+ func (ctx * Context ) PreSend (task func () error ) {
145
+ ctx .preSendTasks = append (ctx .preSendTasks , task )
146
+ }
147
+
148
+ /**
149
+ *
150
+ */
151
+ func (ctx * Context ) PostSend (task func () error ) {
152
+ ctx .postSendTasks = append (ctx .postSendTasks , task )
153
+ }
154
+
137
155
//////////////////////////////////////////////////
138
156
/**
139
157
* Send data
@@ -148,6 +166,11 @@ func (ctx *Context) send(data []byte, err error) {
148
166
return
149
167
}
150
168
169
+ for _ , task := range ctx .preSendTasks {
170
+ //TODO: handle error
171
+ _ = task ()
172
+ }
173
+
151
174
ctx .Response .WriteHeader (ctx .status )
152
175
_ , err = ctx .Response .Write (data )
153
176
@@ -157,6 +180,11 @@ func (ctx *Context) send(data []byte, err error) {
157
180
return
158
181
}
159
182
183
+ for _ , task := range ctx .postSendTasks {
184
+ //TOD: handle error
185
+ _ = task ()
186
+ }
187
+
160
188
ctx .End ()
161
189
}
162
190
0 commit comments