admin.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "syscall"
  8. "time"
  9. "github.com/jmoiron/sqlx/types"
  10. "github.com/labstack/echo"
  11. )
  12. type configScript struct {
  13. RootURL string `json:"rootURL"`
  14. FromEmail string `json:"fromEmail"`
  15. Messengers []string `json:"messengers"`
  16. MediaProvider string `json:"mediaProvider"`
  17. NeedsRestart bool `json:"needsRestart"`
  18. }
  19. // handleGetConfigScript returns general configuration as a Javascript
  20. // variable that can be included in an HTML page directly.
  21. func handleGetConfigScript(c echo.Context) error {
  22. var (
  23. app = c.Get("app").(*App)
  24. out = configScript{
  25. RootURL: app.constants.RootURL,
  26. FromEmail: app.constants.FromEmail,
  27. Messengers: app.manager.GetMessengerNames(),
  28. MediaProvider: app.constants.MediaProvider,
  29. }
  30. )
  31. app.Lock()
  32. out.NeedsRestart = app.needsRestart
  33. app.Unlock()
  34. var (
  35. b = bytes.Buffer{}
  36. j = json.NewEncoder(&b)
  37. )
  38. b.Write([]byte(`var CONFIG = `))
  39. _ = j.Encode(out)
  40. return c.Blob(http.StatusOK, "application/javascript", b.Bytes())
  41. }
  42. // handleGetDashboardCharts returns chart data points to render ont he dashboard.
  43. func handleGetDashboardCharts(c echo.Context) error {
  44. var (
  45. app = c.Get("app").(*App)
  46. out types.JSONText
  47. )
  48. if err := app.queries.GetDashboardCharts.Get(&out); err != nil {
  49. return echo.NewHTTPError(http.StatusInternalServerError,
  50. fmt.Sprintf("Error fetching dashboard stats: %s", pqErrMsg(err)))
  51. }
  52. return c.JSON(http.StatusOK, okResp{out})
  53. }
  54. // handleGetDashboardCounts returns stats counts to show on the dashboard.
  55. func handleGetDashboardCounts(c echo.Context) error {
  56. var (
  57. app = c.Get("app").(*App)
  58. out types.JSONText
  59. )
  60. if err := app.queries.GetDashboardCounts.Get(&out); err != nil {
  61. return echo.NewHTTPError(http.StatusInternalServerError,
  62. fmt.Sprintf("Error fetching dashboard statsc counts: %s", pqErrMsg(err)))
  63. }
  64. return c.JSON(http.StatusOK, okResp{out})
  65. }
  66. // handleReloadApp restarts the app.
  67. func handleReloadApp(c echo.Context) error {
  68. app := c.Get("app").(*App)
  69. go func() {
  70. <-time.After(time.Millisecond * 500)
  71. app.sigChan <- syscall.SIGHUP
  72. }()
  73. return c.JSON(http.StatusOK, okResp{true})
  74. }