2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). 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 (
|
2021-09-27 21:27:24 +08:00
|
|
|
"context"
|
2019-09-19 20:25:01 +08:00
|
|
|
"net/http"
|
2021-07-13 23:01:31 +08:00
|
|
|
"reflect"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/debug/gdebug"
|
2018-08-03 15:22:31 +08:00
|
|
|
)
|
|
|
|
|
2020-05-07 23:05:33 +08:00
|
|
|
// BindHookHandler registers handler for specified hook.
|
2019-06-19 09:06:52 +08:00
|
|
|
func (s *Server) BindHookHandler(pattern string, hook string, handler HandlerFunc) {
|
2021-11-07 21:31:33 +08:00
|
|
|
s.doBindHookHandler(context.TODO(), doBindHookHandlerInput{
|
|
|
|
Prefix: "",
|
|
|
|
Pattern: pattern,
|
|
|
|
HookName: hook,
|
|
|
|
Handler: handler,
|
|
|
|
Source: "",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type doBindHookHandlerInput struct {
|
|
|
|
Prefix string
|
|
|
|
Pattern string
|
|
|
|
HookName string
|
|
|
|
Handler HandlerFunc
|
|
|
|
Source string
|
2020-03-17 14:48:52 +08:00
|
|
|
}
|
|
|
|
|
2021-11-07 21:31:33 +08:00
|
|
|
func (s *Server) doBindHookHandler(ctx context.Context, in doBindHookHandlerInput) {
|
|
|
|
s.setHandler(
|
|
|
|
ctx,
|
|
|
|
setHandlerInput{
|
|
|
|
Prefix: in.Prefix,
|
|
|
|
Pattern: in.Pattern,
|
|
|
|
HandlerItem: &handlerItem{
|
|
|
|
Type: HandlerTypeHook,
|
|
|
|
Name: gdebug.FuncPath(in.Handler),
|
|
|
|
Info: handlerFuncInfo{
|
|
|
|
Func: in.Handler,
|
|
|
|
Type: reflect.TypeOf(in.Handler),
|
|
|
|
},
|
|
|
|
HookName: in.HookName,
|
|
|
|
Source: in.Source,
|
|
|
|
},
|
2021-07-13 23:01:31 +08:00
|
|
|
},
|
2021-11-07 21:31:33 +08:00
|
|
|
)
|
2018-08-03 15:22:31 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-05-07 23:05:33 +08:00
|
|
|
// callHookHandler calls the hook handler by their registered sequences.
|
2018-08-03 15:22:31 +08:00
|
|
|
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 {
|
2020-05-07 23:05:33 +08:00
|
|
|
// Backup the old router variable map.
|
2019-12-03 17:16:52 +08:00
|
|
|
oldRouterMap := r.routerMap
|
2019-06-19 09:06:52 +08:00
|
|
|
for _, item := range hookItems {
|
2021-07-19 20:06:44 +08:00
|
|
|
r.routerMap = item.Values
|
2020-05-07 23:05:33 +08:00
|
|
|
// DO NOT USE the router of the hook handler,
|
|
|
|
// which can overwrite the router of serving handler.
|
2019-06-19 09:06:52 +08:00
|
|
|
// r.Router = item.handler.router
|
2021-07-19 20:06:44 +08:00
|
|
|
if err := s.niceCallHookHandler(item.Handler.Info.Func, r); err != nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
switch err {
|
2020-12-14 13:26:48 +08:00
|
|
|
case exceptionExit:
|
2019-06-19 09:06:52 +08:00
|
|
|
break
|
2020-12-14 13:26:48 +08:00
|
|
|
case exceptionExitAll:
|
2019-06-19 09:06:52 +08:00
|
|
|
fallthrough
|
2020-12-14 13:26:48 +08:00
|
|
|
case exceptionExitHook:
|
2019-06-19 09:06:52 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 23:05:33 +08:00
|
|
|
// Restore the old router variable map.
|
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-05-07 23:05:33 +08:00
|
|
|
// getHookHandlers retrieves and returns the hook handlers of specified hook.
|
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 {
|
2021-07-19 20:06:44 +08:00
|
|
|
if v.Handler.HookName != hook {
|
2020-04-06 22:31:45 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
item := v
|
|
|
|
parsedItems = append(parsedItems, item)
|
|
|
|
}
|
|
|
|
return parsedItems
|
|
|
|
}
|
|
|
|
|
2020-05-07 23:05:33 +08:00
|
|
|
// niceCallHookHandler nicely calls the hook handler function,
|
|
|
|
// which means it automatically catches and returns the possible panic error to
|
|
|
|
// avoid goroutine crash.
|
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
|
|
|
}
|