2018-10-25 21:51:47 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-11-03 02:03:00 +08:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-11-05 13:49:08 +08:00
|
|
|
"fmt"
|
2018-10-25 21:51:47 +08:00
|
|
|
"net/http"
|
2020-07-08 19:00:14 +08:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2018-10-25 21:51:47 +08:00
|
|
|
|
2018-11-05 13:49:08 +08:00
|
|
|
"github.com/jmoiron/sqlx/types"
|
2018-10-25 21:51:47 +08:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
2018-11-03 02:03:00 +08:00
|
|
|
type configScript struct {
|
2020-07-05 20:05:05 +08:00
|
|
|
RootURL string `json:"rootURL"`
|
|
|
|
FromEmail string `json:"fromEmail"`
|
|
|
|
Messengers []string `json:"messengers"`
|
2020-07-08 19:00:14 +08:00
|
|
|
MediaProvider string `json:"mediaProvider"`
|
|
|
|
NeedsRestart bool `json:"needsRestart"`
|
2018-11-03 02:03:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleGetConfigScript returns general configuration as a Javascript
|
|
|
|
// variable that can be included in an HTML page directly.
|
|
|
|
func handleGetConfigScript(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
out = configScript{
|
2020-07-05 20:05:05 +08:00
|
|
|
RootURL: app.constants.RootURL,
|
|
|
|
FromEmail: app.constants.FromEmail,
|
|
|
|
Messengers: app.manager.GetMessengerNames(),
|
|
|
|
MediaProvider: app.constants.MediaProvider,
|
2018-11-03 02:03:00 +08:00
|
|
|
}
|
2020-07-08 19:00:14 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
app.Lock()
|
|
|
|
out.NeedsRestart = app.needsRestart
|
|
|
|
app.Unlock()
|
2018-11-03 02:03:00 +08:00
|
|
|
|
2020-07-08 19:00:14 +08:00
|
|
|
var (
|
2018-11-03 02:03:00 +08:00
|
|
|
b = bytes.Buffer{}
|
|
|
|
j = json.NewEncoder(&b)
|
|
|
|
)
|
|
|
|
b.Write([]byte(`var CONFIG = `))
|
2019-10-25 13:41:47 +08:00
|
|
|
_ = j.Encode(out)
|
2018-11-03 02:03:00 +08:00
|
|
|
return c.Blob(http.StatusOK, "application/javascript", b.Bytes())
|
|
|
|
}
|
2018-11-05 13:49:08 +08:00
|
|
|
|
2020-07-05 00:55:02 +08:00
|
|
|
// handleGetDashboardCharts returns chart data points to render ont he dashboard.
|
|
|
|
func handleGetDashboardCharts(c echo.Context) error {
|
2018-11-05 13:49:08 +08:00
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
2020-07-05 00:55:02 +08:00
|
|
|
out types.JSONText
|
2018-11-05 13:49:08 +08:00
|
|
|
)
|
|
|
|
|
2020-07-05 00:55:02 +08:00
|
|
|
if err := app.queries.GetDashboardCharts.Get(&out); err != nil {
|
2018-11-05 13:49:08 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error fetching dashboard stats: %s", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
2020-07-05 00:55:02 +08:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleGetDashboardCounts returns stats counts to show on the dashboard.
|
|
|
|
func handleGetDashboardCounts(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
out types.JSONText
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := app.queries.GetDashboardCounts.Get(&out); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error fetching dashboard statsc counts: %s", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-11-05 13:49:08 +08:00
|
|
|
}
|
2020-07-08 19:00:14 +08:00
|
|
|
|
|
|
|
// handleReloadApp restarts the app.
|
|
|
|
func handleReloadApp(c echo.Context) error {
|
|
|
|
app := c.Get("app").(*App)
|
|
|
|
go func() {
|
|
|
|
<-time.After(time.Millisecond * 500)
|
|
|
|
app.sigChan <- syscall.SIGHUP
|
|
|
|
}()
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|