2024-03-12 15:49:08 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2024-04-03 12:17:05 +02:00
|
|
|
"net/http"
|
|
|
|
|
2024-09-25 15:33:28 +02:00
|
|
|
"github.com/gorilla/sessions"
|
2024-03-12 15:49:08 +01:00
|
|
|
"gitlab.unjx.de/flohoss/godash/internal/env"
|
|
|
|
"gitlab.unjx.de/flohoss/godash/services"
|
|
|
|
"gitlab.unjx.de/flohoss/godash/views/home"
|
|
|
|
)
|
|
|
|
|
|
|
|
type BookmarkService interface {
|
|
|
|
GetAllBookmarks() *services.Bookmarks
|
|
|
|
}
|
|
|
|
|
|
|
|
type SystemService interface {
|
|
|
|
GetLiveInformation() *services.LiveInformation
|
|
|
|
GetStaticInformation() *services.StaticInformation
|
|
|
|
}
|
|
|
|
|
2024-03-15 18:56:32 +01:00
|
|
|
type WeatherService interface {
|
|
|
|
GetCurrentWeather() *services.OpenWeather
|
|
|
|
}
|
|
|
|
|
2024-09-25 15:33:28 +02:00
|
|
|
func NewAppHandler(env *env.Config, store *sessions.CookieStore, s SystemService, w WeatherService, b BookmarkService) *AppHandler {
|
2024-03-12 15:49:08 +01:00
|
|
|
return &AppHandler{
|
|
|
|
env: env,
|
2024-09-25 15:33:28 +02:00
|
|
|
store: store,
|
2024-03-25 20:31:51 +01:00
|
|
|
systemService: s,
|
|
|
|
weatherService: w,
|
|
|
|
bookmarkService: b,
|
2024-03-12 15:49:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type AppHandler struct {
|
|
|
|
env *env.Config
|
2024-09-25 15:33:28 +02:00
|
|
|
store *sessions.CookieStore
|
2024-03-25 20:31:51 +01:00
|
|
|
systemService SystemService
|
|
|
|
weatherService WeatherService
|
|
|
|
bookmarkService BookmarkService
|
2024-03-12 15:49:08 +01:00
|
|
|
}
|
|
|
|
|
2024-04-03 12:17:05 +02:00
|
|
|
func (bh *AppHandler) appHandler(w http.ResponseWriter, r *http.Request) {
|
2024-03-25 20:31:51 +01:00
|
|
|
bookmarks := bh.bookmarkService.GetAllBookmarks()
|
|
|
|
staticSystem := bh.systemService.GetStaticInformation()
|
|
|
|
liveSystem := bh.systemService.GetLiveInformation()
|
|
|
|
weather := bh.weatherService.GetCurrentWeather()
|
2024-03-26 10:25:19 +01:00
|
|
|
|
2024-03-12 15:49:08 +01:00
|
|
|
titlePage := bh.env.Title
|
|
|
|
|
2024-09-25 15:33:28 +02:00
|
|
|
session, _ := bh.store.Get(r, StoreSessionKey)
|
|
|
|
user := &services.User{
|
|
|
|
Name: session.Values[string(NameKey)].(string),
|
|
|
|
Email: session.Values[string(EmailKey)].(string),
|
|
|
|
Gravatar: session.Values[string(GravatarKey)].(string),
|
2024-09-16 11:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
home.HomeIndex(titlePage, bh.env.Version, home.Home(titlePage, user, bookmarks, staticSystem, liveSystem, weather)).Render(r.Context(), w)
|
2024-03-12 15:49:08 +01:00
|
|
|
}
|