diff --git a/main.go b/main.go index 1c7b44b..d9e47e4 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" "math/rand" "net/http" "strconv" @@ -10,11 +11,13 @@ import ( var winners []string = make([]string, 0, 20) func main() { + http.HandleFunc("/winners", listWiners) http.HandleFunc("/random", randomNumber) http.Handle("/", http.FileServer(http.Dir("./static"))) http.ListenAndServe(":8080", nil) } func randomNumber(w http.ResponseWriter, r *http.Request) { + name := r.URL.Query().Get("name") chance, err := strconv.Atoi(r.URL.Query().Get("chance")) if err != nil { w.WriteHeader(400) @@ -26,7 +29,12 @@ func randomNumber(w http.ResponseWriter, r *http.Request) { return } won := randomize(chance, multiplicator) - winners = append(winners, "mi name") + log.Println(name) + if name != "" { + winners = append(winners, name) + fmt.Println(winners) + } + fmt.Fprint(w, won) } func randomize(chance int, mult int) bool { @@ -45,3 +53,6 @@ func randomize(chance int, mult int) bool { } } } +func listWiners(w http.ResponseWriter, r *http.Request) { + fmt.Fprint(w, winners) +} diff --git a/static/app.js b/static/app.js index 59e6f8a..27e3197 100644 --- a/static/app.js +++ b/static/app.js @@ -1,13 +1,26 @@ -async function getData() { +async function calcChanche() { + const name = document.getElementById("name").value const chance = document.getElementById("Chance").value const mult = document.getElementById("Multiplicator").value - const url = "/random?chance="+chance+"&multiplicator="+mult; + const url = "/random?chance="+chance+"&multiplicator="+mult+"&name="+name; try { const response = await fetch(url); const content = await response.text() document.getElementById("won").innerText=content + getWinners() + } catch (error) { + console.error(error.message); + } +} +async function getWinners() { + const url = "/winners" + try { + const response = await fetch(url); + const content = await response.text() + + document.getElementById("winners").innerText=content } catch (error) { console.error(error.message); } diff --git a/static/index.html b/static/index.html index 6447baa..5153aa9 100644 --- a/static/index.html +++ b/static/index.html @@ -25,7 +25,6 @@ -

Das ist die webseite von Gian

This is a paragraph.

@@ -42,12 +41,18 @@
- +
- + +
+
+

+ \ No newline at end of file