forked from revel/revel
/
plugin.go
49 lines (39 loc) · 1.2 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
package rev
// An plugin that allows the user to inject behavior at various points in the request cycle.
type Plugin interface {
OnAppStart()
BeforeRequest(c *Controller)
AfterRequest(c *Controller)
OnException(c *Controller, err interface{})
}
// It provides default (empty) implementations for all the required methods.
type EmptyPlugin struct{}
func (p EmptyPlugin) OnAppStart() {}
func (p EmptyPlugin) BeforeRequest(c *Controller) {}
func (p EmptyPlugin) AfterRequest(c *Controller) {}
func (p EmptyPlugin) OnException(c *Controller, err interface{}) {}
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) 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)
}
}