forked from knadh/listmonk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
58 lines (48 loc) · 1.37 KB
/
admin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/jmoiron/sqlx/types"
"github.com/labstack/echo"
)
type configScript struct {
RootURL string `json:"rootURL"`
UploadURI string `json:"uploadURI"`
FromEmail string `json:"fromEmail"`
Messengers []string `json:"messengers"`
}
type dashboardStats struct {
Stats types.JSONText `db:"stats"`
}
// 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{
RootURL: app.Constants.RootURL,
UploadURI: app.Constants.UploadURI,
FromEmail: app.Constants.FromEmail,
Messengers: app.Manager.GetMessengerNames(),
}
b = bytes.Buffer{}
j = json.NewEncoder(&b)
)
b.Write([]byte(`var CONFIG = `))
j.Encode(out)
return c.Blob(http.StatusOK, "application/javascript", b.Bytes())
}
// handleGetDashboardStats returns general states for the dashboard.
func handleGetDashboardStats(c echo.Context) error {
var (
app = c.Get("app").(*App)
out dashboardStats
)
if err := app.Queries.GetDashboardStats.Get(&out); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
fmt.Sprintf("Error fetching dashboard stats: %s", pqErrMsg(err)))
}
return c.JSON(http.StatusOK, okResp{out.Stats})
}