forked from WEB-IMB-WS2526/lab-development-imb
Aufgabenblatt 8 Lösungen
parent
b48789e805
commit
773145a856
|
|
@ -1,10 +1,26 @@
|
|||
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) {
|
||||
|
|
@ -14,10 +30,11 @@ func workshopHandler(w http.ResponseWriter, r *http.Request) {
|
|||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
// ließt den Body des Requests ein und befüllt FormFalue(), sofern keine Fehler auftauchen
|
||||
// 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")
|
||||
|
|
@ -70,12 +87,41 @@ func workshopHandler(w http.ResponseWriter, r *http.Request) {
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
module kekse
|
||||
|
||||
go 1.24.5
|
||||
|
||||
require github.com/google/uuid v1.6.0
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// create Cookie
|
||||
func createHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
//UUID erzeugen
|
||||
id := uuid.NewString()
|
||||
|
||||
// Keks erstellen
|
||||
|
||||
cookie := &http.Cookie{
|
||||
Name: "keks",
|
||||
Value: id,
|
||||
Path: "/",
|
||||
}
|
||||
|
||||
// keks setzten
|
||||
http.SetCookie(w, cookie)
|
||||
|
||||
// Ausgabe Browser
|
||||
fmt.Fprintf(w, "Keks erstellt: %s\n", id)
|
||||
|
||||
}
|
||||
|
||||
// show Cookie, falls vorhanden
|
||||
func showHandler(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("keks")
|
||||
if err != nil {
|
||||
fmt.Fprintln(w, "Kein Cookie vorhanden")
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, "Keks gefunden: %s\n", cookie.Value)
|
||||
|
||||
}
|
||||
|
||||
// delete Cookie
|
||||
func deleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("keks")
|
||||
if err != nil {
|
||||
http.Error(w, "Kein Cookie gefunden, löschen nicht möglich", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
// Maxage - 1 löscht Cookie
|
||||
cookie.MaxAge = -1
|
||||
http.SetCookie(w, cookie)
|
||||
fmt.Fprintln(w, "Cookie gelöscht")
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
// Endpunkte registrieren
|
||||
|
||||
http.HandleFunc("/create-cookie", createHandler)
|
||||
http.HandleFunc("/show-cookie", showHandler)
|
||||
http.HandleFunc("/delete-cookie", deleteHandler)
|
||||
|
||||
fmt.Println("Server läuft auf http://localhost:8080")
|
||||
http.ListenAndServe(":8080", nil)
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,128 @@
|
|||
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)
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
openapi: 3.0.0
|
||||
info:
|
||||
title: Workshop-Anmeldung API
|
||||
version: 1.0.0
|
||||
|
||||
servers:
|
||||
- url: http://localhost:8080
|
||||
|
||||
paths:
|
||||
/registrierung:
|
||||
post:
|
||||
summary: Anmeldung zu einem Workshop (Formular)
|
||||
description: API für Anmeldedaten eines Workshops per HTML-Formular.
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/x-www-form-urlencoded:
|
||||
schema:
|
||||
$ref: '#/components/schemas/WorkshopForm'
|
||||
responses:
|
||||
'200':
|
||||
description: Erfolgreiche Anmeldung (Formulardaten angenommen)
|
||||
'400':
|
||||
description: Ungültige Eingabe (fehlende oder fehlerhafte Formulardaten)
|
||||
|
||||
/registrierung-json:
|
||||
post:
|
||||
summary: Anmeldung zu einem Workshop (JSON)
|
||||
description: API für Anmeldedaten eines Workshops im JSON-Format.
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/WorkshopForm'
|
||||
responses:
|
||||
'200':
|
||||
description: Erfolgreiche Anmeldung (JSON-Daten zurückgegeben)
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/WorkshopForm'
|
||||
'400':
|
||||
description: Ungültige JSON-Eingabe
|
||||
|
||||
components:
|
||||
schemas:
|
||||
WorkshopForm:
|
||||
type: object
|
||||
properties:
|
||||
vorname:
|
||||
type: string
|
||||
example: "Max"
|
||||
nachname:
|
||||
type: string
|
||||
example: "Mustermann"
|
||||
email:
|
||||
type: string
|
||||
format: email
|
||||
example: "max@beispiel.de"
|
||||
handy:
|
||||
type: string
|
||||
example: "+491751234567"
|
||||
kurs:
|
||||
type: string
|
||||
example: "Webentwicklung Basics"
|
||||
session:
|
||||
type: string
|
||||
enum: ["vormittag", "nachmittag", "abend", "wochenende"]
|
||||
example: "abend"
|
||||
agb:
|
||||
type: string
|
||||
enum: ["ja", "on"]
|
||||
example: "on"
|
||||
newsletter:
|
||||
type: string
|
||||
enum: ["ja", "on"]
|
||||
example: "ja"
|
||||
equipment:
|
||||
type: string
|
||||
enum: ["ja", "on"]
|
||||
example: "ja"
|
||||
format:
|
||||
type: string
|
||||
enum: ["online", "praesenz"]
|
||||
example: "online"
|
||||
required:
|
||||
- vorname
|
||||
- nachname
|
||||
- email
|
||||
- agb
|
||||
- format
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Handler Funktion login
|
||||
func loginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Sie sind eingeloggt")
|
||||
}
|
||||
|
||||
// Handler Funktion Logout
|
||||
func logoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Sie sind ausgeloggt")
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
http.HandleFunc("/login", loginHandler)
|
||||
http.HandleFunc("/logout", logoutHandler)
|
||||
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
Loading…
Reference in New Issue