diff options
| author | Raymaekers Luca <luca@spacehb.net> | 2025-10-30 13:17:30 +0100 |
|---|---|---|
| committer | Raymaekers Luca <luca@spacehb.net> | 2025-10-30 13:17:30 +0100 |
| commit | 932445fc76b386d62a95dcd9c05660ae1807f351 (patch) | |
| tree | 8702d4ddfc323dbb27546c77fb6e4320cbefe1e2 | |
checkpoint
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | assets/favicon.ico | bin | 0 -> 34777 bytes | |||
| -rw-r--r-- | go.mod | 3 | ||||
| -rw-r--r-- | index.tmpl.html | 104 | ||||
| -rw-r--r-- | main.go | 106 |
5 files changed, 214 insertions, 0 deletions
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 Binary files differnew file mode 100644 index 0000000..3c52275 --- /dev/null +++ b/assets/favicon.ico @@ -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 @@ + <!DOCTYPE html> + <html lang="fr"> + <head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + + <meta property="og:title" content="Cadeaux de noël 2025"/> + <meta property="og:description" content="À qui offrir le cadeau de noël?"/> + + <meta property="og:image" content="/static/favicon.ico" /> + + <link rel="icon" type="image/x-icon" href="/static/favicon.ico"> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"> + <style> +h1 { + display: flex; + justify-content: center; +} +button { + font-size: 1em; + margin-left: 1em; + margin-right: 1em; +} +#buttons { + display: flex; + justify-content: center; + align-items: center; +} + +body { + display: flex; + justify-content: center; + align-items: center; + height: 50vh; +} + </style> + <script> +let people = +[{{ range . }} + {"name": "{{.Name}}", "other": {{.Other}}}, {{ end }} +]; + +function clickButton(button) { + let name = button.innerText; + let person = null; + let person_index = 0; + for(; person_index < people.length; person_index += 1) + { + if(people[person_index].name === name) + { + person = people[person_index]; + break; + } + } + + fetch('/person/', { + method: 'POST', + headers: + { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: "name=" + person.name + }) + .then(function(response) { + response.text(). + then(function(text_response) { + if(text_response === 'ok') { + window.location.reload(); + } + }) + }) + .catch(error => console.error('Error:', error)); + + let other_person = people[person.other]; + document.body.innerHTML = "<h1>Tu as: " + other_person.name + "</h1>"; +} + +window.onload = function() { + let buttons = document.querySelectorAll("button"); + + for(let index = 0; index < buttons.length; index += 1) + { + let button = buttons[index]; + button.addEventListener("click", function(event) { + event.preventDefault(); + clickButton(button); + }); + } + +}; + </script> + + </head> + <body> + <div> + <h1>Qui es-tu?</h1> + <div id="buttons"> + {{ range . }} + <button>{{ .Name }}</button> + {{ end }} + </div> + </div> + </body> + </html> @@ -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 +} |
