2024-08-13 16:38:02 +02:00
|
|
|
package main
|
|
|
|
|
2024-08-15 13:31:15 +02:00
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
)
|
2024-08-13 16:38:02 +02:00
|
|
|
|
|
|
|
type UserSettings struct {
|
|
|
|
Theme string
|
|
|
|
Language string
|
|
|
|
SafeSearch string
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadUserSettings(r *http.Request) UserSettings {
|
|
|
|
var settings UserSettings
|
|
|
|
|
|
|
|
// Load theme
|
|
|
|
if cookie, err := r.Cookie("theme"); err == nil {
|
|
|
|
settings.Theme = cookie.Value
|
|
|
|
} else {
|
|
|
|
settings.Theme = "dark" // Default theme
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load language
|
|
|
|
if cookie, err := r.Cookie("language"); err == nil {
|
|
|
|
settings.Language = cookie.Value
|
|
|
|
} else {
|
|
|
|
settings.Language = "en" // Default language
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load safe search
|
|
|
|
if cookie, err := r.Cookie("safe"); err == nil {
|
|
|
|
settings.SafeSearch = cookie.Value
|
|
|
|
} else {
|
|
|
|
settings.SafeSearch = "" // Default safe search off
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveUserSettings(w http.ResponseWriter, settings UserSettings) {
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "theme",
|
|
|
|
Value: settings.Theme,
|
|
|
|
Path: "/",
|
|
|
|
Secure: true, // Ensure cookie is sent over HTTPS only
|
|
|
|
SameSite: http.SameSiteNoneMode, // Set SameSite to None
|
|
|
|
})
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "language",
|
|
|
|
Value: settings.Language,
|
|
|
|
Path: "/",
|
|
|
|
Secure: true, // Ensure cookie is sent over HTTPS only
|
|
|
|
SameSite: http.SameSiteNoneMode, // Set SameSite to None
|
|
|
|
})
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "safe",
|
|
|
|
Value: settings.SafeSearch,
|
|
|
|
Path: "/",
|
|
|
|
Secure: true, // Ensure cookie is sent over HTTPS only
|
|
|
|
SameSite: http.SameSiteNoneMode, // Set SameSite to None
|
|
|
|
})
|
2024-08-15 13:31:15 +02:00
|
|
|
|
|
|
|
printDebug("settings saved: %v", settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSaveSettings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "POST" {
|
|
|
|
// Load current settings
|
|
|
|
settings := loadUserSettings(r)
|
|
|
|
|
|
|
|
// Update only the settings that were submitted in the form
|
|
|
|
if theme := r.FormValue("theme"); theme != "" {
|
|
|
|
settings.Theme = theme
|
|
|
|
}
|
|
|
|
if lang := r.FormValue("lang"); lang != "" {
|
|
|
|
settings.Language = lang
|
|
|
|
}
|
|
|
|
if safe := r.FormValue("safe"); safe != "" {
|
|
|
|
settings.SafeSearch = safe
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the updated settings
|
|
|
|
saveUserSettings(w, settings)
|
|
|
|
|
|
|
|
// Redirect back to the previous page or settings page
|
|
|
|
http.Redirect(w, r, r.FormValue("past"), http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSettings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Load user settings
|
|
|
|
settings = loadUserSettings(r)
|
|
|
|
|
|
|
|
data := struct {
|
|
|
|
LanguageOptions []LanguageOption
|
|
|
|
CurrentLang string
|
|
|
|
Theme string
|
|
|
|
Safe string
|
|
|
|
}{
|
|
|
|
LanguageOptions: languageOptions,
|
|
|
|
CurrentLang: settings.Language,
|
|
|
|
Theme: settings.Theme,
|
|
|
|
Safe: settings.SafeSearch,
|
|
|
|
}
|
|
|
|
|
|
|
|
printDebug("Rendering settings with data: %+v", data)
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles("templates/settings.html")
|
|
|
|
if err != nil {
|
|
|
|
printErr("Error parsing template: %s", err)
|
|
|
|
http.Error(w, "Internal Server Error", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tmpl.Execute(w, data)
|
|
|
|
if err != nil {
|
|
|
|
printErr("Error executing template: %s", err)
|
|
|
|
http.Error(w, "Internal Server Error", 500)
|
|
|
|
return
|
|
|
|
}
|
2024-08-13 16:38:02 +02:00
|
|
|
}
|