Browse Source

Introduce PluginLoaderFunc

This allows registered plugins to execute code when loaded. This will
also allow the Lua support code to be a plugin.

Closes: https://todo.sr.ht/~sircmpwn/koushin/54
master
Simon Ser 4 years ago
parent
commit
01983eb7b5
No known key found for this signature in database GPG Key ID: FDE7BE0E88F5E48
4 changed files with 26 additions and 8 deletions
  1. +8
    -4
      plugin.go
  2. +7
    -0
      plugin_go.go
  3. +1
    -1
      plugins/base/plugin.go
  4. +10
    -3
      server.go

+ 8
- 4
plugin.go View File

@@ -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)
}

+ 7
- 0
plugin_go.go View File

@@ -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
}
}

+ 1
- 1
plugins/base/plugin.go View File

@@ -12,5 +12,5 @@ func init() {
p.TemplateFuncs(templateFuncs)
registerRoutes(&p)

koushin.RegisterPlugin(p.Plugin())
koushin.RegisterPluginLoader(p.Loader())
}

+ 10
- 3
server.go View File

@@ -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)


Loading…
Cancel
Save