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