diff --git a/README.md b/README.md index f9e8f58..74706d3 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ HTTP server at `themes//assets/*`. ## Plugins -Lua plugins are supported. They can be dropped in `plugins/*.lua`. +Lua plugins are supported. They can be dropped in `plugins//main.lua`. API: @@ -28,6 +28,8 @@ API: * `koushin.set_route(method, path, f)`: register a new HTTP route, `f` will be called with the HTTP context +Plugins can provide their own templates in `plugins//public/*.html`. + ## Contributing Send patches [on the mailing list](https://lists.sr.ht/~sircmpwn/koushin), diff --git a/plugin.go b/plugin.go index 03e2261..73f3962 100644 --- a/plugin.go +++ b/plugin.go @@ -6,9 +6,11 @@ import ( "github.com/labstack/echo/v4" ) +const pluginDir = "plugins" + type Plugin interface { Name() string - Filters() template.FuncMap + LoadTemplate(t *template.Template) error SetRoutes(group *echo.Group) Inject(name string, data interface{}) error Close() error diff --git a/plugin_lua.go b/plugin_lua.go index 372c341..9354de7 100644 --- a/plugin_lua.go +++ b/plugin_lua.go @@ -86,8 +86,20 @@ func (p *luaPlugin) Inject(name string, data interface{}) error { return nil } -func (p *luaPlugin) Filters() template.FuncMap { - return p.filters +func (p *luaPlugin) LoadTemplate(t *template.Template) error { + t.Funcs(p.filters) + + paths, err := filepath.Glob(filepath.Dir(p.filename) + "/public/*.html") + if err != nil { + return err + } + if len(paths) > 0 { + if _, err := t.ParseFiles(paths...); err != nil { + return err + } + } + + return nil } func (p *luaPlugin) SetRoutes(group *echo.Group) { @@ -136,7 +148,7 @@ func loadLuaPlugin(filename string) (*luaPlugin, error) { } func loadAllLuaPlugins(log echo.Logger) ([]Plugin, error) { - filenames, err := filepath.Glob("plugins/*.lua") + filenames, err := filepath.Glob(pluginDir + "/*/main.lua") if err != nil { return nil, fmt.Errorf("filepath.Glob failed: %v", err) } diff --git a/template.go b/template.go index 7f4d658..3bdcc9d 100644 --- a/template.go +++ b/template.go @@ -92,15 +92,18 @@ func loadTemplates(logger echo.Logger, defaultTheme string, plugins []Plugin) (* return url.PathEscape(s) }, }) - for _, p := range plugins { - base = base.Funcs(p.Filters()) - } base, err := base.ParseGlob("public/*.html") if err != nil { return nil, err } + for _, p := range plugins { + if err := p.LoadTemplate(base); err != nil { + return nil, fmt.Errorf("failed to load template for plugin '%v': %v", p.Name(), err) + } + } + themes := make(map[string]*template.Template) files, err := ioutil.ReadDir(themesDir)