08: Lösungen

main
Teena Steger 2026-05-19 09:40:24 +02:00
parent 5cbe3c0553
commit 87ff1d8cf7
7 changed files with 155 additions and 0 deletions

View File

@ -0,0 +1,5 @@
module uebung03/cookies
go 1.24.5
require github.com/google/uuid v1.6.0

View File

@ -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=

View File

@ -0,0 +1,46 @@
package main
import (
"fmt"
"log"
"net/http"
"github.com/google/uuid"
)
func createCookie(w http.ResponseWriter, r *http.Request) {
uuid := uuid.New().String()
cookie := &http.Cookie{
Name: "keks",
Value: uuid,
}
http.SetCookie(w, cookie)
fmt.Fprintf(w, "Cookie gesetzt: keks = %s\n", uuid)
}
func showCookie(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("keks")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(w, "Cookie gefunden: keks = %s\n", cookie.Value)
}
func deleteCookie(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("keks")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
cookie.MaxAge = -1
http.SetCookie(w, cookie)
fmt.Fprintf(w, "Cookie gelöscht.\n")
}
func main() {
http.HandleFunc("/create-cookie", createCookie)
http.HandleFunc("/show-cookie", showCookie)
http.HandleFunc("/delete-cookie", deleteCookie)
log.Fatal(http.ListenAndServe("localhost:8080", nil))
}

View File

@ -0,0 +1,5 @@
module uebung04/sessions
go 1.24.5
require github.com/google/uuid v1.6.0

View File

@ -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=

View File

@ -0,0 +1,75 @@
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"github.com/google/uuid"
)
type UserData struct {
Username string `json:"username"`
Nickname string `json:"nickname"`
Admin bool `json:"admin"`
}
var (
sessions = make(map[string]UserData)
mu sync.Mutex
)
func checkSessionID(r *http.Request) (string, error) {
cookie, err := r.Cookie("session_id")
if err != nil {
return "", errors.New("keine session-ID")
}
return cookie.Value, nil
}
func signup(w http.ResponseWriter, r *http.Request) {
var user UserData
err := json.NewDecoder(r.Body).Decode(&user)
if err != nil || user.Nickname == "" || user.Username == "" {
http.Error(w, "Daten konnten nicht verarbeitet werden.", http.StatusBadRequest)
return
}
newID := uuid.New().String()
newCookie := http.Cookie{
Name: "session_id",
Value: newID,
}
http.SetCookie(w, &newCookie)
mu.Lock()
sessions[newID] = user
mu.Unlock()
}
func whoami(w http.ResponseWriter, r *http.Request) {
sessionID, err := checkSessionID(r)
if err != nil {
fmt.Fprintf(w, "Du bist noch niemand.")
} else {
mu.Lock()
user, ok := sessions[sessionID]
mu.Unlock()
if ok {
fmt.Fprintf(w, "Hallo %s!\n", user.Nickname)
if user.Admin {
fmt.Fprintln(w, "Du bist als Admin registriert.")
} else {
fmt.Fprintln(w, "Du bist nicht als Admin registriert.")
}
}
}
}
func main() {
http.HandleFunc("/signup", signup)
http.HandleFunc("/whoami", whoami)
http.ListenAndServe("localhost:8080", nil)
}

View File

@ -0,0 +1,20 @@
package main
import (
"fmt"
"net/http"
)
func loginFunc(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Sie wurden erfolgreich eingeloggt.")
}
func logoutFunc(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Sie wurden erfolgreich ausgeloggt")
}
func main() {
http.HandleFunc("/login", loginFunc)
http.HandleFunc("/logout", logoutFunc)
http.ListenAndServe("localhost:8080", nil)
}