diff options
author | Simon Ser <contact@emersion.fr> | 2019-12-17 15:19:37 +0100 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2019-12-17 15:19:37 +0100 |
commit | f106c1125f3d0e0f9772d2c5f595fc52ec7dda5b (patch) | |
tree | ff76c2fa8dd3d9f1c6f17682b07abf419f33a2f0 | |
parent | 020e27fe459b0ed66a48b46cb2878520ed4a517b (diff) | |
download | alps-f106c1125f3d0e0f9772d2c5f595fc52ec7dda5b.tar.gz alps-f106c1125f3d0e0f9772d2c5f595fc52ec7dda5b.zip |
Make Go plugin handlers take a *Context
Take a *Context instead of a echo.Context. This saves a type assertion
in each handler.
-rw-r--r-- | plugin_go.go | 25 | ||||
-rw-r--r-- | plugins/base/routes.go | 38 |
2 files changed, 25 insertions, 38 deletions
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) } diff --git a/plugins/base/routes.go b/plugins/base/routes.go index f3f196a..35e72de 100644 --- a/plugins/base/routes.go +++ b/plugins/base/routes.go @@ -19,19 +19,17 @@ import ( ) func registerRoutes(p *koushin.GoPlugin) { - p.GET("/", func(ectx echo.Context) error { - return ectx.Redirect(http.StatusFound, "/mailbox/INBOX") + p.GET("/", func(ctx *koushin.Context) error { + return ctx.Redirect(http.StatusFound, "/mailbox/INBOX") }) p.GET("/mailbox/:mbox", handleGetMailbox) p.POST("/mailbox/:mbox", handleGetMailbox) - p.GET("/message/:mbox/:uid", func(ectx echo.Context) error { - ctx := ectx.(*koushin.Context) + p.GET("/message/:mbox/:uid", func(ctx *koushin.Context) error { return handleGetPart(ctx, false) }) - p.GET("/message/:mbox/:uid/raw", func(ectx echo.Context) error { - ctx := ectx.(*koushin.Context) + p.GET("/message/:mbox/:uid/raw", func(ctx *koushin.Context) error { return handleGetPart(ctx, true) }) @@ -62,9 +60,7 @@ type MailboxRenderData struct { Query string } -func handleGetMailbox(ectx echo.Context) error { - ctx := ectx.(*koushin.Context) - +func handleGetMailbox(ctx *koushin.Context) error { mboxName, err := url.PathUnescape(ctx.Param("mbox")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, err) @@ -125,9 +121,7 @@ func handleGetMailbox(ectx echo.Context) error { }) } -func handleLogin(ectx echo.Context) error { - ctx := ectx.(*koushin.Context) - +func handleLogin(ctx *koushin.Context) error { username := ctx.FormValue("username") password := ctx.FormValue("password") if username != "" && password != "" { @@ -146,9 +140,7 @@ func handleLogin(ectx echo.Context) error { return ctx.Render(http.StatusOK, "login.html", koushin.NewBaseRenderData(ctx)) } -func handleLogout(ectx echo.Context) error { - ctx := ectx.(*koushin.Context) - +func handleLogout(ctx *koushin.Context) error { ctx.Session.Close() ctx.SetSession(nil) return ctx.Redirect(http.StatusFound, "/login") @@ -255,9 +247,7 @@ type ComposeRenderData struct { Message *OutgoingMessage } -func handleCompose(ectx echo.Context) error { - ctx := ectx.(*koushin.Context) - +func handleCompose(ctx *koushin.Context) error { var msg OutgoingMessage if strings.ContainsRune(ctx.Session.Username(), '@') { msg.From = ctx.Session.Username() @@ -358,9 +348,7 @@ func handleCompose(ectx echo.Context) error { }) } -func handleMove(ectx echo.Context) error { - ctx := ectx.(*koushin.Context) - +func handleMove(ctx *koushin.Context) error { mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, err) @@ -391,9 +379,7 @@ func handleMove(ectx echo.Context) error { return ctx.Redirect(http.StatusFound, fmt.Sprintf("/mailbox/%v", url.PathEscape(to))) } -func handleDelete(ectx echo.Context) error { - ctx := ectx.(*koushin.Context) - +func handleDelete(ctx *koushin.Context) error { mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, err) @@ -432,9 +418,7 @@ func handleDelete(ectx echo.Context) error { return ctx.Redirect(http.StatusFound, fmt.Sprintf("/mailbox/%v", url.PathEscape(mboxName))) } -func handleSetFlags(ectx echo.Context) error { - ctx := ectx.(*koushin.Context) - +func handleSetFlags(ctx *koushin.Context) error { mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid")) if err != nil { return echo.NewHTTPError(http.StatusBadRequest, err) |