aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2019-12-17 15:14:15 +0100
committerSimon Ser <contact@emersion.fr>2019-12-17 15:14:36 +0100
commit020e27fe459b0ed66a48b46cb2878520ed4a517b (patch)
tree9de95206d06bd9654cb5e40e81902e7b77cafedc
parentca3672df2ad75cbb866b08ca79320a75e30a55ce (diff)
downloadalps-020e27fe459b0ed66a48b46cb2878520ed4a517b.tar.gz
alps-020e27fe459b0ed66a48b46cb2878520ed4a517b.zip
Add Context to Plugin.Inject
This allows to access the request metadata and the session from injectors.
-rw-r--r--plugin.go2
-rw-r--r--plugin_go.go8
-rw-r--r--plugin_lua.go2
-rw-r--r--template.go2
4 files changed, 7 insertions, 7 deletions
diff --git a/plugin.go b/plugin.go
index 6d5b730..77000f2 100644
--- a/plugin.go
+++ b/plugin.go
@@ -18,7 +18,7 @@ type Plugin interface {
SetRoutes(group *echo.Group)
// Inject is called prior to rendering a template. It can extend the
// template data by setting new items in the Extra map.
- Inject(name string, data RenderData) error
+ Inject(ctx *Context, name string, data RenderData) error
// Close is called when the plugin is unloaded.
Close() error
}
diff --git a/plugin_go.go b/plugin_go.go
index 407363c..9974c17 100644
--- a/plugin_go.go
+++ b/plugin_go.go
@@ -40,14 +40,14 @@ func (p *goPlugin) SetRoutes(group *echo.Group) {
group.Static("/plugins/"+p.p.Name+"/assets", pluginDir+"/"+p.p.Name+"/public/assets")
}
-func (p *goPlugin) Inject(name string, data RenderData) error {
+func (p *goPlugin) Inject(ctx *Context, name string, data RenderData) error {
if f, ok := p.p.injectFuncs["*"]; ok {
- if err := f(data); err != nil {
+ if err := f(ctx, data); err != nil {
return err
}
}
if f, ok := p.p.injectFuncs[name]; ok {
- return f(data)
+ return f(ctx, data)
}
return nil
}
@@ -114,7 +114,7 @@ func (p *GoPlugin) TemplateFuncs(funcs template.FuncMap) {
}
// InjectFunc is a function that injects data prior to rendering a template.
-type InjectFunc func(data RenderData) error
+type InjectFunc func(ctx *Context, data RenderData) error
// Inject registers a function to execute prior to rendering a template. The
// special name "*" matches any template.
diff --git a/plugin_lua.go b/plugin_lua.go
index 816c7f1..8291a20 100644
--- a/plugin_lua.go
+++ b/plugin_lua.go
@@ -86,7 +86,7 @@ func (p *luaPlugin) inject(name string, data RenderData) error {
return nil
}
-func (p *luaPlugin) Inject(name string, data RenderData) error {
+func (p *luaPlugin) Inject(ctx *Context, name string, data RenderData) error {
if err := p.inject("*", data); err != nil {
return err
}
diff --git a/template.go b/template.go
index 6286e08..4b46d84 100644
--- a/template.go
+++ b/template.go
@@ -87,7 +87,7 @@ func (r *renderer) Render(w io.Writer, name string, data interface{}, ectx echo.
ctx := ectx.Get("context").(*Context)
for _, plugin := range ctx.Server.Plugins {
- if err := plugin.Inject(name, data.(RenderData)); err != nil {
+ if err := plugin.Inject(ctx, name, data.(RenderData)); err != nil {
return fmt.Errorf("failed to run plugin '%v': %v", plugin.Name(), err)
}
}