2018-10-25 21:51:47 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
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
|
|
|
|
2021-12-09 23:21:07 +08:00
|
|
|
"github.com/labstack/echo/v4"
|
2018-10-25 21:51:47 +08:00
|
|
|
)
|
|
|
|
|
2021-02-13 15:04:36 +08: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"`
|
2021-06-09 22:41:45 +08:00
|
|
|
Version string `json:"version"`
|
2018-11-03 02:03:00 +08:00
|
|
|
}
|
|
|
|
|
2021-02-13 15:04:36 +08:00
|
|
|
// handleGetServerConfig returns general server config.
|
|
|
|
func handleGetServerConfig(c echo.Context) error {
|
2018-11-03 02:03:00 +08:00
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
2021-02-13 15:04:36 +08:00
|
|
|
out = serverConfig{}
|
2020-07-08 19:00:14 +08:00
|
|
|
)
|
|
|
|
|
2020-12-19 18:55:52 +08:00
|
|
|
// Language list.
|
2021-02-13 15:04:36 +08:00
|
|
|
langList, err := getI18nLangList(app.constants.Lang, app)
|
2020-12-19 18:55:52 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error loading language list: %v", err))
|
|
|
|
}
|
|
|
|
out.Langs = langList
|
2021-02-13 15:04:36 +08:00
|
|
|
out.Lang = app.constants.Lang
|
2020-12-19 18:55:52 +08:00
|
|
|
|
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()
|
2021-06-09 22:41:45 +08:00
|
|
|
out.Version = versionString
|
2018-11-03 02:03:00 +08:00
|
|
|
|
2021-02-13 15:04:36 +08:00
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
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)
|
|
|
|
)
|
|
|
|
|
2022-04-03 23:24:40 +08:00
|
|
|
out, err := app.core.GetDashboardCharts()
|
|
|
|
if err != nil {
|
|
|
|
return 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)
|
|
|
|
)
|
|
|
|
|
2022-04-03 23:24:40 +08:00
|
|
|
out, err := app.core.GetDashboardCounts()
|
|
|
|
if err != nil {
|
|
|
|
return 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)
|
2023-05-27 00:37:58 +08:00
|
|
|
app.chReload <- syscall.SIGHUP
|
2020-07-08 19:00:14 +08:00
|
|
|
}()
|
|
|
|
return c.JSON(http.StatusOK, okResp{true})
|
|
|
|
}
|