-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathfrontend.go
70 lines (61 loc) · 1.89 KB
/
frontend.go
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
package codenames
import (
"math/rand"
"net/http"
"strings"
)
const tpl = `
<!DOCTYPE html>
<html>
<head>
<title>Codenames</title>
<script src="/js/lib/browser.min.js"></script>
<script src="/js/lib/react.min.js"></script>
<script src="/js/lib/react-dom.min.js"></script>
<script src="/js/lib/jquery-3.0.0.min.js"></script>
<script type="text/babel">
window.autogeneratedGameID = "{{.AutogeneratedGameID}}";
</script>
{{range .JSScripts}}
<script type="text/babel" src="/js/{{ . }}"></script>
{{end}}
{{range .Stylesheets}}
<link rel="stylesheet" type="text/css" href="/css/{{ . }}" />
{{end}}
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
</head>
<body>
<div id="app">
<h1>Codenames board game generator</h1>
<p>Play Codenames across multiple devices: computers, tablets, phones, etc.
Board state automatically syncs between devices.</p>
</div>
<script type="text/babel">
ReactDOM.render(<window.App />, document.getElementById('app'));
</script>
</body>
</html>
`
type templateParameters struct {
AutogeneratedGameID string
JSLibs []string
JSScripts []string
Stylesheets []string
}
func (s *Server) handleIndex(rw http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" {
http.NotFound(rw, req)
return
}
first := s.words[rand.Intn(len(s.words))]
second := s.words[rand.Intn(len(s.words))]
err := s.tpl.Execute(rw, templateParameters{
AutogeneratedGameID: strings.Replace(first+"-"+second, " ", "", -1),
JSLibs: s.jslib.RelativePaths(),
JSScripts: s.js.RelativePaths(),
Stylesheets: s.css.RelativePaths(),
})
if err != nil {
http.Error(rw, "error rendering", http.StatusInternalServerError)
}
}