-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
403 lines (354 loc) · 10.8 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package main
//go:generate esc -o assets.go assets templates
import (
"bufio"
"fmt"
"html/template"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"golang.org/x/crypto/ssh/terminal"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/justinas/alice"
)
// AppName Application name
const AppName = "gjvote"
// The directory that we're storing all of the data in
const DataDir = "data"
// DbName Main Database name, which is <AppName>.db
const DbName = AppName + ".db"
// pageData is stuff that changes per request
type pageData struct {
Site *siteData
Title string
HideTitleImage bool
SubTitle string
Stylesheets []string
HeaderScripts []string
Scripts []string
FlashMessage string
FlashClass string
LoggedIn bool
Menu []menuItem
BottomMenu []menuItem
HideAdminMenu bool
session *pageSession
CurrentJam string
ClientId string
ClientIsAuth bool
ClientIsServer bool
AuthMode int
PublicMode int
TemplateData interface{}
}
type menuItem struct {
Label string
Location string
Icon string
}
var sessionStore *sessions.CookieStore
var r *mux.Router
var m *model
func main() {
var err error
if m, err = NewModel(); err != nil {
errorExit("Unable to initialize Model: " + err.Error())
}
loadConfig()
if err = m.site.SaveToDB(); err != nil {
errorExit("Unable to save site config to DB: " + err.Error())
}
// Save changes to the DB every 5 minutes
go func() {
for {
time.Sleep(5 * time.Minute)
m.saveChanges()
}
}()
initialize()
// We should have a session secret by now, initialize the store
sessionStore = sessions.NewCookieStore([]byte(m.site.sessionSecret))
r = mux.NewRouter()
r.StrictSlash(true)
if m.site.DevMode {
fmt.Println("Operating in Development Mode")
}
r.PathPrefix("/assets/").Handler(http.FileServer(FS(m.site.DevMode)))
// Admin Subrouter
admin := r.PathPrefix("/admin").Subrouter()
admin.HandleFunc("/", handleAdmin)
admin.HandleFunc("/dologin", handleAdminDoLogin)
admin.HandleFunc("/dologout", handleAdminDoLogout)
admin.HandleFunc("/{category}", handleAdmin)
admin.HandleFunc("/{category}/{id}", handleAdmin)
admin.HandleFunc("/{category}/{id}/{function}", handleAdmin)
admin.HandleFunc("/{category}/{id}/{function}/{subid}", handleAdmin)
// Public Subrouter
pub := r.PathPrefix("/").Subrouter()
pub.HandleFunc("/", handleMain)
pub.HandleFunc("/{function}", handleMain)
pub.HandleFunc("/image/{teamid}/{imageid}", handleImageRequest)
pub.HandleFunc("/thumbnail/{teamid}/{imageid}", handleThumbnailRequest)
pub.HandleFunc("/team/{id}", handleTeamMgmtRequest)
pub.HandleFunc("/team/{id}/{function}", handleTeamMgmtRequest)
pub.HandleFunc("/team/{id}/{function}/{subid}", handleTeamMgmtRequest)
// API Subrouter
//api := r.PathPrefix("/api").Subtrouter()
http.Handle("/", r)
chain := alice.New(loggingHandler).Then(r)
// Set up a channel to intercept Ctrl+C for graceful shutdowns
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
// Save the changes when the app quits
fmt.Println("\nFinishing up...")
m.saveChanges()
os.Exit(0)
}()
fmt.Printf("Listening on port %d\n", m.site.Port)
log.Fatal(http.ListenAndServe(m.site.Ip+":"+strconv.Itoa(m.site.Port), chain))
}
func loadConfig() {
if len(os.Args) > 1 {
for _, v := range os.Args {
key := v
val := ""
eqInd := strings.Index(v, "=")
if eqInd > 0 {
// It's a key/val argument
key = v[:eqInd]
val = v[eqInd+1:]
}
switch key {
case "-title":
m.site.Title = val
fmt.Print("Set site title: ", m.site.Title, "\n")
case "-ip":
m.site.Ip = val
fmt.Print("Set site IP: ", m.site.Ip, "\n")
case "-port":
var tryPort int
var err error
if tryPort, err = strconv.Atoi(val); err != nil {
fmt.Print("Invalid port given: ", val, " (Must be an integer)\n")
tryPort = m.site.Port
}
// TODO: Make sure a valid port number is given
m.site.Port = tryPort
case "-session-name":
m.site.SessionName = val
case "-server-dir":
// TODO: Probably check if the given directory is valid
m.site.ServerDir = val
case "-help", "-h", "-?":
printHelp()
done()
case "-dev":
m.site.DevMode = true
case "-reset-defaults":
resetToDefaults()
done()
}
}
}
}
func initialize() {
// Test if we have an admin user first
if !m.hasUser() {
// Nope, create one
reader := bufio.NewReader(os.Stdin)
fmt.Println("Create new Admin user")
fmt.Print("Email: ")
email, _ := reader.ReadString('\n')
email = strings.TrimSpace(email)
var pw1, pw2 []byte
for string(pw1) != string(pw2) || string(pw1) == "" {
fmt.Print("Password: ")
pw1, _ = terminal.ReadPassword(0)
fmt.Println("")
fmt.Print("Repeat Password: ")
pw2, _ = terminal.ReadPassword(0)
fmt.Println("")
if string(pw1) != string(pw2) {
fmt.Println("Entered Passwords don't match!")
}
}
assertError(m.updateUserPassword(email, string(pw1)))
}
// Now test if the 'current jam' is named
if m.jam.Name == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Create New Game Jam")
fmt.Print("GameJam Name: ")
gjName, _ := reader.ReadString('\n')
gjName = strings.TrimSpace(gjName)
m.jam.Name = gjName
assertError(m.jam.SaveToDB())
}
if m.jam.Name != "" {
fmt.Println("Current Jam Name: " + m.jam.Name)
} else {
fmt.Println("No Jam Name Specified")
}
if m.site.sessionSecret == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Println("A good session secret is like a good password")
fmt.Print("Create New Session Secret: ")
sessSc, _ := reader.ReadString('\n')
sessSc = strings.TrimSpace(sessSc)
m.site.sessionSecret = sessSc
assertError(m.site.SaveToDB())
}
}
func loggingHandler(h http.Handler) http.Handler {
return handlers.LoggingHandler(os.Stdout, h)
}
func InitPageData(w http.ResponseWriter, req *http.Request) *pageData {
if m.site.DevMode {
w.Header().Set("Cache-Control", "no-cache")
}
p := new(pageData)
// Get session
var err error
var s *sessions.Session
if s, err = sessionStore.Get(req, m.site.SessionName); err != nil {
fmt.Println("Session error... Recreating.")
//http.Error(w, err.Error(), 500)
}
p.session = new(pageSession)
p.session.session = s
p.session.req = req
p.session.w = w
// First check if we're logged in
userEmail, _ := p.session.getStringValue("email")
// With a valid account
p.LoggedIn = m.isValidUserEmail(userEmail)
p.Site = m.site
p.SubTitle = "GameJam Voting"
p.Stylesheets = make([]string, 0, 0)
p.Stylesheets = append(p.Stylesheets, "/assets/vendor/css/pure-min.css")
p.Stylesheets = append(p.Stylesheets, "/assets/vendor/css/grids-responsive-min.css")
p.Stylesheets = append(p.Stylesheets, "/assets/vendor/material-font/css/material-design-iconic-font.min.css")
p.Stylesheets = append(p.Stylesheets, "/assets/css/gjvote.css")
p.HeaderScripts = make([]string, 0, 0)
p.HeaderScripts = append(p.HeaderScripts, "/assets/vendor/js/snack-min.js")
p.Scripts = make([]string, 0, 0)
p.Scripts = append(p.Scripts, "/assets/js/gjvote.js")
p.FlashMessage, p.FlashClass = p.session.getFlashMessage()
if p.FlashClass == "" {
p.FlashClass = "hidden"
}
// Build the menu
if p.LoggedIn {
p.Menu = append(p.Menu, menuItem{"Admin", "/admin", "zmdi-key"})
p.Menu = append(p.Menu, menuItem{"Jam", "/admin/jam", "zmdi-group"})
p.Menu = append(p.Menu, menuItem{"Teams", "/admin/teams", "zmdi-accounts-alt"})
p.Menu = append(p.Menu, menuItem{"Games", "/admin/games", "zmdi-gamepad"})
p.Menu = append(p.Menu, menuItem{"Votes", "/admin/votes", "zmdi-assignment-check"})
p.Menu = append(p.Menu, menuItem{"Archive", "/admin/archive", "zmdi-archive"})
p.Menu = append(p.Menu, menuItem{"Clients", "/admin/clients", "zmdi-devices"})
p.BottomMenu = append(p.BottomMenu, menuItem{"Users", "/admin/users", "zmdi-accounts"})
p.BottomMenu = append(p.BottomMenu, menuItem{"Logout", "/admin/dologout", "zmdi-eject"})
} else {
p.BottomMenu = append(p.BottomMenu, menuItem{"Admin", "/admin", "zmdi-sign-in"})
}
p.HideAdminMenu = true
p.ClientId = p.session.getClientId()
var cl *Client
if cl, err = m.GetClient(p.ClientId); err != nil {
// A new client
cl = NewClient(p.ClientId)
}
p.ClientIsAuth = cl.Auth
p.ClientIsServer = clientIsServer(req)
// Public Mode
p.PublicMode = m.site.GetPublicMode()
// Authentication Mode
p.AuthMode = m.site.GetAuthMode()
return p
}
func (p *pageData) show(tmplName string, w http.ResponseWriter) error {
for _, tmpl := range []string{
"htmlheader.html",
"header.html",
tmplName,
"footer.html",
"htmlfooter.html",
} {
if err := outputTemplate(tmpl, p, w); err != nil {
fmt.Printf("Error outputting Template %s\n%s\n", tmpl, err)
return err
}
}
return nil
}
// outputTemplate
// Spit out a template
func outputTemplate(tmplName string, tmplData interface{}, w http.ResponseWriter) error {
n := "/templates/" + tmplName
l := template.Must(template.New("layout").Parse(FSMustString(m.site.DevMode, n)))
t := template.Must(l.Parse(FSMustString(m.site.DevMode, n)))
return t.Execute(w, tmplData)
}
// redirect can be used only for GET redirects
func redirect(url string, w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, url, 303)
}
func resetToDefaults() {
def := NewSiteData(m)
fmt.Println("Reset settings to defaults?")
fmt.Print(m.site.Title, " -> ", def.Title, "\n")
fmt.Print(m.site.Port, " -> ", def.Port, "\n")
fmt.Print(m.site.SessionName, " -> ", def.SessionName, "\n")
fmt.Print(m.site.ServerDir, " -> ", def.ServerDir, "\n")
fmt.Println("Are you sure? (y/N): ")
reader := bufio.NewReader(os.Stdin)
conf, _ := reader.ReadString('\n')
conf = strings.ToUpper(strings.TrimSpace(conf))
if strings.HasPrefix(conf, "Y") {
if err := def.SaveToDB(); err != nil {
errorExit("Error resetting to defaults: " + err.Error())
}
fmt.Println("Reset to defaults")
}
}
func printHelp() {
help := []string{
"Game Jam Voting Help",
" -help, -h, -? Print this message",
" -dev Development mode, load assets from file system",
" -port=<port num> Set the site port",
" -session-name=<session> Set the name of the session to be used",
" -server-dir=<directory> Set the server directory",
" This designates where the database will be saved",
" and where the app will look for files if you're",
" operating in 'development' mode (-dev)",
" -title=<title> Set the site title",
" -current-jam=<name> Change the name of the current jam",
" -reset-defaults Reset all configuration options to defaults",
"",
}
for _, v := range help {
fmt.Println(v)
}
}
func done() {
os.Exit(0)
}
func errorExit(msg string) {
fmt.Println(msg)
os.Exit(1)
}
func assertError(err error) {
if err != nil {
panic(err)
}
}