summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorRaymaekers Luca <luca@spacehb.net>2025-10-30 13:17:30 +0100
committerRaymaekers Luca <luca@spacehb.net>2025-10-30 13:17:30 +0100
commit932445fc76b386d62a95dcd9c05660ae1807f351 (patch)
tree8702d4ddfc323dbb27546c77fb6e4320cbefe1e2 /main.go
checkpoint
Diffstat (limited to 'main.go')
-rw-r--r--main.go106
1 files changed, 106 insertions, 0 deletions
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
+}