This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
153 lines (130 loc) · 3.66 KB
/
ui.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package splenda
import (
"bytes"
"io/ioutil"
"net/http"
"path"
"strings"
"time"
)
// Root handles GET /, loading the homepage (authenticated or unauthenticated).
func (a *api) Root(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
res.WriteHeader(405)
return
}
_, err := a.authorize(req)
if err != nil {
renderFile("web/index.html", res)
} else {
renderFile("web/index-authenticated.html", res)
}
}
// Asset handles requests for arbitrary public assets from the web folder.
func (a *api) Asset(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
res.WriteHeader(405)
return
}
file := path.Join("web", strings.TrimPrefix(req.URL.Path, "/assets/"))
renderFile(file, res)
}
// Signup handles GET /signup, rendering the UI for signing up a new user.
func (a *api) Signup(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
res.WriteHeader(405)
return
}
renderFile("web/signup.html", res)
}
// Login handles POST /login, the browser-based log-in endpoint.
func (a *api) Login(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodPost {
res.WriteHeader(405)
return
}
if err := req.ParseForm(); err != nil {
res.WriteHeader(400)
return
}
id := req.Form.Get("id")
pw := req.Form.Get("pw")
sid, err := a.auth.Login(id, pw)
if err != nil {
renderFile("web/login-failed.html", res)
return
}
// Redirect back to the homepage with a session cookie.
http.SetCookie(res, &http.Cookie{
Name: "sid",
Value: sid,
Path: "/",
Expires: time.Now().Add(13 * 24 * time.Hour),
})
res.Header().Set("Location", "/")
res.WriteHeader(303)
}
// Logout handles GET /logout, the browser-based log-out endpoint.
func (a *api) Logout(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
res.WriteHeader(405)
return
}
// Redirect back to the homepage with NO session cookie.
http.SetCookie(res, &http.Cookie{
Name: "sid",
Value: "",
Path: "/",
Expires: time.Now().Add(-1 * time.Hour),
})
res.Header().Set("Location", "/")
res.WriteHeader(303)
}
// Game handles GET /games/<id>, rendering the game view.
func (a *api) Game(res http.ResponseWriter, req *http.Request) {
if req.Method != http.MethodGet {
res.WriteHeader(405)
return
}
userID, err := a.authorize(req)
if err != nil {
res.WriteHeader(401)
return
}
gameID := strings.TrimPrefix(req.URL.Path, "/games/")
renderTemplate("web/game.html", gameID, userID, res)
}
// RenderFile renders a non-template file.
func renderFile(file string, res http.ResponseWriter) {
bs, err := ioutil.ReadFile(file)
if err != nil {
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
renderBytes(file, bs, res)
}
// RenderTemplate renders a templated file, subbing in {{GAMEID}} and {{USERID}}.
func renderTemplate(file string, gameID string, userID string, res http.ResponseWriter) {
bs, err := ioutil.ReadFile(file)
if err != nil {
res.WriteHeader(500)
res.Write([]byte(err.Error() + "\n"))
return
}
bs = bytes.ReplaceAll(bs, []byte("{{GAMEID}}"), []byte(gameID))
bs = bytes.ReplaceAll(bs, []byte("{{USERID}}"), []byte(userID))
renderBytes(file, bs, res)
}
// RenderBytes renders the pre-loaded bytes of a file with the appropriate MIME type.
func renderBytes(file string, bs []byte, res http.ResponseWriter) {
if strings.HasSuffix(file, ".html") {
res.Header().Set("Content-Type", "text/html")
} else if strings.HasSuffix(file, ".js") {
res.Header().Set("Content-Type", "text/javascript")
} else if strings.HasSuffix(file, ".css") {
res.Header().Set("Content-Type", "text/css")
}
res.WriteHeader(200)
res.Write(bs)
}