2019-12-09 15:02:12 +00:00
|
|
|
package koushin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-09 16:54:24 +00:00
|
|
|
"html/template"
|
2019-12-09 15:02:12 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
"layeh.com/gopher-luar"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Plugin interface {
|
|
|
|
Name() string
|
2019-12-09 16:54:24 +00:00
|
|
|
Filters() template.FuncMap
|
2019-12-09 15:02:12 +00:00
|
|
|
Render(name string, data interface{}) error
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type luaPlugin struct {
|
2019-12-09 16:25:02 +00:00
|
|
|
filename string
|
|
|
|
state *lua.LState
|
|
|
|
renderCallbacks map[string]*lua.LFunction
|
2019-12-09 16:54:24 +00:00
|
|
|
filters template.FuncMap
|
2019-12-09 15:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *luaPlugin) Name() string {
|
|
|
|
return p.filename
|
|
|
|
}
|
|
|
|
|
2019-12-09 16:25:02 +00:00
|
|
|
func (p *luaPlugin) onRender(l *lua.LState) int {
|
|
|
|
name := l.CheckString(1)
|
|
|
|
f := l.CheckFunction(2)
|
|
|
|
p.renderCallbacks[name] = f
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-12-09 16:54:24 +00:00
|
|
|
func (p *luaPlugin) setFilter(l *lua.LState) int {
|
|
|
|
name := l.CheckString(1)
|
|
|
|
f := l.CheckFunction(2)
|
2019-12-09 17:35:51 +00:00
|
|
|
p.filters[name] = func(args ...interface{}) string {
|
2019-12-09 16:54:24 +00:00
|
|
|
luaArgs := make([]lua.LValue, len(args))
|
|
|
|
for i, v := range args {
|
|
|
|
luaArgs[i] = luar.New(l, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := l.CallByParam(lua.P{
|
2019-12-09 17:35:51 +00:00
|
|
|
Fn: f,
|
|
|
|
NRet: 1,
|
2019-12-09 16:54:24 +00:00
|
|
|
Protect: true,
|
|
|
|
}, luaArgs...)
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // TODO: better error handling?
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := l.CheckString(-1)
|
|
|
|
l.Pop(1)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:02:12 +00:00
|
|
|
func (p *luaPlugin) Render(name string, data interface{}) error {
|
2019-12-09 16:25:02 +00:00
|
|
|
f, ok := p.renderCallbacks[name]
|
|
|
|
if !ok {
|
2019-12-09 15:02:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-09 16:54:24 +00:00
|
|
|
err := p.state.CallByParam(lua.P{
|
2019-12-09 16:25:02 +00:00
|
|
|
Fn: f,
|
2019-12-09 15:02:12 +00:00
|
|
|
NRet: 0,
|
|
|
|
Protect: true,
|
2019-12-09 16:54:24 +00:00
|
|
|
}, luar.New(p.state, data))
|
|
|
|
if err != nil {
|
2019-12-09 15:02:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-09 16:54:24 +00:00
|
|
|
func (p *luaPlugin) Filters() template.FuncMap {
|
|
|
|
return p.filters
|
|
|
|
}
|
|
|
|
|
2019-12-09 15:02:12 +00:00
|
|
|
func (p *luaPlugin) Close() error {
|
|
|
|
p.state.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadLuaPlugin(filename string) (*luaPlugin, error) {
|
|
|
|
l := lua.NewState()
|
2019-12-09 16:25:02 +00:00
|
|
|
p := &luaPlugin{
|
|
|
|
filename: filename,
|
|
|
|
state: l,
|
|
|
|
renderCallbacks: make(map[string]*lua.LFunction),
|
2019-12-09 16:54:24 +00:00
|
|
|
filters: make(template.FuncMap),
|
2019-12-09 16:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mt := l.NewTypeMetatable("koushin")
|
|
|
|
l.SetGlobal("koushin", mt)
|
|
|
|
l.SetField(mt, "on_render", l.NewFunction(p.onRender))
|
2019-12-09 16:54:24 +00:00
|
|
|
l.SetField(mt, "set_filter", l.NewFunction(p.setFilter))
|
2019-12-09 16:25:02 +00:00
|
|
|
|
2019-12-09 15:02:12 +00:00
|
|
|
if err := l.DoFile(filename); err != nil {
|
2019-12-09 16:25:02 +00:00
|
|
|
l.Close()
|
2019-12-09 15:02:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-09 16:25:02 +00:00
|
|
|
return p, nil
|
2019-12-09 15:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadAllLuaPlugins(log echo.Logger) ([]Plugin, error) {
|
|
|
|
filenames, err := filepath.Glob("plugins/*.lua")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("filepath.Glob failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins := make([]Plugin, 0, len(filenames))
|
|
|
|
for _, filename := range filenames {
|
|
|
|
log.Printf("Loading Lua plugin '%v'", 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)
|
|
|
|
}
|
|
|
|
plugins = append(plugins, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return plugins, nil
|
|
|
|
}
|