aboutsummaryrefslogtreecommitdiff
path: root/plugins/lua/lua.go
blob: d23670267d85a655954811d3e95efa483f532a71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package koushinlua

import (
	"fmt"
	"html/template"
	"path/filepath"

	"git.sr.ht/~emersion/koushin"
	"github.com/labstack/echo/v4"
	"github.com/yuin/gopher-lua"
	"layeh.com/gopher-luar"
)

type luaRoute struct {
	method string
	path   string
	f      *lua.LFunction
}

type luaPlugin struct {
	filename        string
	state           *lua.LState
	renderCallbacks map[string]*lua.LFunction
	filters         template.FuncMap
	routes          []luaRoute
}

func (p *luaPlugin) Name() string {
	return p.filename
}

func (p *luaPlugin) onRender(l *lua.LState) int {
	name := l.CheckString(1)
	f := l.CheckFunction(2)
	p.renderCallbacks[name] = f
	return 0
}

func (p *luaPlugin) setFilter(l *lua.LState) int {
	name := l.CheckString(1)
	f := l.CheckFunction(2)
	p.filters[name] = func(args ...interface{}) string {
		luaArgs := make([]lua.LValue, len(args))
		for i, v := range args {
			luaArgs[i] = luar.New(l, v)
		}

		err := l.CallByParam(lua.P{
			Fn:      f,
			NRet:    1,
			Protect: true,
		}, luaArgs...)
		if err != nil {
			panic(err) // TODO: better error handling?
		}

		ret := l.CheckString(-1)
		l.Pop(1)
		return ret
	}
	return 0
}

func (p *luaPlugin) setRoute(l *lua.LState) int {
	method := l.CheckString(1)
	path := l.CheckString(2)
	f := l.CheckFunction(3)
	p.routes = append(p.routes, luaRoute{method, path, f})
	return 0
}

func (p *luaPlugin) inject(name string, data koushin.RenderData) error {
	f, ok := p.renderCallbacks[name]
	if !ok {
		return nil
	}

	err := p.state.CallByParam(lua.P{
		Fn:      f,
		NRet:    0,
		Protect: true,
	}, luar.New(p.state, data))
	if err != nil {
		return err
	}

	return nil
}

func (p *luaPlugin) Inject(ctx *koushin.Context, name string, data koushin.RenderData) error {
	if err := p.inject("*", data); err != nil {
		return err
	}
	return p.inject(name, data)
}

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) {
	for _, r := range p.routes {
		group.Match([]string{r.method}, r.path, func(ctx echo.Context) error {
			err := p.state.CallByParam(lua.P{
				Fn:      r.f,
				NRet:    0,
				Protect: true,
			}, luar.New(p.state, ctx))
			if err != nil {
				return fmt.Errorf("Lua plugin error: %v", err)
			}

			return nil
		})
	}

	_, name := filepath.Split(filepath.Dir(p.filename))
	group.Static("/plugins/"+name+"/assets", filepath.Dir(p.filename)+"/public/assets")
}

func (p *luaPlugin) Close() error {
	p.state.Close()
	return nil
}

func loadLuaPlugin(filename string) (*luaPlugin, error) {
	l := lua.NewState()
	p := &luaPlugin{
		filename:        filename,
		state:           l,
		renderCallbacks: make(map[string]*lua.LFunction),
		filters:         make(template.FuncMap),
	}

	mt := l.NewTypeMetatable("koushin")
	l.SetGlobal("koushin", mt)
	l.SetField(mt, "on_render", l.NewFunction(p.onRender))
	l.SetField(mt, "set_filter", l.NewFunction(p.setFilter))
	l.SetField(mt, "set_route", l.NewFunction(p.setRoute))

	if err := l.DoFile(filename); err != nil {
		l.Close()
		return nil, err
	}

	return p, nil
}

func loadAllLuaPlugins(s *koushin.Server) ([]koushin.Plugin, error) {
	log := s.Logger()

	filenames, err := filepath.Glob(koushin.PluginDir + "/*/main.lua")
	if err != nil {
		return nil, fmt.Errorf("filepath.Glob failed: %v", err)
	}

	plugins := make([]koushin.Plugin, 0, len(filenames))
	for _, filename := range filenames {
		log.Printf("Loading Lua plugin %q", filename)

		p, err := loadLuaPlugin(filename)
		if err != nil {
			for _, p := range plugins {
				p.Close()
			}
			return nil, fmt.Errorf("failed to load Lua plugin %q: %v", filename, err)
		}
		plugins = append(plugins, p)
	}

	return plugins, nil
}