forked from WEB-IB-SS26/development-ib
08: Demos und Labor
parent
a26dd68636
commit
7aaf855f4e
|
|
@ -0,0 +1,21 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="de">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Bücher-Merkliste</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Eigenen Merkzettel anlegen</h1>
|
||||
<form action="http://localhost:8080/add" method="GET">
|
||||
<label for="buchname">Titel des Buchs:</label>
|
||||
<input type="text" id="buchname" name="book">
|
||||
<button type="submit">Merken</button>
|
||||
</form>
|
||||
<br>
|
||||
<a href="http://localhost:8080/list">Gehe zu Merkzettel</a>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
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)
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
module demo/sessions
|
||||
|
||||
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,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type appleHandler int
|
||||
type bananaHandler string
|
||||
|
||||
func (a appleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Apple 🍎")
|
||||
}
|
||||
|
||||
func (b bananaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Banana 🍌")
|
||||
}
|
||||
|
||||
func main() {
|
||||
var a appleHandler
|
||||
var b bananaHandler
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/apple", a)
|
||||
mux.Handle("/banana", b)
|
||||
http.ListenAndServe("localhost:8080", mux)
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type appleDefaultHandler int
|
||||
type bananaDefaultHandler string
|
||||
|
||||
func (a appleDefaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Apple")
|
||||
}
|
||||
|
||||
func (b bananaDefaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "Banana")
|
||||
}
|
||||
|
||||
func main() {
|
||||
var a appleDefaultHandler
|
||||
var b bananaDefaultHandler
|
||||
http.Handle("/apple", a)
|
||||
http.Handle("/banana", b)
|
||||
http.ListenAndServe("localhost:8080", nil)
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func appleFunc(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "🍎 Apple func antwortet")
|
||||
}
|
||||
|
||||
func bananaFunc(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "🍌 Banana func antwortet")
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/apple", appleFunc)
|
||||
http.HandleFunc("/banana", bananaFunc)
|
||||
http.ListenAndServe("localhost:8080", nil)
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Firstname string `json:"firstname"`
|
||||
Lastname string `json:"lastname"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
|
||||
func encode(w http.ResponseWriter, r *http.Request) {
|
||||
mariam := User{
|
||||
Firstname: "Mariam",
|
||||
Lastname: "Okonkwo",
|
||||
Age: 25,
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(mariam)
|
||||
}
|
||||
|
||||
func decode(w http.ResponseWriter, r *http.Request) {
|
||||
var user User
|
||||
json.NewDecoder(r.Body).Decode(&user)
|
||||
fmt.Fprintf(w, "%s %s is %d years old!", user.Firstname, user.Lastname, user.Age)
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/encode", encode)
|
||||
http.HandleFunc("/decode", decode)
|
||||
http.ListenAndServe("localhost:8080", nil)
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func start(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "<a href=\"/set\">Setze Cookie</a>")
|
||||
fmt.Fprintln(w, "<a href=\"/get\">Zeige Cookie</a>")
|
||||
fmt.Fprintln(w, "<a href=\"/get\">Lösche Cookie</a>")
|
||||
}
|
||||
|
||||
func setCookie(w http.ResponseWriter, r *http.Request) {
|
||||
c := &http.Cookie{Name: "lang", Value: "de"}
|
||||
http.SetCookie(w, c)
|
||||
fmt.Fprintln(w, "<p>Cookie gesetzt. Überprüfe in Dev-Tools oder <a href=\"/get\">hier anzeigen</a> oder <a href=\"/clear\">hier löschen</a></p>")
|
||||
}
|
||||
|
||||
func getCookie(w http.ResponseWriter, r *http.Request) {
|
||||
c, err := r.Cookie("lang")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Fprintln(w, "Cookie: lang = ", c.Value)
|
||||
}
|
||||
|
||||
func clearCookie(w http.ResponseWriter, r *http.Request) {
|
||||
c, err := r.Cookie("lang")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
c.MaxAge = -1
|
||||
http.SetCookie(w, c)
|
||||
fmt.Fprintln(w, "Cookie gelöscht: lang")
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", start)
|
||||
http.HandleFunc("/set", setCookie)
|
||||
http.HandleFunc("/get", getCookie)
|
||||
http.HandleFunc("/clear", clearCookie)
|
||||
|
||||
fmt.Println("Server läuft auf http://localhost:8080")
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
# Übungsblatt 08
|
||||
|
||||
## 1. Go-Übung: Routing
|
||||
|
||||
**Aufgabenstellung**: Erstellen Sie ein einfaches Webserver-Programm in Go, das zwei HTTP-Endpunkte `/login` und `/logout` bereitstellt. Beim Aufruf dieser Pfade soll jeweils eine passende Textnachricht im Browser erscheinen. Verwenden Sie dazu die Standardbibliothek `net/http` und definieren Sie eigene Funktionen zur Übergabe an `HandleFunc`.
|
||||
|
||||
|
||||
## 2. Go-Übung: Cookies
|
||||
|
||||
**Aufgabenstellung**: Erstellen Sie ein einfaches Webserver-Programm in Go, das drei HTTP-Endpunkte `/create-cookie`, `/show-cookie` und `/delete-cookie` bereitstellt. Beim Aufruf dieser Pfade soll entweder ein neuer Cookie mit dem Namen `keks` und einer generierten UUID als Wert erstellt, der Wert im Browser ausgegeben oder der Cookie ganz gelöscht werden. Außerdem soll jeweils eine passende Textnachricht im Browser erscheinen.
|
||||
|
||||
## 3. Go-Übung: Sessions
|
||||
|
||||
**Aufgabenstellung**: Erstellen Sie ein Webserver-Programm in Go, das die API-Spezifikation [nickname.json](nickname.json) erfüllt.
|
||||
|
||||
#### Hinweis zu 2. und 3.
|
||||
Um externe Bibliotheken (z.B. `github.com/google/uuid` zur Erstellung von UUIDs) verwenden zu können, müssen Sie zunächst ein Modul definieren und die Abhängigkeiten darüber verwalten. Eine kurze Anleitung dazu finden Sie hier: [Abhängigkeiten mit go.mod](anleitung_extlib.md)
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
# Abhängigkeitsverwaltung mit `go.mod`
|
||||
|
||||
## Hintergrund
|
||||
|
||||
Externe Pakete, die im Code importiert werden, werden über eine `go.mod`‑Datei verwaltet. Diese Datei definiert das Modul und listet alle Abhängigkeiten auf. Sie gehört fest zum Projekt und wird im Quellcode‑Repository mitgeführt.
|
||||
|
||||
## Anweisungen
|
||||
|
||||
*Annahme*: Go-Programm ist bereits vorhanden und die Imports im Code sind gesetzt. Außerdem befinden Sie sich in dem Ordner mit dem Go-Programm.
|
||||
|
||||
1. **Modul initialisieren (falls noch nicht geschehen)**
|
||||
```bash
|
||||
go mod init example.com/myapp
|
||||
```
|
||||
Der Modulpfad entspricht in der Praxis meist der Repository‑Adresse, z. B. `github.com/mymodule`. Für einfache Beispiele genügt ein Platzhalter wie z.B. `example.com/myapp`
|
||||
|
||||
2. **Abhängigkeiten auflösen und aufräumen**
|
||||
```bash
|
||||
go mod tidy
|
||||
```
|
||||
Dadurch werden alle im Code verwendeten Imports heruntergeladen und ins `go.mod` eingetragen. Nicht mehr benötigte Pakete werden entfernt.
|
||||
|
||||
3. **Programm starten**
|
||||
```bash
|
||||
go run .
|
||||
```
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
{
|
||||
"openapi": "3.0.0",
|
||||
"info": {
|
||||
"title": "Workshop API",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"servers": [
|
||||
{
|
||||
"url": "localhost:8080"
|
||||
}
|
||||
],
|
||||
"paths": {
|
||||
"/signup": {
|
||||
"post": {
|
||||
"summary": "Erstellt eine Session für eine:n Benutzer:in.",
|
||||
"requestBody": {
|
||||
"required": true,
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/UserData"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Success",
|
||||
"content": {
|
||||
"text/html": {
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request",
|
||||
"content": {
|
||||
"text/html": {
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"example": "Daten konnten nicht verarbeitet werden."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/whoami": {
|
||||
"get": {
|
||||
"summary": "Liefert eine Nachricht an eine:n Benutzer:in zurück.",
|
||||
"description": "Falls der/die Benutzer:in sich über einen signup-Request bereits registriert hat, erhält er/sie eine Nachricht unter Nennung des Spitznamens und der Information, ob er/sie als Admin registriert ist.",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Nachricht an registrierte Benutzer:innen",
|
||||
"content": {
|
||||
"text/html": {
|
||||
"schema": {
|
||||
"type": "string"
|
||||
},
|
||||
"example": "Hallo Karla!\nDu bist als Admin registriert."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"UserData": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"username",
|
||||
"nickname"
|
||||
],
|
||||
"properties": {
|
||||
"username": {
|
||||
"type": "string",
|
||||
"example": "fischauge12345"
|
||||
},
|
||||
"nickname": {
|
||||
"type": "string",
|
||||
"example": "Karla"
|
||||
},
|
||||
"admin": {
|
||||
"type": "boolean",
|
||||
"example": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue