forked from WEB-IB-SS26/development-ib
implemented labor 08
parent
b815fbc101
commit
9173b0e33a
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type loginHandler int
|
||||
type logoutHandler string
|
||||
|
||||
func (a loginHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("Login"))
|
||||
}
|
||||
|
||||
func (b logoutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("Logout"))
|
||||
}
|
||||
|
||||
func main() {
|
||||
var a loginHandler
|
||||
var b logoutHandler
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/login", a)
|
||||
mux.Handle("/logout", b)
|
||||
http.ListenAndServe("localhost:8080", mux)
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type createHandler int
|
||||
type showHandler int
|
||||
type deleteHandler int
|
||||
|
||||
func (a createHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
uuid := uuid.New().String()
|
||||
cookie := &http.Cookie{Name: "keks", Value: uuid}
|
||||
http.SetCookie(w, cookie)
|
||||
w.Write([]byte("Cookie erstellt!"))
|
||||
}
|
||||
|
||||
func (b showHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("keks")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
w.Write([]byte("Keks Wert: " + cookie.Value))
|
||||
}
|
||||
|
||||
func (b deleteHandler) ServeHTTP(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)
|
||||
w.Write([]byte("Keks gelöscht"))
|
||||
}
|
||||
|
||||
func main() {
|
||||
var a createHandler
|
||||
var b showHandler
|
||||
var c deleteHandler
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/create-cookie", a)
|
||||
mux.Handle("/show-cookie", b)
|
||||
mux.Handle("/delete-cookie", c)
|
||||
http.ListenAndServe("localhost:8080", mux)
|
||||
}
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type signupHandler int
|
||||
type whoAmIHandler int
|
||||
|
||||
type User struct {
|
||||
Username string `json:"username"`
|
||||
Nickname string `json:"nickname"`
|
||||
Admin bool `json:"admin"`
|
||||
}
|
||||
|
||||
var Users map[string]User
|
||||
|
||||
func (a signupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "POST")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
||||
var user User
|
||||
if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
|
||||
http.Error(w, "Daten konnten nicht verarbeitet werden.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
uuid := uuid.New().String()
|
||||
Users[uuid] = user
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Registrierung erfolgreich"))
|
||||
cookie := &http.Cookie{Name: "uuid", Value: uuid}
|
||||
http.SetCookie(w, cookie)
|
||||
}
|
||||
|
||||
func (b whoAmIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, err := r.Cookie("uuid")
|
||||
if err != nil {
|
||||
w.Write([]byte("Keinen Login Cookie gefunden."))
|
||||
return
|
||||
}
|
||||
if user, ok := Users[cookie.Value]; ok {
|
||||
nickname := user.Nickname
|
||||
admin := user.Admin
|
||||
|
||||
var status string
|
||||
if admin {
|
||||
status = "Admin"
|
||||
} else {
|
||||
status = "normaler Benutzer"
|
||||
}
|
||||
|
||||
w.Write([]byte("Hallo " + nickname + "!\nDu bist als " + status + " registriert."))
|
||||
|
||||
} else {
|
||||
w.Write([]byte("User nicht gefunden!"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
Users = make(map[string]User)
|
||||
var a signupHandler
|
||||
var b whoAmIHandler
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/signup", a)
|
||||
mux.Handle("/whoami", b)
|
||||
http.ListenAndServe("localhost:8080", mux)
|
||||
}
|
||||
Loading…
Reference in New Issue