package main import ( "errors" "fmt" "log" "net/http" ) type workshopHandler int func parseCheckboxValue(value string) string { if value == "" || (value != "ja" && value != "on") { return "nein" } return "ja" } func parseRadiobuttonValue(value string) (string, error) { if value == "" || (value != "praesenz" && value != "online") { return "", errors.New("Auswahl nicht erlaubt.") } return value, nil } func (worksh workshopHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Nur POST-Anfragen erlaubt", http.StatusMethodNotAllowed) return } // Formulardaten parsen und Überprüfen, ob ungültige Formulardaten gesendet wurde if err := r.ParseForm(); err != nil { http.Error(w, "Fehler beim Parsen des Formulars", http.StatusBadRequest) return } // Überprüfen, ob leeres Formular gesendet wurde if len(r.PostForm) == 0 { http.Error(w, "Kein Formular gesendet", http.StatusBadRequest) return } // Formulardaten auslesen und ggf. überprüfen vorname := r.PostForm.Get("vorname") if vorname == "" { http.Error(w, "Pflichtfeld fehlt.", http.StatusBadRequest) return } nachname := r.PostForm.Get("nachname") if nachname == "" { http.Error(w, "Pflichtfeld fehlt.", http.StatusBadRequest) return } email := r.PostForm.Get("email") telefon := r.PostForm.Get("telefon") sessions := r.PostForm["sessions"] agb := parseCheckboxValue(r.PostForm.Get("agb")) if agb == "nein" { http.Error(w, "AGB wurde nicht akzeptiert.", http.StatusBadRequest) return } newsletter := parseCheckboxValue(r.PostForm.Get("newsletter")) equipment := parseCheckboxValue(r.PostForm.Get("equipment")) format, err := parseRadiobuttonValue(r.PostForm.Get("format")) if err != nil { http.Error(w, "Ungültige Auswahl des Formats.", http.StatusBadRequest) return } // Ausgabe im Terminal fmt.Fprintf(w, "Neue Registrierung erhalten:\n") fmt.Fprintf(w, "Vorname: %s\n", vorname) fmt.Fprintf(w, "Nachname: %s\n", nachname) fmt.Fprintf(w, "E-Mail: %s\n", email) fmt.Fprintf(w, "Telefon: %s\n", telefon) fmt.Fprintf(w, "Bevorzugte Sessions: \n") for _, session := range sessions { fmt.Fprintf(w, " - %s\n", session) } fmt.Fprintf(w, "AGB: %s\n", agb) fmt.Fprintf(w, "Newsletter: %s\n", newsletter) fmt.Fprintf(w, "Equipment benötigt: %s\n", equipment) fmt.Fprintf(w, "Teilnahmeformat: %s", format) } func main() { var workshop workshopHandler fmt.Println("Server läuft auf localhost:8080...") log.Fatal(http.ListenAndServe(":8080", workshop)) }