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-09-20 19:01:24 +08:00
|
|
|
"sort"
|
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-12-19 18:55:52 +08:00
|
|
|
RootURL string `json:"rootURL"`
|
|
|
|
FromEmail string `json:"fromEmail"`
|
|
|
|
Messengers []string `json:"messengers"`
|
|
|
|
MediaProvider string `json:"mediaProvider"`
|
|
|
|
NeedsRestart bool `json:"needsRestart"`
|
|
|
|
Update *AppUpdate `json:"update"`
|
|
|
|
Langs []i18nLang `json:"langs"`
|
|
|
|
Lang json.RawMessage `json:"lang"`
|
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,
|
|
|
|
MediaProvider: app.constants.MediaProvider,
|
2018-11-03 02:03:00 +08:00
|
|
|
}
|
2020-07-08 19:00:14 +08:00
|
|
|
)
|
|
|
|
|
2020-12-19 18:55:52 +08:00
|
|
|
// Language list.
|
|
|
|
langList, err := geti18nLangList(app.constants.Lang, app)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error loading language list: %v", err))
|
|
|
|
}
|
|
|
|
out.Langs = langList
|
|
|
|
|
|
|
|
// Current language.
|
|
|
|
out.Lang = json.RawMessage(app.i18n.JSON())
|
|
|
|
|
2020-09-20 19:01:24 +08:00
|
|
|
// Sort messenger names with `email` always as the first item.
|
|
|
|
var names []string
|
|
|
|
for name := range app.messengers {
|
|
|
|
if name == emailMsgr {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
sort.Strings(names)
|
|
|
|
out.Messengers = append(out.Messengers, emailMsgr)
|
|
|
|
out.Messengers = append(out.Messengers, names...)
|
|
|
|
|
2020-07-08 19:00:14 +08:00
|
|
|
app.Lock()
|
|
|
|
out.NeedsRestart = app.needsRestart
|
2020-08-08 19:29:47 +08:00
|
|
|
out.Update = app.update
|
2020-07-08 19:00:14 +08:00
|
|
|
app.Unlock()
|
2018-11-03 02:03:00 +08:00
|
|
|
|
2020-12-19 18:55:52 +08:00
|
|
|
// Write the Javascript variable opening;
|
|
|
|
b := bytes.Buffer{}
|
2018-11-03 02:03:00 +08:00
|
|
|
b.Write([]byte(`var CONFIG = `))
|
2020-12-19 18:55:52 +08:00
|
|
|
|
|
|
|
// Encode the config payload as JSON and write as the variable's value assignment.
|
|
|
|
j := json.NewEncoder(&b)
|
|
|
|
if err := j.Encode(out); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
app.i18n.Ts("admin.errorMarshallingConfig", map[string]string{
|
|
|
|
"error": err.Error(),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
return c.Blob(http.StatusOK, "application/javascript; charset=utf-8", b.Bytes())
|
2018-11-03 02:03:00 +08:00
|
|
|
}
|
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,
|
2020-12-19 18:55:52 +08:00
|
|
|
app.i18n.Ts("globals.messages.errorFetching", map[string]string{
|
|
|
|
"name": "dashboard charts",
|
|
|
|
"error": pqErrMsg(err),
|
|
|
|
}))
|
2018-11-05 13:49:08 +08:00
|
|
|
}
|
|
|
|
|
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,
|
2020-12-19 18:55:52 +08:00
|
|
|
app.i18n.Ts("globals.messages.errorFetching", map[string]string{
|
|
|
|
"name": "dashboard stats",
|
|
|
|
"error": pqErrMsg(err),
|
|
|
|
}))
|
2020-07-05 00:55:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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})
|
|
|
|
}
|