aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--plugin.go12
-rw-r--r--plugin_go.go7
-rw-r--r--plugins/base/plugin.go2
-rw-r--r--server.go13
4 files changed, 26 insertions, 8 deletions
diff --git a/plugin.go b/plugin.go
index 77000f2..4e46aaf 100644
--- a/plugin.go
+++ b/plugin.go
@@ -23,9 +23,13 @@ type Plugin interface {
Close() error
}
-var plugins []Plugin
+// PluginLoaderFunc loads plugins for the provided server.
+type PluginLoaderFunc func(*Server) ([]Plugin, error)
-// RegisterPlugin registers a plugin to be loaded on server startup.
-func RegisterPlugin(p Plugin) {
- plugins = append(plugins, p)
+var pluginLoaders []PluginLoaderFunc
+
+// RegisterPluginLoader registers a plugin loader. The loader will be called on
+// server start-up and reload.
+func RegisterPluginLoader(f PluginLoaderFunc) {
+ pluginLoaders = append(pluginLoaders, f)
}
diff --git a/plugin_go.go b/plugin_go.go
index 34abeeb..9a6bb64 100644
--- a/plugin_go.go
+++ b/plugin_go.go
@@ -132,3 +132,10 @@ func (p *GoPlugin) Inject(name string, f InjectFunc) {
func (p *GoPlugin) Plugin() Plugin {
return &goPlugin{p}
}
+
+// Loader returns a loader function for this plugin.
+func (p *GoPlugin) Loader() PluginLoaderFunc {
+ return func(*Server) ([]Plugin, error) {
+ return []Plugin{p.Plugin()}, nil
+ }
+}
diff --git a/plugins/base/plugin.go b/plugins/base/plugin.go
index 5a946f5..16eaf1d 100644
--- a/plugins/base/plugin.go
+++ b/plugins/base/plugin.go
@@ -12,5 +12,5 @@ func init() {
p.TemplateFuncs(templateFuncs)
registerRoutes(&p)
- koushin.RegisterPlugin(p.Plugin())
+ koushin.RegisterPluginLoader(p.Loader())
}
diff --git a/server.go b/server.go
index a59c0e4..e032d14 100644
--- a/server.go
+++ b/server.go
@@ -176,9 +176,16 @@ func (s *Server) parseSMTPUpstream() error {
}
func (s *Server) load() error {
- plugins := append([]Plugin(nil), plugins...)
- for _, p := range plugins {
- s.e.Logger.Printf("Registered plugin '%v'", p.Name())
+ var plugins []Plugin
+ for _, load := range pluginLoaders {
+ l, err := load(s)
+ if err != nil {
+ return fmt.Errorf("failed to load plugins: %v", err)
+ }
+ for _, p := range l {
+ s.e.Logger.Printf("Loaded plugin %q", p.Name())
+ }
+ plugins = append(plugins, l...)
}
luaPlugins, err := loadAllLuaPlugins(s.e.Logger)