2022-08-09 15:25:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go.goblog.app/app/pkgs/plugins"
|
|
|
|
"go.goblog.app/app/pkgs/plugintypes"
|
|
|
|
"go.goblog.app/app/pkgs/yaegiwrappers"
|
|
|
|
)
|
|
|
|
|
2022-08-12 10:48:16 +00:00
|
|
|
const (
|
|
|
|
execPlugin = "exec"
|
|
|
|
middlewarePlugin = "middleware"
|
|
|
|
uiPlugin = "ui"
|
|
|
|
)
|
|
|
|
|
2022-08-09 15:25:22 +00:00
|
|
|
func (a *goBlog) initPlugins() error {
|
|
|
|
a.pluginHost = plugins.NewPluginHost(yaegiwrappers.Symbols)
|
|
|
|
|
2022-08-12 10:48:16 +00:00
|
|
|
a.pluginHost.AddPluginType(execPlugin, (*plugintypes.Exec)(nil))
|
|
|
|
a.pluginHost.AddPluginType(middlewarePlugin, (*plugintypes.Middleware)(nil))
|
|
|
|
a.pluginHost.AddPluginType(uiPlugin, (*plugintypes.UI)(nil))
|
2022-08-09 15:25:22 +00:00
|
|
|
|
|
|
|
for _, pc := range a.cfg.Plugins {
|
|
|
|
if pluginInterface, err := a.pluginHost.LoadPlugin(&plugins.PluginConfig{
|
|
|
|
Path: pc.Path,
|
|
|
|
ImportPath: pc.Import,
|
|
|
|
PluginType: pc.Type,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
} else if pluginInterface != nil {
|
|
|
|
if setAppPlugin, ok := pluginInterface.(plugintypes.SetApp); ok {
|
|
|
|
setAppPlugin.SetApp(a)
|
|
|
|
}
|
|
|
|
if setConfigPlugin, ok := pluginInterface.(plugintypes.SetConfig); ok {
|
|
|
|
setConfigPlugin.SetConfig(pc.Config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 10:48:16 +00:00
|
|
|
execs := getPluginsForType[plugintypes.Exec](a, execPlugin)
|
2022-08-09 15:25:22 +00:00
|
|
|
for _, p := range execs {
|
|
|
|
go p.Exec()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPluginsForType[T any](a *goBlog, pluginType string) (list []T) {
|
2022-08-12 10:48:16 +00:00
|
|
|
if a == nil || a.pluginHost == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-09 15:25:22 +00:00
|
|
|
return plugins.GetPluginsForType[T](a.pluginHost, pluginType)
|
|
|
|
}
|
|
|
|
|
2022-08-12 10:48:16 +00:00
|
|
|
// Implement all needed interfaces
|
2022-08-09 15:25:22 +00:00
|
|
|
|
|
|
|
func (a *goBlog) GetDatabase() plugintypes.Database {
|
|
|
|
return a.db
|
|
|
|
}
|
|
|
|
|
2022-08-12 10:48:16 +00:00
|
|
|
func (p *post) GetParameters() map[string][]string {
|
|
|
|
return p.Parameters
|
|
|
|
}
|
|
|
|
|
|
|
|
type pluginPostRenderData struct {
|
|
|
|
p *post
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *pluginPostRenderData) GetPost() plugintypes.Post {
|
|
|
|
return d.p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *post) pluginRenderData() plugintypes.PostRenderData {
|
|
|
|
return &pluginPostRenderData{p: p}
|
|
|
|
}
|
2022-10-31 09:52:35 +00:00
|
|
|
|
|
|
|
func (b *configBlog) GetBlog() string {
|
|
|
|
return b.name
|
|
|
|
}
|
|
|
|
|
|
|
|
type pluginBlogRenderData struct {
|
|
|
|
b *configBlog
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *pluginBlogRenderData) GetBlog() plugintypes.Blog {
|
|
|
|
return d.b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *configBlog) pluginRenderData() plugintypes.BlogRenderData {
|
|
|
|
return &pluginBlogRenderData{b: b}
|
|
|
|
}
|