listmonk/cmd/admin.go

104 lines
2.4 KiB
Go
Raw Normal View History

2018-10-25 21:51:47 +08:00
package main
import (
"encoding/json"
"fmt"
2018-10-25 21:51:47 +08:00
"net/http"
"sort"
"syscall"
"time"
2018-10-25 21:51:47 +08:00
"github.com/labstack/echo/v4"
2018-10-25 21:51:47 +08:00
)
type serverConfig struct {
RootURL string `json:"root_url"`
FromEmail string `json:"from_email"`
Messengers []string `json:"messengers"`
Langs []i18nLang `json:"langs"`
Lang string `json:"lang"`
Permissions json.RawMessage `json:"permissions"`
Update *AppUpdate `json:"update"`
NeedsRestart bool `json:"needs_restart"`
Version string `json:"version"`
}
// handleGetServerConfig returns general server config.
func handleGetServerConfig(c echo.Context) error {
var (
app = c.Get("app").(*App)
)
out := serverConfig{
RootURL: app.constants.RootURL,
FromEmail: app.constants.FromEmail,
Lang: app.constants.Lang,
Permissions: app.constants.PermissionsRaw,
}
2020-12-19 18:55:52 +08:00
// Language list.
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
// 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...)
app.Lock()
out.NeedsRestart = app.needsRestart
out.Update = app.update
app.Unlock()
2021-06-09 22:41:45 +08:00
out.Version = versionString
return c.JSON(http.StatusOK, okResp{out})
}
2020-07-05 00:55:02 +08:00
// handleGetDashboardCharts returns chart data points to render ont he dashboard.
func handleGetDashboardCharts(c echo.Context) error {
var (
app = c.Get("app").(*App)
)
out, err := app.core.GetDashboardCharts()
if err != nil {
return 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, err := app.core.GetDashboardCounts()
if err != nil {
return err
2020-07-05 00:55:02 +08:00
}
return c.JSON(http.StatusOK, okResp{out})
}
// handleReloadApp restarts the app.
func handleReloadApp(c echo.Context) error {
app := c.Get("app").(*App)
go func() {
<-time.After(time.Millisecond * 500)
app.chReload <- syscall.SIGHUP
}()
return c.JSON(http.StatusOK, okResp{true})
}