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"
|
|
|
|
|
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 {
|
|
|
|
RootURL string `json:"rootURL"`
|
2018-11-03 02:27:44 +08:00
|
|
|
UploadURI string `json:"uploadURI"`
|
2018-11-03 02:03:00 +08:00
|
|
|
FromEmail string `json:"fromEmail"`
|
|
|
|
Messengers []string `json:"messengers"`
|
|
|
|
}
|
|
|
|
|
2018-11-05 13:49:08 +08:00
|
|
|
type dashboardStats struct {
|
|
|
|
Stats types.JSONText `db:"stats"`
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
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-03-08 02:33:22 +08:00
|
|
|
RootURL: app.constants.RootURL,
|
|
|
|
FromEmail: app.constants.FromEmail,
|
|
|
|
Messengers: app.manager.GetMessengerNames(),
|
2018-11-03 02:03:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
b = bytes.Buffer{}
|
|
|
|
j = json.NewEncoder(&b)
|
|
|
|
)
|
|
|
|
|
|
|
|
b.Write([]byte(`var CONFIG = `))
|
2019-10-25 13:41:47 +08:00
|
|
|
_ = j.Encode(out)
|
2018-11-03 02:03:00 +08:00
|
|
|
return c.Blob(http.StatusOK, "application/javascript", b.Bytes())
|
|
|
|
}
|
2018-11-05 13:49:08 +08:00
|
|
|
|
|
|
|
// handleGetDashboardStats returns general states for the dashboard.
|
|
|
|
func handleGetDashboardStats(c echo.Context) error {
|
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
|
|
|
out dashboardStats
|
|
|
|
)
|
|
|
|
|
2020-03-08 02:33:22 +08:00
|
|
|
if err := app.queries.GetDashboardStats.Get(&out); err != nil {
|
2018-11-05 13:49:08 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error fetching dashboard stats: %s", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{out.Stats})
|
|
|
|
}
|