2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-08-03 15:22:31 +08:00
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-08-03 15:22:31 +08:00
|
|
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
|
|
import (
|
2019-12-04 10:03:03 +08:00
|
|
|
|
"github.com/gogf/gf/debug/gdebug"
|
2019-09-19 20:25:01 +08:00
|
|
|
|
"net/http"
|
2018-08-03 15:22:31 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 绑定指定的hook回调函数, pattern参数同BindHandler,支持命名路由;hook参数的值由ghttp server设定,参数不区分大小写
|
2019-06-19 09:06:52 +08:00
|
|
|
|
func (s *Server) BindHookHandler(pattern string, hook string, handler HandlerFunc) {
|
2020-03-17 14:48:52 +08:00
|
|
|
|
s.doBindHookHandler(pattern, hook, handler, "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Server) doBindHookHandler(pattern string, hook string, handler HandlerFunc, source string) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
s.setHandler(pattern, &handlerItem{
|
2019-08-03 15:54:12 +08:00
|
|
|
|
itemType: gHANDLER_TYPE_HOOK,
|
2019-12-04 10:03:03 +08:00
|
|
|
|
itemName: gdebug.FuncPath(handler),
|
2019-08-03 15:54:12 +08:00
|
|
|
|
itemFunc: handler,
|
2019-08-03 17:14:54 +08:00
|
|
|
|
hookName: hook,
|
2020-03-17 14:48:52 +08:00
|
|
|
|
source: source,
|
2019-08-03 18:08:10 +08:00
|
|
|
|
})
|
2018-08-03 15:22:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 通过map批量绑定回调函数
|
2019-09-29 14:27:09 +08:00
|
|
|
|
func (s *Server) BindHookHandlerByMap(pattern string, hookMap map[string]HandlerFunc) {
|
|
|
|
|
for k, v := range hookMap {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
s.BindHookHandler(pattern, k, v)
|
|
|
|
|
}
|
2018-08-03 15:22:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 事件回调处理,内部使用了缓存处理.
|
|
|
|
|
// 并按照指定hook回调函数的优先级及注册顺序进行调用
|
|
|
|
|
func (s *Server) callHookHandler(hook string, r *Request) {
|
2019-08-03 17:14:54 +08:00
|
|
|
|
hookItems := r.getHookHandlers(hook)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
if len(hookItems) > 0 {
|
|
|
|
|
// 备份原有的router变量
|
2019-12-03 17:16:52 +08:00
|
|
|
|
oldRouterMap := r.routerMap
|
2019-06-19 09:06:52 +08:00
|
|
|
|
for _, item := range hookItems {
|
2019-12-03 17:16:52 +08:00
|
|
|
|
r.routerMap = item.values
|
2019-06-19 09:06:52 +08:00
|
|
|
|
// 不使用hook的router对象,保留路由注册服务的router对象,不能覆盖
|
|
|
|
|
// r.Router = item.handler.router
|
2019-08-03 15:54:12 +08:00
|
|
|
|
if err := s.niceCallHookHandler(item.handler.itemFunc, r); err != nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
switch err {
|
|
|
|
|
case gEXCEPTION_EXIT:
|
|
|
|
|
break
|
|
|
|
|
case gEXCEPTION_EXIT_ALL:
|
|
|
|
|
fallthrough
|
|
|
|
|
case gEXCEPTION_EXIT_HOOK:
|
|
|
|
|
return
|
|
|
|
|
default:
|
2019-09-19 20:25:01 +08:00
|
|
|
|
r.Response.WriteStatus(http.StatusInternalServerError, err)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 恢复原有的router变量
|
2019-12-03 17:16:52 +08:00
|
|
|
|
r.routerMap = oldRouterMap
|
2019-06-19 09:06:52 +08:00
|
|
|
|
}
|
2018-08-03 15:22:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 22:31:45 +08:00
|
|
|
|
// 获得当前请求,指定类型的的钩子函数列表
|
|
|
|
|
func (r *Request) getHookHandlers(hook string) []*handlerParsedItem {
|
|
|
|
|
if !r.hasHookHandler {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
parsedItems := make([]*handlerParsedItem, 0, 4)
|
|
|
|
|
for _, v := range r.handlers {
|
|
|
|
|
if v.handler.hookName != hook {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
item := v
|
|
|
|
|
parsedItems = append(parsedItems, item)
|
|
|
|
|
}
|
|
|
|
|
return parsedItems
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-13 00:43:36 +08:00
|
|
|
|
// 友好地调用方法
|
|
|
|
|
func (s *Server) niceCallHookHandler(f HandlerFunc, r *Request) (err interface{}) {
|
2019-06-19 09:06:52 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
err = recover()
|
|
|
|
|
}()
|
|
|
|
|
f(r)
|
|
|
|
|
return
|
2019-01-13 00:43:36 +08:00
|
|
|
|
}
|