plugin.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package controllers
  2. import (
  3. "github.com/Jinnrry/pmail/dto/response"
  4. "github.com/Jinnrry/pmail/hooks"
  5. "github.com/Jinnrry/pmail/utils/context"
  6. "io"
  7. "net/http"
  8. "strings"
  9. )
  10. func GetPluginList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
  11. ret := []string{}
  12. for s, _ := range hooks.HookList {
  13. ret = append(ret, s)
  14. }
  15. response.NewSuccessResponse(ret).FPrint(w)
  16. }
  17. func SettingsHtml(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
  18. args := strings.Split(req.RequestURI, "/")
  19. if len(args) < 4 {
  20. response.NewErrorResponse(response.ParamsError, "404", "").FPrint(w)
  21. return
  22. }
  23. pluginName := args[4]
  24. if plugin, ok := hooks.HookList[pluginName]; ok {
  25. dt, err := io.ReadAll(req.Body)
  26. if err != nil {
  27. response.NewErrorResponse(response.ParamsError, err.Error(), "").FPrint(w)
  28. return
  29. }
  30. html := plugin.SettingsHtml(ctx,
  31. strings.Join(args[4:], "/"),
  32. string(dt),
  33. )
  34. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  35. w.Write([]byte(html))
  36. return
  37. }
  38. response.NewErrorResponse(response.ParamsError, "404", "")
  39. }