Make Go plugin handlers take a *Context
Take a *Context instead of a echo.Context. This saves a type assertion in each handler.
This commit is contained in:
parent
020e27fe45
commit
f106c1125f
2 changed files with 25 additions and 38 deletions
23
plugin_go.go
23
plugin_go.go
|
@ -34,7 +34,10 @@ func (p *goPlugin) LoadTemplate(t *template.Template) error {
|
||||||
|
|
||||||
func (p *goPlugin) SetRoutes(group *echo.Group) {
|
func (p *goPlugin) SetRoutes(group *echo.Group) {
|
||||||
for _, r := range p.p.routes {
|
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")
|
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 {
|
type goPluginRoute struct {
|
||||||
Method string
|
Method string
|
||||||
Path string
|
Path string
|
||||||
Handler echo.HandlerFunc
|
Handler HandlerFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// GoPlugin is a helper to create Go plugins.
|
// GoPlugin is a helper to create Go plugins.
|
||||||
|
@ -78,27 +81,27 @@ type GoPlugin struct {
|
||||||
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.
|
// AddRoute registers a new HTTP route.
|
||||||
//
|
func (p *GoPlugin) AddRoute(method, path string, handler HandlerFunc) {
|
||||||
// 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) {
|
|
||||||
p.routes = append(p.routes, goPluginRoute{method, path, handler})
|
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)
|
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)
|
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)
|
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)
|
p.AddRoute(http.MethodPut, path, handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,19 +19,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func registerRoutes(p *koushin.GoPlugin) {
|
func registerRoutes(p *koushin.GoPlugin) {
|
||||||
p.GET("/", func(ectx echo.Context) error {
|
p.GET("/", func(ctx *koushin.Context) error {
|
||||||
return ectx.Redirect(http.StatusFound, "/mailbox/INBOX")
|
return ctx.Redirect(http.StatusFound, "/mailbox/INBOX")
|
||||||
})
|
})
|
||||||
|
|
||||||
p.GET("/mailbox/:mbox", handleGetMailbox)
|
p.GET("/mailbox/:mbox", handleGetMailbox)
|
||||||
p.POST("/mailbox/:mbox", handleGetMailbox)
|
p.POST("/mailbox/:mbox", handleGetMailbox)
|
||||||
|
|
||||||
p.GET("/message/:mbox/:uid", func(ectx echo.Context) error {
|
p.GET("/message/:mbox/:uid", func(ctx *koushin.Context) error {
|
||||||
ctx := ectx.(*koushin.Context)
|
|
||||||
return handleGetPart(ctx, false)
|
return handleGetPart(ctx, false)
|
||||||
})
|
})
|
||||||
p.GET("/message/:mbox/:uid/raw", func(ectx echo.Context) error {
|
p.GET("/message/:mbox/:uid/raw", func(ctx *koushin.Context) error {
|
||||||
ctx := ectx.(*koushin.Context)
|
|
||||||
return handleGetPart(ctx, true)
|
return handleGetPart(ctx, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -62,9 +60,7 @@ type MailboxRenderData struct {
|
||||||
Query string
|
Query string
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleGetMailbox(ectx echo.Context) error {
|
func handleGetMailbox(ctx *koushin.Context) error {
|
||||||
ctx := ectx.(*koushin.Context)
|
|
||||||
|
|
||||||
mboxName, err := url.PathUnescape(ctx.Param("mbox"))
|
mboxName, err := url.PathUnescape(ctx.Param("mbox"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
return echo.NewHTTPError(http.StatusBadRequest, err)
|
||||||
|
@ -125,9 +121,7 @@ func handleGetMailbox(ectx echo.Context) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLogin(ectx echo.Context) error {
|
func handleLogin(ctx *koushin.Context) error {
|
||||||
ctx := ectx.(*koushin.Context)
|
|
||||||
|
|
||||||
username := ctx.FormValue("username")
|
username := ctx.FormValue("username")
|
||||||
password := ctx.FormValue("password")
|
password := ctx.FormValue("password")
|
||||||
if username != "" && password != "" {
|
if username != "" && password != "" {
|
||||||
|
@ -146,9 +140,7 @@ func handleLogin(ectx echo.Context) error {
|
||||||
return ctx.Render(http.StatusOK, "login.html", koushin.NewBaseRenderData(ctx))
|
return ctx.Render(http.StatusOK, "login.html", koushin.NewBaseRenderData(ctx))
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleLogout(ectx echo.Context) error {
|
func handleLogout(ctx *koushin.Context) error {
|
||||||
ctx := ectx.(*koushin.Context)
|
|
||||||
|
|
||||||
ctx.Session.Close()
|
ctx.Session.Close()
|
||||||
ctx.SetSession(nil)
|
ctx.SetSession(nil)
|
||||||
return ctx.Redirect(http.StatusFound, "/login")
|
return ctx.Redirect(http.StatusFound, "/login")
|
||||||
|
@ -255,9 +247,7 @@ type ComposeRenderData struct {
|
||||||
Message *OutgoingMessage
|
Message *OutgoingMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleCompose(ectx echo.Context) error {
|
func handleCompose(ctx *koushin.Context) error {
|
||||||
ctx := ectx.(*koushin.Context)
|
|
||||||
|
|
||||||
var msg OutgoingMessage
|
var msg OutgoingMessage
|
||||||
if strings.ContainsRune(ctx.Session.Username(), '@') {
|
if strings.ContainsRune(ctx.Session.Username(), '@') {
|
||||||
msg.From = ctx.Session.Username()
|
msg.From = ctx.Session.Username()
|
||||||
|
@ -358,9 +348,7 @@ func handleCompose(ectx echo.Context) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleMove(ectx echo.Context) error {
|
func handleMove(ctx *koushin.Context) error {
|
||||||
ctx := ectx.(*koushin.Context)
|
|
||||||
|
|
||||||
mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid"))
|
mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
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)))
|
return ctx.Redirect(http.StatusFound, fmt.Sprintf("/mailbox/%v", url.PathEscape(to)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleDelete(ectx echo.Context) error {
|
func handleDelete(ctx *koushin.Context) error {
|
||||||
ctx := ectx.(*koushin.Context)
|
|
||||||
|
|
||||||
mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid"))
|
mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
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)))
|
return ctx.Redirect(http.StatusFound, fmt.Sprintf("/mailbox/%v", url.PathEscape(mboxName)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSetFlags(ectx echo.Context) error {
|
func handleSetFlags(ctx *koushin.Context) error {
|
||||||
ctx := ectx.(*koushin.Context)
|
|
||||||
|
|
||||||
mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid"))
|
mboxName, uid, err := parseMboxAndUid(ctx.Param("mbox"), ctx.Param("uid"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, err)
|
return echo.NewHTTPError(http.StatusBadRequest, err)
|
||||||
|
|
Loading…
Reference in a new issue