@@ -16,23 +16,27 @@ import (
1616 * Context
1717 */
1818type 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
2931}
3032
3133/**
3234 * Initialization of context on every request
3335 */
3436func (ctx * Context ) init () {
3537 ctx .data = make (map [string ]interface {})
38+ ctx .preSendTasks = make ([]func () error , 0 )
39+ ctx .postSendTasks = make ([]func () error , 0 )
3640 ctx .status = 200
3741 ctx .found = false
3842 ctx .end = false
@@ -134,6 +138,20 @@ func (ctx *Context) Text(data string) {
134138 ctx .send (body , err )
135139}
136140
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+
137155//////////////////////////////////////////////////
138156/**
139157 * Send data
@@ -148,6 +166,11 @@ func (ctx *Context) send(data []byte, err error) {
148166 return
149167 }
150168
169+ for _ , task := range ctx .preSendTasks {
170+ //TODO: handle error
171+ _ = task ()
172+ }
173+
151174 ctx .Response .WriteHeader (ctx .status )
152175 _ , err = ctx .Response .Write (data )
153176
@@ -157,6 +180,11 @@ func (ctx *Context) send(data []byte, err error) {
157180 return
158181 }
159182
183+ for _ , task := range ctx .postSendTasks {
184+ //TOD: handle error
185+ _ = task ()
186+ }
187+
160188 ctx .End ()
161189}
162190
0 commit comments