2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-11-05 05:49:08 +00:00
|
|
|
"fmt"
|
2018-10-25 13:51:47 +00:00
|
|
|
"net/http"
|
2020-09-20 11:01:24 +00:00
|
|
|
"sort"
|
2020-07-08 11:00:14 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2018-11-05 05:49:08 +00:00
|
|
|
"github.com/jmoiron/sqlx/types"
|
2018-10-25 13:51:47 +00:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
2021-02-13 07:04:36 +00:00
|
|
|
type serverConfig struct {
|
|
|
|
Messengers []string `json:"messengers"`
|
|
|
|
Langs []i18nLang `json:"langs"`
|
|
|
|
Lang string `json:"lang"`
|
|
|
|
Update *AppUpdate `json:"update"`
|
|
|
|
NeedsRestart bool `json:"needs_restart"`
|
2018-11-02 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
2021-02-13 07:04:36 +00:00
|
|
|
// handleGetServerConfig returns general server config.
|
|
|
|
func handleGetServerConfig(c echo.Context) error {
|
2018-11-02 18:03:00 +00:00
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
2021-02-13 07:04:36 +00:00
|
|
|
out = serverConfig{}
|
2020-07-08 11:00:14 +00:00
|
|
|
)
|
|
|
|
|
2020-12-19 10:55:52 +00:00
|
|
|
// Language list.
|
2021-02-13 07:04:36 +00:00
|
|
|
langList, err := getI18nLangList(app.constants.Lang, app)
|
2020-12-19 10:55:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error loading language list: %v", err))
|
|
|
|
}
|
|
|
|
out.Langs = langList
|
2021-02-13 07:04:36 +00:00
|
|
|
out.Lang = app.constants.Lang
|
2020-12-19 10:55:52 +00:00
|
|
|
|
2020-09-20 11:01:24 +00: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 11:00:14 +00:00
|
|
|
app.Lock()
|
|
|
|
out.NeedsRestart = app.needsRestart
|
2020-08-08 11:29:47 +00:00
|
|
|
out.Update = app.update
|
2020-07-08 11:00:14 +00:00
|
|
|
app.Unlock()
|
2018-11-02 18:03:00 +00:00
|
|
|
|
2021-02-13 07:04:36 +00:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-11-02 18:03:00 +00:00
|
|
|
}
|
2018-11-05 05:49:08 +00:00
|
|
|
|
2020-07-04 16:55:02 +00:00
|
|
|
// handleGetDashboardCharts returns chart data points to render ont he dashboard.
|
|
|
|
func handleGetDashboardCharts(c echo.Context) error {
|
2018-11-05 05:49:08 +00:00
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
2020-07-04 16:55:02 +00:00
|
|
|
out types.JSONText
|
2018-11-05 05:49:08 +00:00
|
|
|
)
|
|
|
|
|
2020-07-04 16:55:02 +00:00
|
|
|
if err := app.queries.GetDashboardCharts.Get(&out); err != nil {
|
2018-11-05 05:49:08 +00:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("globals.messages.errorFetching", "name", "dashboard charts", "error", pqErrMsg(err)))
|
2018-11-05 05:49:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 16:55:02 +00: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,
|
2021-01-23 13:24:33 +00:00
|
|
|
app.i18n.Ts("globals.messages.errorFetching", "name", "dashboard stats", "error", pqErrMsg(err)))
|
2020-07-04 16:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-11-05 05:49:08 +00:00
|
|
|
}
|
2020-07-08 11:00:14 +00: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})
|
|
|
|
}
|