-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook.go
57 lines (51 loc) · 1.69 KB
/
hook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package tor
const (
HookAfterInit = "AfterInit"
HookBeforeMethodGet = "BeforeMethodGet"
HookAfterMethodGet = "AfterMethodGet"
HookBeforeMethodPost = "BeforeMethodPost"
HookAfterMethodPost = "AfterMethodPost"
HookBeforeMethodHead = "BeforeMethodHead"
HookAfterMethodHead = "AfterMethodHead"
HookBeforeMethodDelete = "BeforeMethodDelete"
HookAfterMethodDelete = "AfterMethodDelete"
HookBeforeMethodPut = "BeforeMethodPut"
HookAfterMethodPut = "AfterMethodPut"
HookBeforeMethodPatch = "BeforeMethodPatch"
HookAfterMethodPatch = "AfterMethodPatch"
HookBeforeMethodOptions = "BeforeMethodOptions"
HookAfterMethodOptions = "AfterMethodOptions"
HookBeforeRender = "BeforeRender"
HookAfterRender = "AfterRender"
HookBeforeOutput = "BeforeOutput"
HookAfterOutput = "AfterOutput"
)
type torHook struct {
app *torApp
controllerHooks map[string][]HookControllerFunc
}
type HookController struct {
Context *torContext
Template *torTemplate
Session *torSession
}
type HookControllerFunc func(*HookController)
func (this *torHook) AddControllerHook(event string, hookFunc HookControllerFunc) {
if this.controllerHooks == nil {
this.controllerHooks = make(map[string][]HookControllerFunc)
}
if _, ok := this.controllerHooks[event]; !ok {
this.controllerHooks[event] = []HookControllerFunc{}
}
this.controllerHooks[event] = append(this.controllerHooks[event], hookFunc)
}
func (this *torHook) CallControllerHook(event string, hc *HookController) {
if funcList, ok := this.controllerHooks[event]; ok {
for _, hookFunc := range funcList {
hookFunc(hc)
if hc.Context.Response.Finished {
return
}
}
}
}