From f106c1125f3d0e0f9772d2c5f595fc52ec7dda5b Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 17 Dec 2019 15:19:37 +0100 Subject: Make Go plugin handlers take a *Context Take a *Context instead of a echo.Context. This saves a type assertion in each handler. --- plugin_go.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'plugin_go.go') diff --git a/plugin_go.go b/plugin_go.go index 9974c17..34abeeb 100644 --- a/plugin_go.go +++ b/plugin_go.go @@ -34,7 +34,10 @@ func (p *goPlugin) LoadTemplate(t *template.Template) error { func (p *goPlugin) SetRoutes(group *echo.Group) { for _, r := range p.p.routes { - group.Add(r.Method, r.Path, r.Handler) + h := r.Handler + group.Add(r.Method, r.Path, func(ectx echo.Context) error { + return h(ectx.(*Context)) + }) } group.Static("/plugins/"+p.p.Name+"/assets", pluginDir+"/"+p.p.Name+"/public/assets") @@ -59,7 +62,7 @@ func (p *goPlugin) Close() error { type goPluginRoute struct { Method string Path string - Handler echo.HandlerFunc + Handler HandlerFunc } // GoPlugin is a helper to create Go plugins. @@ -75,30 +78,30 @@ type GoPlugin struct { routes []goPluginRoute templateFuncs template.FuncMap - injectFuncs map[string]InjectFunc + injectFuncs map[string]InjectFunc } +// HandlerFunc is a function serving HTTP requests. +type HandlerFunc func(*Context) error + // AddRoute registers a new HTTP route. -// -// The echo.Context passed to the HTTP handler can be type-asserted to -// *koushin.Context. -func (p *GoPlugin) AddRoute(method, path string, handler echo.HandlerFunc) { +func (p *GoPlugin) AddRoute(method, path string, handler HandlerFunc) { p.routes = append(p.routes, goPluginRoute{method, path, handler}) } -func (p *GoPlugin) DELETE(path string, handler echo.HandlerFunc) { +func (p *GoPlugin) DELETE(path string, handler HandlerFunc) { p.AddRoute(http.MethodDelete, path, handler) } -func (p *GoPlugin) GET(path string, handler echo.HandlerFunc) { +func (p *GoPlugin) GET(path string, handler HandlerFunc) { p.AddRoute(http.MethodGet, path, handler) } -func (p *GoPlugin) POST(path string, handler echo.HandlerFunc) { +func (p *GoPlugin) POST(path string, handler HandlerFunc) { p.AddRoute(http.MethodPost, path, handler) } -func (p *GoPlugin) PUT(path string, handler echo.HandlerFunc) { +func (p *GoPlugin) PUT(path string, handler HandlerFunc) { p.AddRoute(http.MethodPut, path, handler) } -- cgit v1.2.3-59-g8ed1b