2019-12-16 11:26:26 +00:00
|
|
|
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)
|
|
|
|
|
2020-01-20 21:04:50 +00:00
|
|
|
paths, err := filepath.Glob(PluginDir + "/" + p.p.Name + "/public/*.html")
|
2019-12-16 11:26:26 +00:00
|
|
|
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 {
|
2019-12-17 14:19:37 +00:00
|
|
|
h := r.Handler
|
|
|
|
group.Add(r.Method, r.Path, func(ectx echo.Context) error {
|
|
|
|
return h(ectx.(*Context))
|
|
|
|
})
|
2019-12-16 11:26:26 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 21:04:50 +00:00
|
|
|
group.Static("/plugins/"+p.p.Name+"/assets", PluginDir+"/"+p.p.Name+"/public/assets")
|
2019-12-16 11:26:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:14:15 +00:00
|
|
|
func (p *goPlugin) Inject(ctx *Context, name string, data RenderData) error {
|
2019-12-17 12:23:10 +00:00
|
|
|
if f, ok := p.p.injectFuncs["*"]; ok {
|
2019-12-17 14:14:15 +00:00
|
|
|
if err := f(ctx, data); err != nil {
|
2019-12-17 12:23:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if f, ok := p.p.injectFuncs[name]; ok {
|
2019-12-17 14:14:15 +00:00
|
|
|
return f(ctx, data)
|
2019-12-17 12:23:10 +00:00
|
|
|
}
|
2019-12-16 11:26:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *goPlugin) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type goPluginRoute struct {
|
|
|
|
Method string
|
|
|
|
Path string
|
2019-12-17 14:19:37 +00:00
|
|
|
Handler HandlerFunc
|
2019-12-16 11:26:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 09:58:31 +00:00
|
|
|
// GoPlugin is a helper to create Go plugins.
|
|
|
|
//
|
2020-01-21 12:40:55 +00:00
|
|
|
// Use this struct to define your plugin, then call RegisterPluginLoader:
|
2019-12-17 09:58:31 +00:00
|
|
|
//
|
|
|
|
// p := GoPlugin{Name: "my-plugin"}
|
|
|
|
// // Define routes, template functions, etc
|
2020-01-21 12:40:55 +00:00
|
|
|
// koushin.RegisterPluginLoader(p.Loader())
|
2019-12-16 11:26:26 +00:00
|
|
|
type GoPlugin struct {
|
|
|
|
Name string
|
|
|
|
|
|
|
|
routes []goPluginRoute
|
|
|
|
|
|
|
|
templateFuncs template.FuncMap
|
2019-12-17 14:19:37 +00:00
|
|
|
injectFuncs map[string]InjectFunc
|
2019-12-16 11:26:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 14:19:37 +00:00
|
|
|
// HandlerFunc is a function serving HTTP requests.
|
|
|
|
type HandlerFunc func(*Context) error
|
|
|
|
|
2019-12-17 09:58:31 +00:00
|
|
|
// AddRoute registers a new HTTP route.
|
2019-12-17 14:19:37 +00:00
|
|
|
func (p *GoPlugin) AddRoute(method, path string, handler HandlerFunc) {
|
2019-12-16 11:26:26 +00:00
|
|
|
p.routes = append(p.routes, goPluginRoute{method, path, handler})
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:19:37 +00:00
|
|
|
func (p *GoPlugin) DELETE(path string, handler HandlerFunc) {
|
2019-12-16 11:26:26 +00:00
|
|
|
p.AddRoute(http.MethodDelete, path, handler)
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:19:37 +00:00
|
|
|
func (p *GoPlugin) GET(path string, handler HandlerFunc) {
|
2019-12-16 11:26:26 +00:00
|
|
|
p.AddRoute(http.MethodGet, path, handler)
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:19:37 +00:00
|
|
|
func (p *GoPlugin) POST(path string, handler HandlerFunc) {
|
2019-12-16 11:26:26 +00:00
|
|
|
p.AddRoute(http.MethodPost, path, handler)
|
|
|
|
}
|
|
|
|
|
2019-12-17 14:19:37 +00:00
|
|
|
func (p *GoPlugin) PUT(path string, handler HandlerFunc) {
|
2019-12-16 11:26:26 +00:00
|
|
|
p.AddRoute(http.MethodPut, path, handler)
|
|
|
|
}
|
|
|
|
|
2019-12-17 09:58:31 +00:00
|
|
|
// TemplateFuncs registers new template functions.
|
2019-12-16 11:26:26 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-17 12:23:10 +00:00
|
|
|
// InjectFunc is a function that injects data prior to rendering a template.
|
2019-12-17 14:14:15 +00:00
|
|
|
type InjectFunc func(ctx *Context, data RenderData) error
|
2019-12-17 12:23:10 +00:00
|
|
|
|
|
|
|
// Inject registers a function to execute prior to rendering a template. The
|
|
|
|
// special name "*" matches any template.
|
|
|
|
func (p *GoPlugin) Inject(name string, f InjectFunc) {
|
|
|
|
if p.injectFuncs == nil {
|
|
|
|
p.injectFuncs = make(map[string]InjectFunc)
|
|
|
|
}
|
|
|
|
p.injectFuncs[name] = f
|
|
|
|
}
|
|
|
|
|
2019-12-17 09:58:31 +00:00
|
|
|
// Plugin returns an object implementing Plugin.
|
2019-12-16 11:26:26 +00:00
|
|
|
func (p *GoPlugin) Plugin() Plugin {
|
|
|
|
return &goPlugin{p}
|
|
|
|
}
|
2020-01-20 20:37:28 +00:00
|
|
|
|
|
|
|
// Loader returns a loader function for this plugin.
|
|
|
|
func (p *GoPlugin) Loader() PluginLoaderFunc {
|
|
|
|
return func(*Server) ([]Plugin, error) {
|
|
|
|
return []Plugin{p.Plugin()}, nil
|
|
|
|
}
|
|
|
|
}
|