A webmail client. Forked from https://git.sr.ht/~migadu/alps
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

132 lines
2.9 KiB

  1. package koushin
  2. import (
  3. "html/template"
  4. "net/http"
  5. "path/filepath"
  6. "github.com/labstack/echo/v4"
  7. )
  8. type goPlugin struct {
  9. p *GoPlugin
  10. }
  11. func (p *goPlugin) Name() string {
  12. return p.p.Name
  13. }
  14. func (p *goPlugin) LoadTemplate(t *template.Template) error {
  15. t.Funcs(p.p.templateFuncs)
  16. paths, err := filepath.Glob(pluginDir + "/" + p.p.Name + "/public/*.html")
  17. if err != nil {
  18. return err
  19. }
  20. if len(paths) > 0 {
  21. if _, err := t.ParseFiles(paths...); err != nil {
  22. return err
  23. }
  24. }
  25. return nil
  26. }
  27. func (p *goPlugin) SetRoutes(group *echo.Group) {
  28. for _, r := range p.p.routes {
  29. group.Add(r.Method, r.Path, r.Handler)
  30. }
  31. group.Static("/plugins/"+p.p.Name+"/assets", pluginDir+"/"+p.p.Name+"/public/assets")
  32. }
  33. func (p *goPlugin) Inject(name string, data RenderData) error {
  34. if f, ok := p.p.injectFuncs["*"]; ok {
  35. if err := f(data); err != nil {
  36. return err
  37. }
  38. }
  39. if f, ok := p.p.injectFuncs[name]; ok {
  40. return f(data)
  41. }
  42. return nil
  43. }
  44. func (p *goPlugin) Close() error {
  45. return nil
  46. }
  47. type goPluginRoute struct {
  48. Method string
  49. Path string
  50. Handler echo.HandlerFunc
  51. }
  52. // GoPlugin is a helper to create Go plugins.
  53. //
  54. // Use this struct to define your plugin, then call RegisterPlugin:
  55. //
  56. // p := GoPlugin{Name: "my-plugin"}
  57. // // Define routes, template functions, etc
  58. // koushin.RegisterPlugin(p.Plugin())
  59. type GoPlugin struct {
  60. Name string
  61. routes []goPluginRoute
  62. templateFuncs template.FuncMap
  63. injectFuncs map[string]InjectFunc
  64. }
  65. // AddRoute registers a new HTTP route.
  66. //
  67. // The echo.Context passed to the HTTP handler can be type-asserted to
  68. // *koushin.Context.
  69. func (p *GoPlugin) AddRoute(method, path string, handler echo.HandlerFunc) {
  70. p.routes = append(p.routes, goPluginRoute{method, path, handler})
  71. }
  72. func (p *GoPlugin) DELETE(path string, handler echo.HandlerFunc) {
  73. p.AddRoute(http.MethodDelete, path, handler)
  74. }
  75. func (p *GoPlugin) GET(path string, handler echo.HandlerFunc) {
  76. p.AddRoute(http.MethodGet, path, handler)
  77. }
  78. func (p *GoPlugin) POST(path string, handler echo.HandlerFunc) {
  79. p.AddRoute(http.MethodPost, path, handler)
  80. }
  81. func (p *GoPlugin) PUT(path string, handler echo.HandlerFunc) {
  82. p.AddRoute(http.MethodPut, path, handler)
  83. }
  84. // TemplateFuncs registers new template functions.
  85. func (p *GoPlugin) TemplateFuncs(funcs template.FuncMap) {
  86. if p.templateFuncs == nil {
  87. p.templateFuncs = make(template.FuncMap, len(funcs))
  88. }
  89. for k, f := range funcs {
  90. p.templateFuncs[k] = f
  91. }
  92. }
  93. // InjectFunc is a function that injects data prior to rendering a template.
  94. type InjectFunc func(data RenderData) error
  95. // Inject registers a function to execute prior to rendering a template. The
  96. // special name "*" matches any template.
  97. func (p *GoPlugin) Inject(name string, f InjectFunc) {
  98. if p.injectFuncs == nil {
  99. p.injectFuncs = make(map[string]InjectFunc)
  100. }
  101. p.injectFuncs[name] = f
  102. }
  103. // Plugin returns an object implementing Plugin.
  104. func (p *GoPlugin) Plugin() Plugin {
  105. return &goPlugin{p}
  106. }