added winners
All checks were successful
Release / publish (push) Successful in 59s

This commit is contained in:
Gian Breitenstein 2024-09-26 17:46:12 +02:00
parent a067952f7d
commit 5cae25cb2f
3 changed files with 35 additions and 6 deletions

13
main.go
View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"log"
"math/rand" "math/rand"
"net/http" "net/http"
"strconv" "strconv"
@ -10,11 +11,13 @@ import (
var winners []string = make([]string, 0, 20) var winners []string = make([]string, 0, 20)
func main() { func main() {
http.HandleFunc("/winners", listWiners)
http.HandleFunc("/random", randomNumber) http.HandleFunc("/random", randomNumber)
http.Handle("/", http.FileServer(http.Dir("./static"))) http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }
func randomNumber(w http.ResponseWriter, r *http.Request) { func randomNumber(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
chance, err := strconv.Atoi(r.URL.Query().Get("chance")) chance, err := strconv.Atoi(r.URL.Query().Get("chance"))
if err != nil { if err != nil {
w.WriteHeader(400) w.WriteHeader(400)
@ -26,7 +29,12 @@ func randomNumber(w http.ResponseWriter, r *http.Request) {
return return
} }
won := randomize(chance, multiplicator) 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) fmt.Fprint(w, won)
} }
func randomize(chance int, mult int) bool { 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)
}

View File

@ -1,13 +1,26 @@
async function getData() { async function calcChanche() {
const name = document.getElementById("name").value
const chance = document.getElementById("Chance").value const chance = document.getElementById("Chance").value
const mult = document.getElementById("Multiplicator").value const mult = document.getElementById("Multiplicator").value
const url = "/random?chance="+chance+"&multiplicator="+mult; const url = "/random?chance="+chance+"&multiplicator="+mult+"&name="+name;
try { try {
const response = await fetch(url); const response = await fetch(url);
const content = await response.text() const content = await response.text()
document.getElementById("won").innerText=content 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) { } catch (error) {
console.error(error.message); console.error(error.message);
} }

View File

@ -25,7 +25,6 @@
</style> </style>
<body> <body>
<script> </script>
<h1>Das ist die webseite von Gian</h1> <h1>Das ist die webseite von Gian</h1>
<p>This is a paragraph.</p> <p>This is a paragraph.</p>
@ -42,12 +41,18 @@
</div> </div>
<div class="labelcontainer"> <div class="labelcontainer">
<label>name</label> <label>name</label>
<input type="text"> <input type="text" id="name">
</div> </div>
<div> <div>
<input type="button" onclick="getData()" value="Generate"> <input type="button" onclick="calcChanche()" value="Generate">
</div>
<div>
<p id="winners"></p>
</div> </div>
</body> </body>
<script>
getWinners()
</script>
</html> </html>