From 932445fc76b386d62a95dcd9c05660ae1807f351 Mon Sep 17 00:00:00 2001 From: Raymaekers Luca Date: Thu, 30 Oct 2025 13:17:30 +0100 Subject: checkpoint --- .gitignore | 1 + assets/favicon.ico | Bin 0 -> 34777 bytes go.mod | 3 ++ index.tmpl.html | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 214 insertions(+) create mode 100644 .gitignore create mode 100644 assets/favicon.ico create mode 100644 go.mod create mode 100644 index.tmpl.html create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a9a5aec --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tmp diff --git a/assets/favicon.ico b/assets/favicon.ico new file mode 100644 index 0000000..3c52275 Binary files /dev/null and b/assets/favicon.ico differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d0d3d78 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module m + +go 1.25.3 diff --git a/index.tmpl.html b/index.tmpl.html new file mode 100644 index 0000000..78bb347 --- /dev/null +++ b/index.tmpl.html @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + +
+

Qui es-tu?

+
+ {{ range . }} + + {{ end }} +
+
+ + diff --git a/main.go b/main.go new file mode 100644 index 0000000..c948780 --- /dev/null +++ b/main.go @@ -0,0 +1,106 @@ +package main + +//- Libraries +import "fmt" +import "net/http" +import "math/rand" +import "html/template" +import "strings" +import _ "embed" + +//- Types +type Person struct { + Name string + Other int +} + +//- Globals + +//go:embed index.tmpl.html +var page_html string + +var people = []Person{ + {Name: "Nawel"}, + {Name: "Tobias"}, + {Name: "Luca"}, + {Name: "Aeris"}, + {Name: "Lionel"}, + {Name: "Aurélie"}, + {Name: "Sean"}, + {Name: "Émilie"}, + {Name: "Yves"}, + {Name: "Marthe"}, +} +var people_count = len(people) + +//- Main +func main() { + var seed int64 + seed = rand.Int63() + seed = 5642410750512497522 + fmt.Println("seed:", seed) + + src := rand.NewSource(seed) + r := rand.New(src) + rand.Seed(seed) + + var list []int + correct := false + for !correct { + list = r.Perm(people_count) + + correct = true + for i, v := range list { + if(v == i) { + fmt.Println("incorrect, need to reshuffle") + correct = false + break + } + } + } + + for i, v := range list { + people[i].Other = v + } + + http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./assets")))) + + http.HandleFunc("/person/", func(writer http.ResponseWriter, request *http.Request) { + name := request.FormValue("name") + + var found bool + for _, value := range people { + if name == value.Name { + found = true + } + } + + if found { + fmt.Fprintln(writer, "ok") + } else { + fmt.Fprintln(writer, "error") + } + }) + + // Execute the template before-hand since the contents won't change. + var buf strings.Builder + template_response, err := template.New("roulette").Parse(page_html) + if err != nil { + fmt.Println(err) + } + template_response.ExecuteTemplate(&buf, "roulette", people) + response := buf.String() + + http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { + fmt.Fprint(writer, response) + }) + + address := "localhost:15118" + fmt.Printf("Listening on http://%s\n", address) + err = http.ListenAndServe(address, nil) + if err != nil { + panic(err) + } + + return +} -- cgit v1.2.3-70-g09d2