This commit is contained in:
Gian Breitenstein 2024-09-26 16:43:51 +02:00
parent 68fa1e0fa9
commit 073bf775db
4 changed files with 117 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.schreifuchs.ch/aspergerli/Webchance
go 1.22.7

47
main.go Normal file
View File

@ -0,0 +1,47 @@
package main
import (
"fmt"
"math/rand"
"net/http"
"strconv"
)
var winners []string = make([]string, 0, 20)
func main() {
http.HandleFunc("/random", randomNumber)
http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":8080", nil)
}
func randomNumber(w http.ResponseWriter, r *http.Request) {
chance, err := strconv.Atoi(r.URL.Query().Get("chance"))
if err != nil {
w.WriteHeader(400)
return
}
multiplicator, err := strconv.Atoi(r.URL.Query().Get("multiplicator"))
if err != nil {
w.WriteHeader(400)
return
}
won := randomize(chance, multiplicator)
winners = append(winners, "mi name")
fmt.Fprint(w, won)
}
func randomize(chance int, mult int) bool {
r := rand.Intn(chance * mult)
if r == 0 {
if chance == 10 && mult == 1 {
return false
} else {
return true
}
} else {
if chance == 10 && mult == 1 {
return true
} else {
return false
}
}
}

14
static/app.js Normal file
View File

@ -0,0 +1,14 @@
async function getData() {
const chance = document.getElementById("Chance").value
const mult = document.getElementById("Multiplicator").value
const url = "/random?chance="+chance+"&multiplicator="+mult;
try {
const response = await fetch(url);
const content = await response.text()
document.getElementById("won").innerText=content
} catch (error) {
console.error(error.message);
}
}

53
static/index.html Normal file
View File

@ -0,0 +1,53 @@
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script type = "text/javascript" src="app.js"></script>
</head>
<style>
body {
background-color: rgba(131, 143, 197, 0.986);
}
.labelcontainer {
display: flex;
}
label {
display: block;
color: rgb(28, 17, 85);
min-width: 20rem;
font-size: 1.5rem;
}
input {
width: 10rem;
}
</style>
<body>
<script> </script>
<h1>Das ist die webseite von Gian</h1>
<p>This is a paragraph.</p>
<p id="won"></p>
<div class="labelcontainer">
<label>Chance</label>
<input type="number" id="Chance" name="Chance" min="10">
</div>
<div class="labelcontainer">
<label>Multiplicator</label>
<input type="number" id="Multiplicator" name="Multiplicator" min="1" max="3">
</div>
<div class="labelcontainer">
<label>name</label>
<input type="text">
</div>
<div>
<input type="button" onclick="getData()" value="Generate">
</div>
</body>
</html>