2018-10-25 13:51:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-11-02 18:03:00 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-11-05 05:49:08 +00:00
|
|
|
"fmt"
|
2018-10-25 13:51:47 +00:00
|
|
|
"net/http"
|
|
|
|
|
2018-11-05 05:49:08 +00:00
|
|
|
"github.com/jmoiron/sqlx/types"
|
2018-10-25 13:51:47 +00:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
2018-11-02 18:03:00 +00:00
|
|
|
type configScript struct {
|
2020-07-05 12:05:05 +00:00
|
|
|
RootURL string `json:"rootURL"`
|
|
|
|
FromEmail string `json:"fromEmail"`
|
|
|
|
Messengers []string `json:"messengers"`
|
|
|
|
MediaProvider string `json:"media_provider"`
|
2018-11-02 18:03:00 +00: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-07-05 12:05:05 +00:00
|
|
|
RootURL: app.constants.RootURL,
|
|
|
|
FromEmail: app.constants.FromEmail,
|
|
|
|
Messengers: app.manager.GetMessengerNames(),
|
|
|
|
MediaProvider: app.constants.MediaProvider,
|
2018-11-02 18:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b = bytes.Buffer{}
|
|
|
|
j = json.NewEncoder(&b)
|
|
|
|
)
|
|
|
|
|
|
|
|
b.Write([]byte(`var CONFIG = `))
|
2019-10-25 05:41:47 +00:00
|
|
|
_ = j.Encode(out)
|
2018-11-02 18:03:00 +00:00
|
|
|
return c.Blob(http.StatusOK, "application/javascript", b.Bytes())
|
|
|
|
}
|
2018-11-05 05:49:08 +00:00
|
|
|
|
2020-07-04 16:55:02 +00:00
|
|
|
// handleGetDashboardCharts returns chart data points to render ont he dashboard.
|
|
|
|
func handleGetDashboardCharts(c echo.Context) error {
|
2018-11-05 05:49:08 +00:00
|
|
|
var (
|
|
|
|
app = c.Get("app").(*App)
|
2020-07-04 16:55:02 +00:00
|
|
|
out types.JSONText
|
2018-11-05 05:49:08 +00:00
|
|
|
)
|
|
|
|
|
2020-07-04 16:55:02 +00:00
|
|
|
if err := app.queries.GetDashboardCharts.Get(&out); err != nil {
|
2018-11-05 05:49:08 +00:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error fetching dashboard stats: %s", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:55:02 +00: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 types.JSONText
|
|
|
|
)
|
|
|
|
|
|
|
|
if err := app.queries.GetDashboardCounts.Get(&out); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError,
|
|
|
|
fmt.Sprintf("Error fetching dashboard statsc counts: %s", pqErrMsg(err)))
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, okResp{out})
|
2018-11-05 05:49:08 +00:00
|
|
|
}
|