summaryrefslogtreecommitdiff
path: root/main.go
blob: c9487805b9f834b68ae276066bad2cc5e14a508a (plain) (blame)
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
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
}