forked from revel/revel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
71 lines (59 loc) · 1.99 KB
/
plugin.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package revel
// An plugin that allows the user to inject behavior at various points in the request cycle.
type Plugin interface {
// Called on server startup (and on each code reload).
OnAppStart()
// Called after the router has finished configuration.
OnRoutesLoaded(router *Router)
// Called before every request.
BeforeRequest(c *Controller)
// Called after every non-panicking request, before the Result has been applied.
AfterRequest(c *Controller)
// Called when a panic exits an action, with the recovered error value.
OnException(c *Controller, err interface{})
// Called after every request (panic or not), after the Result has been applied.
Finally(c *Controller)
}
// It provides default (empty) implementations for all the required methods.
type EmptyPlugin struct{}
func (p EmptyPlugin) OnAppStart() {}
func (p EmptyPlugin) OnRoutesLoaded(router *Router) {}
func (p EmptyPlugin) BeforeRequest(c *Controller) {}
func (p EmptyPlugin) AfterRequest(c *Controller) {}
func (p EmptyPlugin) OnException(c *Controller, err interface{}) {}
func (p EmptyPlugin) Finally(c *Controller) {}
type PluginCollection []Plugin
var plugins PluginCollection
func RegisterPlugin(p Plugin) {
plugins = append(plugins, p)
}
func (plugins PluginCollection) OnAppStart() {
for _, p := range plugins {
p.OnAppStart()
}
}
func (plugins PluginCollection) OnRoutesLoaded(router *Router) {
for _, p := range plugins {
p.OnRoutesLoaded(router)
}
}
func (plugins PluginCollection) BeforeRequest(c *Controller) {
for _, p := range plugins {
p.BeforeRequest(c)
}
}
func (plugins PluginCollection) AfterRequest(c *Controller) {
for _, p := range plugins {
p.AfterRequest(c)
}
}
func (plugins PluginCollection) OnException(c *Controller, err interface{}) {
for _, p := range plugins {
p.OnException(c, err)
}
}
func (plugins PluginCollection) Finally(c *Controller) {
for _, p := range plugins {
p.Finally(c)
}
}