forked from WEB-IMB-WS2526/lab-development-imb
47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
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))
|
|
}
|