forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
184 lines (160 loc) · 6.39 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) 2016, Gerasimos Maropoulos
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
// or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL JULIEN SCHMIDT BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package iris
import (
"fmt"
)
// IPlugin is the interface which all Plugins must implement.
//
// A Plugin can register other plugins also from it's Activate state
type IPlugin interface {
// GetName has to returns the name of the plugin, a name is unique
// name has to be not dependent from other methods of the plugin,
// because it is being called even before the Activate
GetName() string
// GetDescription has to returns the description of what the plugins is used for
GetDescription() string
// Activate called BEFORE the plugin being added to the plugins list,
// if Activate returns none nil error then the plugin is not being added to the list
// it is being called only one time
//
// PluginContainer parameter used to add other plugins if that's necessary by the plugin
Activate(IPluginContainer) error
// PreHandle it's being called every time BEFORE a Route is registed to the Router
//
// first parameter is the HTTP method
// second is the Route
PreHandle(string, IRoute)
// PostHandle it's being called every time AFTER a Route successfully registed to the Router
//
// first parameter is the HTTP method
// second is the Route
PostHandle(string, IRoute)
// PreListen it's being called only one time, BEFORE the Server is started (if .Listen called)
// is used to do work at the time all other things are ready to go
PreListen(*Station)
// PostListen it's being called only one time, AFTER the Server is started (if .Listen called)
// is used to do work when the server is running
PostListen(*Station, error)
// PreClose it's being called only one time, BEFORE the Iris .Close method
// any plugin cleanup/clear memory happens here
//
// The plugin is deactivated after this state
PreClose(*Station)
}
type IPluginContainer interface {
Plugin(plugin IPlugin) error
RemovePlugin(pluginName string)
GetByName(pluginName string) IPlugin
Printf(format string, a ...interface{})
DoPreHandle(method string, route IRoute)
DoPostHandle(method string, route IRoute)
DoPreListen(station *Station)
DoPostListen(station *Station, err error)
DoPreClose(station *Station)
}
// PluginContainer is the base container of all Iris, registed plugins
type PluginContainer struct {
activatedPlugins []IPlugin
}
// Plugin activates the plugins and if succeed then adds it to the activated plugins list
func (p *PluginContainer) Plugin(plugin IPlugin) error {
if p.activatedPlugins == nil {
p.activatedPlugins = make([]IPlugin, 0)
}
// Check if the plugin already exists
if p.GetByName(plugin.GetName()) != nil {
return fmt.Errorf("[Iris] Error on Plugin: %s is already exists: %s", plugin.GetName(), plugin.GetDescription())
}
// Activate the plugin, if no error then add it to the plugins
err := plugin.Activate(p)
if err != nil {
return err
}
// All ok, add it to the plugins list
p.activatedPlugins = append(p.activatedPlugins, plugin)
return nil
}
// RemovePlugin DOES NOT calls the plugin.PreClose method but it removes it completely from the plugins list
func (p *PluginContainer) RemovePlugin(pluginName string) {
if p.activatedPlugins == nil {
return
}
indexToRemove := -1
for i := 0; i < len(p.activatedPlugins); i++ {
if p.activatedPlugins[i].GetName() == pluginName {
indexToRemove = i
}
}
if indexToRemove != -1 {
p.activatedPlugins = append(p.activatedPlugins[:indexToRemove], p.activatedPlugins[indexToRemove+1:]...)
}
}
// GetByName returns a plugin instance by it's name
func (p *PluginContainer) GetByName(pluginName string) IPlugin {
if p.activatedPlugins == nil {
return nil
}
for i := 0; i < len(p.activatedPlugins); i++ {
if p.activatedPlugins[i].GetName() == pluginName {
return p.activatedPlugins[i]
}
}
return nil
}
// Printf sends plain text to any registed logger (future), some plugins maybe want use this method
// maybe at the future I change it, instead of sync even-driven to async channels...
func (p *PluginContainer) Printf(format string, a ...interface{}) {
fmt.Printf(format, a...) //for now just this.
}
// These methods are the callers for all plugins' events
func (p *PluginContainer) DoPreHandle(method string, route IRoute) {
for i := 0; i < len(p.activatedPlugins); i++ {
p.activatedPlugins[i].PreHandle(method, route)
}
}
func (p *PluginContainer) DoPostHandle(method string, route IRoute) {
for i := 0; i < len(p.activatedPlugins); i++ {
p.activatedPlugins[i].PostHandle(method, route)
}
}
func (p *PluginContainer) DoPreListen(station *Station) {
for i := 0; i < len(p.activatedPlugins); i++ {
p.activatedPlugins[i].PreListen(station)
}
}
func (p *PluginContainer) DoPostListen(station *Station, err error) {
for i := 0; i < len(p.activatedPlugins); i++ {
p.activatedPlugins[i].PostListen(station, err)
}
}
func (p *PluginContainer) DoPreClose(station *Station) { //tood IStation
for i := 0; i < len(p.activatedPlugins); i++ {
p.activatedPlugins[i].PreClose(station)
}
}
var _ IPluginContainer = &PluginContainer{}