Browse Source

Make Go plugin handlers take a *Context

Take a *Context instead of a echo.Context. This saves a type assertion
in each handler.
master
Simon Ser 4 years ago
parent
commit
f106c1125f
No known key found for this signature in database GPG Key ID: FDE7BE0E88F5E48
2 changed files with 25 additions and 38 deletions
  1. +14
    -11
      plugin_go.go
  2. +11
    -27
      plugins/base/routes.go

+ 14
- 11
plugin_go.go View File

@@ -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)
}



+ 11
- 27
plugins/base/routes.go View File

@@ -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)


Loading…
Cancel
Save