forked from WEB-IMB-WS2526/lab-development-imb
129 lines
3.3 KiB
Go
129 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// Struct Unmarshal, Felder müssen exportiert sein (Großbuchstabe) und opt. mit json Tag (8.2)
|
|
|
|
type WorkshopForm struct {
|
|
Vorname string `json:"vorname"`
|
|
Nachname string `json:"nachname"`
|
|
Email string `json:"email"`
|
|
Handy string `json:"handy"`
|
|
Kurs string `json:"kurs"`
|
|
Session string `json:"session"`
|
|
Agb string `json:"agb"`
|
|
Newsletter string `json:"newsletter"`
|
|
Equipment string `json:"equipment"`
|
|
Format string `json:"format"`
|
|
}
|
|
|
|
// Handler Funktion anlegen
|
|
// w = Antwort an Browser, r=Anfrage vom Client
|
|
func workshopHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Prüfen auf POST, Fehler falls andere Methode
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
// ließt den Body des Requests ein und befüllt FormValue(), sofern keine Fehler auftauchen
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
http.Error(w, "Fehler beim Einlesen des Formulars", http.StatusBadRequest)
|
|
return
|
|
}
|
|
// Wenn Felder die als required markiert wurden leer sind: Fehlermeldung
|
|
vorname := r.FormValue("vorname")
|
|
if vorname == "" {
|
|
http.Error(w, "Vorname fehlt", http.StatusBadRequest)
|
|
return
|
|
}
|
|
nachname := r.FormValue("nachname")
|
|
if nachname == "" {
|
|
http.Error(w, "Nachname fehlt", http.StatusBadRequest)
|
|
return
|
|
}
|
|
email := r.FormValue("email")
|
|
if email == "" {
|
|
http.Error(w, "E-Mail fehlt", http.StatusBadRequest)
|
|
return
|
|
}
|
|
handy := r.FormValue("handy")
|
|
|
|
// Checkboxen auslesen
|
|
|
|
agb := r.FormValue("agb")
|
|
if agb == "" {
|
|
http.Error(w, "AGBs müssen akzeptiert werden", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// newsletter und equipment beliben leer, wenn nicht angehakt
|
|
newsletter := r.FormValue("newsletter")
|
|
|
|
equipment := r.FormValue("equipment")
|
|
|
|
// Radio Buttons
|
|
|
|
format := r.FormValue("format")
|
|
if format == "" {
|
|
http.Error(w, "Wählen sie ein Kursformat", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Dropdown
|
|
|
|
session := r.FormValue("session")
|
|
if session == "" {
|
|
http.Error(w, "Wählen sie eine Zeit aus", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
fmt.Println("Vorname: ", vorname)
|
|
fmt.Println("Nachname: ", nachname)
|
|
fmt.Println("E-Mail: : ", email)
|
|
fmt.Println("Telefon: ", handy)
|
|
fmt.Println("Newsletter:", newsletter)
|
|
fmt.Println("Equipment:", equipment)
|
|
fmt.Println("Format:", format)
|
|
fmt.Println("Session:", session)
|
|
|
|
fmt.Fprintln(w, "POST Request funktioniert")
|
|
}
|
|
|
|
// Erweiterung um Json Daten zu empfangen (Aufgabe 8.2)
|
|
func jsonHandler(w http.ResponseWriter, r *http.Request) {
|
|
//Nur POST
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
// lesen und unmarshal
|
|
// Variable für die Daten aus der Form
|
|
var workshopForm WorkshopForm
|
|
|
|
// Prüfung auf gültiges json, falls Fehler 400
|
|
err := json.NewDecoder(r.Body).Decode(&workshopForm)
|
|
if err != nil {
|
|
http.Error(w, "Json ungültig", http.StatusBadRequest)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(workshopForm)
|
|
|
|
}
|
|
|
|
func main() {
|
|
// Anfrage an Pfad in HTML /registrierung, worshop Handler wird aufgerufen
|
|
http.HandleFunc("/registrierung", workshopHandler)
|
|
http.HandleFunc("/registrierung-json", jsonHandler)
|
|
|
|
fmt.Println("Server läuft auf http://localhost:8080")
|
|
http.ListenAndServe(":8080", nil)
|
|
}
|