改进ghttp事件回调名称

This commit is contained in:
John
2018-04-09 18:51:46 +08:00
parent 88cde11fb5
commit c4b9144d45
3 changed files with 16 additions and 19 deletions

View File

@ -12,14 +12,14 @@ func (s *Server) startCloseQueueLoop() {
for {
if v := s.closeQueue.PopFront(); v != nil {
r := v.(*Request)
s.callHookHandler(r, "BeforeRequestClose")
s.callHookHandler(r, "BeforeClose")
// 关闭当前会话的Cookie
r.Cookie.Close()
// 更新Session会话超时时间
r.Session.UpdateExpire()
s.callHookHandler(r, "AfterRequestClose")
s.callHookHandler(r, "AfterClose")
}
}
}()

View File

@ -78,22 +78,21 @@ func (s *Server)callHandler(h *HandlerItem, r *Request) {
s.callHookHandler(r, "AfterServe")
// 路由规则打包
s.callHookHandler(r, "BeforeRouterPatch")
s.callHookHandler(r, "BeforePatch")
if buffer, err := s.Router.Patch(r.Response.Buffer()); err == nil {
r.Response.ClearBuffer()
r.Response.Write(buffer)
}
s.callHookHandler(r, "AfterRouterPatch")
s.callHookHandler(r, "AfterPatch")
s.callHookHandler(r, "BeforeOutput")
// 输出Cookie
s.callHookHandler(r, "BeforeCookieOutput")
r.Cookie.Output()
s.callHookHandler(r, "AfterCookieOutput")
// 输出缓冲区
s.callHookHandler(r, "BeforeBufferOutput")
r.Response.OutputBuffer()
s.callHookHandler(r, "AfterBufferOutput")
s.callHookHandler(r, "AfterOutput")
// 将Request对象指针丢到队列中异步处理
s.closeQueue.PushBack(r)

View File

@ -8,16 +8,14 @@ import (
func main() {
pattern := "/"
ghttp.GetServer().BindHookHandlerByMap(pattern, map[string]ghttp.HandlerFunc{
"BeforeServe" : func(r *ghttp.Request){ fmt.Println("BeforeServe") },
"AfterServe" : func(r *ghttp.Request){ fmt.Println("AfterServe") },
"BeforeRouterPatch" : func(r *ghttp.Request){ fmt.Println("BeforeRouterPatch") },
"AfterRouterPatch" : func(r *ghttp.Request){ fmt.Println("AfterRouterPatch") },
"BeforeCookieOutput" : func(r *ghttp.Request){ fmt.Println("BeforeCookieOutput") },
"AfterCookieOutput" : func(r *ghttp.Request){ fmt.Println("AfterCookieOutput") },
"BeforeBufferOutput" : func(r *ghttp.Request){ fmt.Println("BeforeBufferOutput") },
"AfterBufferOutput" : func(r *ghttp.Request){ fmt.Println("AfterBufferOutput") },
"BeforeRequestClose" : func(r *ghttp.Request){ fmt.Println("BeforeRequestClose") },
"AfterRequestClose" : func(r *ghttp.Request){ fmt.Println("AfterRequestClose") },
"BeforeServe" : func(r *ghttp.Request){ fmt.Println("BeforeServe") },
"AfterServe" : func(r *ghttp.Request){ fmt.Println("AfterServe") },
"BeforePatch" : func(r *ghttp.Request){ fmt.Println("BeforePatch") },
"AfterPatch" : func(r *ghttp.Request){ fmt.Println("AfterPatch") },
"BeforeOutput" : func(r *ghttp.Request){ fmt.Println("BeforeOutput") },
"AfterOutput" : func(r *ghttp.Request){ fmt.Println("AfterOutput") },
"BeforeClose" : func(r *ghttp.Request){ fmt.Println("BeforeClose") },
"AfterClose" : func(r *ghttp.Request){ fmt.Println("AfterClose") },
})
ghttp.GetServer().BindHandler(pattern, func(r *ghttp.Request) {
r.Response.WriteString("Hello World!")