2020-05-24 14:41:19 +03:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2022-08-13 11:28:36 +03:00
|
|
|
"encoding/json"
|
2020-05-24 17:34:32 +03:00
|
|
|
"fmt"
|
2022-10-16 18:42:32 +03:00
|
|
|
"github.com/emvi/logbuch"
|
2023-03-03 22:40:50 +03:00
|
|
|
"github.com/go-chi/chi/v5"
|
2020-05-24 14:41:19 +03:00
|
|
|
"github.com/gorilla/schema"
|
2020-10-09 22:37:16 +03:00
|
|
|
conf "github.com/muety/wakapi/config"
|
2020-05-24 14:41:19 +03:00
|
|
|
"github.com/muety/wakapi/models"
|
2020-11-06 23:19:54 +03:00
|
|
|
"github.com/muety/wakapi/models/view"
|
2023-01-02 20:05:28 +03:00
|
|
|
routeutils "github.com/muety/wakapi/routes/utils"
|
2021-01-17 11:24:09 +03:00
|
|
|
"github.com/muety/wakapi/services"
|
2020-05-24 14:41:19 +03:00
|
|
|
"net/http"
|
2021-01-17 11:24:09 +03:00
|
|
|
"strconv"
|
2022-08-13 11:28:36 +03:00
|
|
|
"strings"
|
2021-01-17 11:24:09 +03:00
|
|
|
"time"
|
2020-05-24 14:41:19 +03:00
|
|
|
)
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
type HomeHandler struct {
|
2021-01-17 11:24:09 +03:00
|
|
|
config *conf.Config
|
|
|
|
keyValueSrvc services.IKeyValueService
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var loginDecoder = schema.NewDecoder()
|
2020-05-24 17:34:32 +03:00
|
|
|
var signupDecoder = schema.NewDecoder()
|
2021-04-05 23:57:57 +03:00
|
|
|
var resetPasswordDecoder = schema.NewDecoder()
|
2020-05-24 14:41:19 +03:00
|
|
|
|
2021-01-17 11:24:09 +03:00
|
|
|
func NewHomeHandler(keyValueService services.IKeyValueService) *HomeHandler {
|
2020-11-06 23:19:54 +03:00
|
|
|
return &HomeHandler{
|
2021-01-17 11:24:09 +03:00
|
|
|
config: conf.Get(),
|
|
|
|
keyValueSrvc: keyValueService,
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 22:40:50 +03:00
|
|
|
func (h *HomeHandler) RegisterRoutes(router chi.Router) {
|
|
|
|
router.Get("/", h.GetIndex)
|
2021-01-30 12:34:52 +03:00
|
|
|
}
|
|
|
|
|
2020-11-06 23:19:54 +03:00
|
|
|
func (h *HomeHandler) GetIndex(w http.ResponseWriter, r *http.Request) {
|
2020-05-24 14:41:19 +03:00
|
|
|
if h.config.IsDev() {
|
|
|
|
loadTemplates()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" {
|
2020-10-04 11:37:38 +03:00
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.Server.BasePath), http.StatusFound)
|
2020-05-24 14:41:19 +03:00
|
|
|
return
|
|
|
|
}
|
2023-03-04 11:33:36 +03:00
|
|
|
if h.config.Security.DisableFrontpage {
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("%s/login", h.config.Server.BasePath), http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
2020-05-24 14:41:19 +03:00
|
|
|
|
2023-01-02 20:05:28 +03:00
|
|
|
templates[conf.IndexTemplate].Execute(w, h.buildViewModel(r, w))
|
2020-05-24 14:41:19 +03:00
|
|
|
}
|
|
|
|
|
2023-01-02 20:05:28 +03:00
|
|
|
func (h *HomeHandler) buildViewModel(r *http.Request, w http.ResponseWriter) *view.HomeViewModel {
|
2021-01-17 11:24:09 +03:00
|
|
|
var totalHours int
|
|
|
|
var totalUsers int
|
2022-08-13 11:28:36 +03:00
|
|
|
var newsbox view.Newsbox
|
2021-01-17 11:24:09 +03:00
|
|
|
|
2022-08-13 11:28:36 +03:00
|
|
|
if kv, err := h.keyValueSrvc.GetString(conf.KeyLatestTotalTime); err == nil && kv != nil && kv.Value != "" {
|
|
|
|
if d, err := time.ParseDuration(kv.Value); err == nil {
|
2021-01-17 11:24:09 +03:00
|
|
|
totalHours = int(d.Hours())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 11:28:36 +03:00
|
|
|
if kv, err := h.keyValueSrvc.GetString(conf.KeyLatestTotalUsers); err == nil && kv != nil && kv.Value != "" {
|
|
|
|
if d, err := strconv.Atoi(kv.Value); err == nil {
|
2021-01-17 11:24:09 +03:00
|
|
|
totalUsers = d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-13 11:28:36 +03:00
|
|
|
if kv, err := h.keyValueSrvc.GetString(conf.KeyNewsbox); err == nil && kv != nil && kv.Value != "" {
|
2022-10-16 18:42:32 +03:00
|
|
|
if err := json.NewDecoder(strings.NewReader(kv.Value)).Decode(&newsbox); err != nil {
|
|
|
|
logbuch.Error("failed to decode newsbox message - %v", err)
|
|
|
|
}
|
2022-08-13 11:28:36 +03:00
|
|
|
}
|
|
|
|
|
2023-01-02 20:05:28 +03:00
|
|
|
vm := &view.HomeViewModel{
|
2021-01-17 11:24:09 +03:00
|
|
|
TotalHours: totalHours,
|
|
|
|
TotalUsers: totalUsers,
|
2022-08-13 11:28:36 +03:00
|
|
|
Newsbox: &newsbox,
|
2020-11-06 23:19:54 +03:00
|
|
|
}
|
2023-01-02 20:05:28 +03:00
|
|
|
return routeutils.WithSessionMessages(vm, r, w)
|
2020-11-06 23:19:54 +03:00
|
|
|
}
|