package main import ( "fmt" "net/http" "sync" "github.com/google/uuid" ) // Session-Speicher (Map von SessionID -> Merkzettel) var ( sessions = make(map[string][]string) mu sync.Mutex ) // Hilfsfunktion: Session-ID aus Cookie holen oder neue erstellen func getSessionID(w http.ResponseWriter, r *http.Request) string { cookie, err := r.Cookie("session_id") if err != nil { // Neue Session-ID erzeugen (UUID) newID := uuid.New().String() http.SetCookie(w, &http.Cookie{ Name: "session_id", Value: newID, }) mu.Lock() sessions[newID] = []string{} // leeren Merkzettel anlegen mu.Unlock() return newID } return cookie.Value } // Buch zum Merkzettel hinzufügen func addToMerkzettel(w http.ResponseWriter, r *http.Request) { sessionID := getSessionID(w, r) book := r.URL.Query().Get("book") if book == "" { fmt.Fprintln(w, "Bitte Buchtitel angeben: ?book=XYZ") return } mu.Lock() sessions[sessionID] = append(sessions[sessionID], book) mu.Unlock() fmt.Fprintf(w, "Buch '%s' zum Merkzettel hinzugefügt!\n", book) } // Merkzettel anzeigen func showMerkzettel(w http.ResponseWriter, r *http.Request) { sessionID := getSessionID(w, r) mu.Lock() books := sessions[sessionID] mu.Unlock() fmt.Fprintf(w, "Dein Merkzettel: %v\n", books) } func main() { http.HandleFunc("/add", addToMerkzettel) http.HandleFunc("/list", showMerkzettel) fmt.Println("Server läuft auf http://localhost:8080") http.ListenAndServe(":8080", nil) }