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