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 (
"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)
}

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 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);
}

View File

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