diff options
-rw-r--r-- | plugin.go | 12 | ||||
-rw-r--r-- | plugin_go.go | 7 | ||||
-rw-r--r-- | plugins/base/plugin.go | 2 | ||||
-rw-r--r-- | server.go | 13 |
4 files changed, 26 insertions, 8 deletions
@@ -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()) } @@ -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) |