2024-03-25 15:52:18 +05:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-04-01 22:38:23 +05:00
|
|
|
"time"
|
2024-03-25 15:52:18 +05:00
|
|
|
|
|
|
|
"github.com/ordinary-dev/phoenix/database"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2024-04-01 22:38:23 +05:00
|
|
|
func ShowMainPage(w http.ResponseWriter, r *http.Request) {
|
2024-03-26 00:40:52 +05:00
|
|
|
groups, err := database.GetGroupsWithLinks()
|
|
|
|
if err != nil {
|
|
|
|
ShowError(w, http.StatusInternalServerError, err)
|
2024-03-25 15:52:18 +05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-01 22:38:23 +05:00
|
|
|
// Get desired style.
|
|
|
|
style := r.FormValue("style")
|
|
|
|
|
|
|
|
if style == "tiles" || style == "list" {
|
|
|
|
// If a valid style is specified in the url -
|
|
|
|
// save the value in a cookie.
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "phoenix-style",
|
|
|
|
Value: style,
|
|
|
|
Expires: time.Now().Add(time.Hour * 24 * 30 * 12 * 10),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// The style is not specified or has an incorrect type, check the cookies.
|
|
|
|
styleCookie, err := r.Cookie("phoenix-style")
|
|
|
|
if err == nil {
|
|
|
|
style = styleCookie.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if style != "tiles" && style != "list" {
|
|
|
|
style = "list"
|
|
|
|
}
|
|
|
|
|
2024-03-26 00:40:52 +05:00
|
|
|
err = Render("index.html.tmpl", w, map[string]any{
|
2024-03-25 15:52:18 +05:00
|
|
|
"description": "Self-hosted start page.",
|
|
|
|
"groups": groups,
|
2024-04-01 22:38:23 +05:00
|
|
|
"style": style,
|
2024-03-25 15:52:18 +05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}
|