phoenix/views/pages/index.go

46 lines
1,002 B
Go
Raw Permalink Normal View History

package pages
import (
"net/http"
"time"
"github.com/ordinary-dev/phoenix/database"
)
func ShowMainPage(w http.ResponseWriter, r *http.Request) {
groups, err := database.GetGroupsWithLinks()
if err != nil {
ShowError(w, http.StatusInternalServerError, err)
return
}
// 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-04-01 23:55:43 +05:00
Render("index.html.tmpl", w, map[string]any{
"description": "Self-hosted start page.",
"groups": groups,
"style": style,
})
}