From 073bf775db5af1d4e8f36a17fdfa28e90df56ff6 Mon Sep 17 00:00:00 2001 From: Gian Breitenstein Date: Thu, 26 Sep 2024 16:43:51 +0200 Subject: [PATCH] init --- go.mod | 3 +++ main.go | 47 +++++++++++++++++++++++++++++++++++++++++ static/app.js | 14 +++++++++++++ static/index.html | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 go.mod create mode 100644 main.go create mode 100644 static/app.js create mode 100644 static/index.html diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7f150eb --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.schreifuchs.ch/aspergerli/Webchance + +go 1.22.7 diff --git a/main.go b/main.go new file mode 100644 index 0000000..1c7b44b --- /dev/null +++ b/main.go @@ -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 + } + } +} diff --git a/static/app.js b/static/app.js new file mode 100644 index 0000000..59e6f8a --- /dev/null +++ b/static/app.js @@ -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); + } +} \ No newline at end of file diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..6447baa --- /dev/null +++ b/static/index.html @@ -0,0 +1,53 @@ + + + + + Page Title + + + + + + +

Das ist die webseite von Gian

+

This is a paragraph.

+ +

+
+ + + +
+
+ + + +
+
+ + +
+
+ +
+ + + + \ No newline at end of file