1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
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
}
type PageData struct {
People []Person
Seed int64
}
var DEBUG = true
//- 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)
func TemplateToString(page_html string, people []Person, seed int64) string {
var buf strings.Builder
template_response, err := template.New("roulette").Parse(page_html)
if err != nil {
fmt.Println(err)
}
err = template_response.ExecuteTemplate(&buf, "roulette", PageData{people, seed})
if err != nil {
fmt.Println(err)
}
response := buf.String()
return response
}
// - Main
func main() {
var seed int64
seed = rand.Int63()
seed = 1924480304604450476
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.
response := TemplateToString(page_html, people, seed);
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
if DEBUG {
response = TemplateToString(page_html, people, seed);
}
fmt.Fprint(writer, response)
})
var address string
if DEBUG {
address = "0.0.0.0:15118"
} else {
address = "localhost:15118"
}
fmt.Printf("Listening on http://%s\n", address)
if err := http.ListenAndServe(address, nil); err != nil {
panic(err)
}
return
}
|