Add an API for Go plugins
References: https://todo.sr.ht/~sircmpwn/koushin/39
This commit is contained in:
parent
90080cc8b6
commit
e83844fbad
3 changed files with 111 additions and 1 deletions
|
@ -22,3 +22,10 @@ type Plugin interface {
|
|||
// Close is called when the plugin is unloaded.
|
||||
Close() error
|
||||
}
|
||||
|
||||
var plugins []Plugin
|
||||
|
||||
// RegisterPlugin registers a plugin to be loaded on server startup.
|
||||
func RegisterPlugin(p Plugin) {
|
||||
plugins = append(plugins, p)
|
||||
}
|
||||
|
|
97
plugin_go.go
Normal file
97
plugin_go.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package koushin
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type goPlugin struct {
|
||||
p *GoPlugin
|
||||
}
|
||||
|
||||
func (p *goPlugin) Name() string {
|
||||
return p.p.Name
|
||||
}
|
||||
|
||||
func (p *goPlugin) LoadTemplate(t *template.Template) error {
|
||||
t.Funcs(p.p.templateFuncs)
|
||||
|
||||
paths, err := filepath.Glob(pluginDir + "/" + p.p.Name + "/public/*.html")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(paths) > 0 {
|
||||
if _, err := t.ParseFiles(paths...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *goPlugin) SetRoutes(group *echo.Group) {
|
||||
for _, r := range p.p.routes {
|
||||
group.Add(r.Method, r.Path, r.Handler)
|
||||
}
|
||||
|
||||
group.Static("/assets", pluginDir + "/" + p.p.Name + "/public/assets")
|
||||
}
|
||||
|
||||
func (p *goPlugin) Inject(name string, data interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *goPlugin) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type goPluginRoute struct {
|
||||
Method string
|
||||
Path string
|
||||
Handler echo.HandlerFunc
|
||||
}
|
||||
|
||||
type GoPlugin struct {
|
||||
Name string
|
||||
|
||||
routes []goPluginRoute
|
||||
|
||||
templateFuncs template.FuncMap
|
||||
}
|
||||
|
||||
func (p *GoPlugin) AddRoute(method, path string, handler echo.HandlerFunc) {
|
||||
p.routes = append(p.routes, goPluginRoute{method, path, handler})
|
||||
}
|
||||
|
||||
func (p *GoPlugin) DELETE(path string, handler echo.HandlerFunc) {
|
||||
p.AddRoute(http.MethodDelete, path, handler)
|
||||
}
|
||||
|
||||
func (p *GoPlugin) GET(path string, handler echo.HandlerFunc) {
|
||||
p.AddRoute(http.MethodGet, path, handler)
|
||||
}
|
||||
|
||||
func (p *GoPlugin) POST(path string, handler echo.HandlerFunc) {
|
||||
p.AddRoute(http.MethodPost, path, handler)
|
||||
}
|
||||
|
||||
func (p *GoPlugin) PUT(path string, handler echo.HandlerFunc) {
|
||||
p.AddRoute(http.MethodPut, path, handler)
|
||||
}
|
||||
|
||||
func (p *GoPlugin) TemplateFuncs(funcs template.FuncMap) {
|
||||
if p.templateFuncs == nil {
|
||||
p.templateFuncs = make(template.FuncMap, len(funcs))
|
||||
}
|
||||
|
||||
for k, f := range funcs {
|
||||
p.templateFuncs[k] = f
|
||||
}
|
||||
}
|
||||
|
||||
func (p *GoPlugin) Plugin() Plugin {
|
||||
return &goPlugin{p}
|
||||
}
|
|
@ -137,10 +137,16 @@ func New(e *echo.Echo, options *Options) error {
|
|||
return err
|
||||
}
|
||||
|
||||
s.Plugins, err = loadAllLuaPlugins(e.Logger)
|
||||
s.Plugins = append([]Plugin(nil), plugins...)
|
||||
for _, p := range s.Plugins {
|
||||
e.Logger.Printf("Registered plugin '%v'", p.Name())
|
||||
}
|
||||
|
||||
luaPlugins, err := loadAllLuaPlugins(e.Logger)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load plugins: %v", err)
|
||||
}
|
||||
s.Plugins = append(s.Plugins, luaPlugins...)
|
||||
|
||||
e.Renderer, err = loadTemplates(e.Logger, options.Theme, s.Plugins)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in a new issue