diff options
author | Simon Ser <contact@emersion.fr> | 2020-01-20 22:04:50 +0100 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2020-01-20 22:04:50 +0100 |
commit | b58c15d121095ef58f8fb1ac5d3a456e89baee2a (patch) | |
tree | 2cc12e88691d811b3379f437cc821ab214e1992d | |
parent | efa08163333654f3245c272333e77565b6287b14 (diff) | |
download | alps-b58c15d121095ef58f8fb1ac5d3a456e89baee2a.tar.gz alps-b58c15d121095ef58f8fb1ac5d3a456e89baee2a.zip |
Extract Lua infrastructure into a plugin
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | cmd/koushin/main.go | 1 | ||||
-rw-r--r-- | plugin.go | 3 | ||||
-rw-r--r-- | plugin_go.go | 4 | ||||
-rw-r--r-- | plugins/lua/lua.go (renamed from plugin_lua.go) | 20 | ||||
-rw-r--r-- | plugins/lua/plugin.go | 9 | ||||
-rw-r--r-- | server.go | 18 |
7 files changed, 32 insertions, 24 deletions
@@ -3,3 +3,4 @@ !/public/themes/sourcehut /plugins/* !/plugins/base +!/plugins/lua diff --git a/cmd/koushin/main.go b/cmd/koushin/main.go index d22b404..4897dc9 100644 --- a/cmd/koushin/main.go +++ b/cmd/koushin/main.go @@ -13,6 +13,7 @@ import ( "github.com/labstack/gommon/log" _ "git.sr.ht/~emersion/koushin/plugins/base" + _ "git.sr.ht/~emersion/koushin/plugins/lua" ) func main() { @@ -6,7 +6,8 @@ import ( "github.com/labstack/echo/v4" ) -const pluginDir = "plugins" +// PluginDir is the path to the plugins directory. +const PluginDir = "plugins" // Plugin extends koushin with additional functionality. type Plugin interface { diff --git a/plugin_go.go b/plugin_go.go index 9a6bb64..24c4df6 100644 --- a/plugin_go.go +++ b/plugin_go.go @@ -19,7 +19,7 @@ func (p *goPlugin) Name() string { func (p *goPlugin) LoadTemplate(t *template.Template) error { t.Funcs(p.p.templateFuncs) - paths, err := filepath.Glob(pluginDir + "/" + p.p.Name + "/public/*.html") + paths, err := filepath.Glob(PluginDir + "/" + p.p.Name + "/public/*.html") if err != nil { return err } @@ -40,7 +40,7 @@ func (p *goPlugin) SetRoutes(group *echo.Group) { }) } - group.Static("/plugins/"+p.p.Name+"/assets", pluginDir+"/"+p.p.Name+"/public/assets") + group.Static("/plugins/"+p.p.Name+"/assets", PluginDir+"/"+p.p.Name+"/public/assets") } func (p *goPlugin) Inject(ctx *Context, name string, data RenderData) error { diff --git a/plugin_lua.go b/plugins/lua/lua.go index 8291a20..bbc5c39 100644 --- a/plugin_lua.go +++ b/plugins/lua/lua.go @@ -1,4 +1,4 @@ -package koushin +package koushinlua import ( "fmt" @@ -8,6 +8,7 @@ import ( "github.com/labstack/echo/v4" "github.com/yuin/gopher-lua" "layeh.com/gopher-luar" + "git.sr.ht/~emersion/koushin" ) type luaRoute struct { @@ -68,7 +69,7 @@ func (p *luaPlugin) setRoute(l *lua.LState) int { return 0 } -func (p *luaPlugin) inject(name string, data RenderData) error { +func (p *luaPlugin) inject(name string, data koushin.RenderData) error { f, ok := p.renderCallbacks[name] if !ok { return nil @@ -86,7 +87,7 @@ func (p *luaPlugin) inject(name string, data RenderData) error { return nil } -func (p *luaPlugin) Inject(ctx *Context, name string, data RenderData) error { +func (p *luaPlugin) Inject(ctx *koushin.Context, name string, data koushin.RenderData) error { if err := p.inject("*", data); err != nil { return err } @@ -157,21 +158,24 @@ func loadLuaPlugin(filename string) (*luaPlugin, error) { return p, nil } -func loadAllLuaPlugins(log echo.Logger) ([]Plugin, error) { - filenames, err := filepath.Glob(pluginDir + "/*/main.lua") +func loadAllLuaPlugins(s *koushin.Server) ([]koushin.Plugin, error) { + log := s.Logger() + + filenames, err := filepath.Glob(koushin.PluginDir + "/*/main.lua") if err != nil { return nil, fmt.Errorf("filepath.Glob failed: %v", err) } - plugins := make([]Plugin, 0, len(filenames)) + plugins := make([]koushin.Plugin, 0, len(filenames)) for _, filename := range filenames { - log.Printf("Loading Lua plugin '%v'", filename) + log.Printf("Loading Lua plugin %q", filename) + p, err := loadLuaPlugin(filename) if err != nil { for _, p := range plugins { p.Close() } - return nil, fmt.Errorf("failed to load Lua plugin '%v': %v", filename, err) + return nil, fmt.Errorf("failed to load Lua plugin %q: %v", filename, err) } plugins = append(plugins, p) } diff --git a/plugins/lua/plugin.go b/plugins/lua/plugin.go new file mode 100644 index 0000000..dbfee6d --- /dev/null +++ b/plugins/lua/plugin.go @@ -0,0 +1,9 @@ +package koushinlua + +import ( + "git.sr.ht/~emersion/koushin" +) + +func init() { + koushin.RegisterPluginLoader(loadAllLuaPlugins) +} @@ -18,9 +18,8 @@ type Server struct { e *echo.Echo Sessions *SessionManager - mutex sync.RWMutex // used for server reload - plugins []Plugin - luaPlugins []Plugin + mutex sync.RWMutex // used for server reload + plugins []Plugin // maps protocols to URLs (protocol can be empty for auto-discovery) upstreams map[string]*url.URL @@ -188,12 +187,6 @@ func (s *Server) load() error { plugins = append(plugins, l...) } - luaPlugins, err := loadAllLuaPlugins(s.e.Logger) - if err != nil { - return fmt.Errorf("failed to load plugins: %v", err) - } - plugins = append(plugins, luaPlugins...) - renderer := newRenderer(s.e.Logger, s.defaultTheme) if err := renderer.Load(plugins); err != nil { return fmt.Errorf("failed to load templates: %v", err) @@ -204,15 +197,14 @@ func (s *Server) load() error { s.mutex.Lock() defer s.mutex.Unlock() - // Close previous Lua plugins - for _, p := range s.luaPlugins { + // Close previous plugins + for _, p := range s.plugins { if err := p.Close(); err != nil { - s.e.Logger.Printf("Failed to unload plugin '%v': %v", p.Name(), err) + s.e.Logger.Printf("Failed to unload plugin %q: %v", p.Name(), err) } } s.plugins = plugins - s.luaPlugins = luaPlugins s.e.Renderer = renderer for _, p := range plugins { |