-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
223 lines (186 loc) · 7.4 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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"path/filepath"
"github.com/gorilla/mux"
"github.com/oxtoacart/bpool"
r "gopkg.in/dancannon/gorethink.v2"
)
type Page struct {
User *User
PermissionLevels PermissionLevels
Data interface{}
}
type key int
const UserKey key = 0
var siteConfiguration *Configuration
var templates map[string]*template.Template
var bufpool *bpool.BufferPool
var dataStore *DataStore
func parseTemplates() {
if templates == nil {
templates = make(map[string]*template.Template)
}
layouts, err := filepath.Glob("layouts/*.html")
if err != nil {
log.Fatal(err)
}
includes, err := filepath.Glob("includes/*.html")
if err != nil {
log.Fatal(err)
}
for _, layout := range layouts {
files := append(includes, layout)
templates[filepath.Base(layout)] = template.Must(template.ParseFiles(files...))
}
}
func renderTemplate(w http.ResponseWriter, r *http.Request, templateName string, data interface{}) {
tmpl, ok := templates[templateName+".html"]
if !ok {
http.Error(w, "Template not found", http.StatusInternalServerError)
return
}
buf := bufpool.Get()
defer bufpool.Put(buf)
var user *User
if email, ok := isLoggedIn(r); ok {
user = fetchUserByEmail(email)
}
page := Page{
User: user,
PermissionLevels: getPermissionLevels(),
Data: data,
}
err := tmpl.ExecuteTemplate(buf, "base", page)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
buf.WriteTo(w)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, r, "home", nil)
}
func initializeTables() {
r.DBCreate("velvetdb").Run(dataStore.GetSession())
r.TableCreate("users").Run(dataStore.GetSession())
r.TableCreate("players").Run(dataStore.GetSession())
r.TableCreate("gametypes").Run(dataStore.GetSession())
r.TableCreate("matches").Run(dataStore.GetSession())
r.TableCreate("tournaments").Run(dataStore.GetSession())
r.TableCreate("tournamentresults").Run(dataStore.GetSession())
r.TableCreate("sessions").Run(dataStore.GetSession())
}
func isAdminMiddleware(next func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, ok := isLoggedIn(r)
if !ok {
http.NotFound(w, r)
return
}
next(w, r)
})
}
func hasPermissionMiddleware(next func(http.ResponseWriter, *http.Request), permission int) func(http.ResponseWriter, *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
email, ok := isLoggedIn(r)
if !ok {
http.NotFound(w, r)
return
}
u := fetchUserByEmail(email)
if u == nil {
http.NotFound(w, r)
return
}
if !u.HasPermission(permission) {
http.NotFound(w, r)
return
}
next(w, r)
})
}
func main() {
siteConfiguration = getConfiguration()
session, err := r.Connect(r.ConnectOpts{
Address: siteConfiguration.RethinkConnection,
Database: "velvetdb",
})
if err != nil {
log.Fatalln(err.Error())
}
dataStore = &DataStore{session: session}
initializeTables()
initializeSessionStore()
bufpool = bpool.NewBufferPool(64)
parseTemplates()
r := mux.NewRouter()
// Serve files from the assets directory
r.PathPrefix("/assets/").Handler(http.StripPrefix("/assets/",
http.FileServer(http.Dir("assets/"))))
r.HandleFunc("/", homeHandler)
r.HandleFunc("/editplayer/{playerNick:[-a-zA-Z0-9]+}", isAdminMiddleware(editPlayerHandler))
r.HandleFunc("/players", playersHandler)
r.HandleFunc("/player/{playerNick:[-a-zA-Z0-9]+}", playerViewHandler)
r.HandleFunc("/addplayer", isAdminMiddleware(addPlayerHandler))
r.HandleFunc("/addgametype", isAdminMiddleware(addGameTypeHandler))
r.HandleFunc("/addmatch", isAdminMiddleware(addMatchHandler))
r.HandleFunc("/edit/match/{match:[-a-zA-Z0-9]+}", isAdminMiddleware(editMatchHandler))
r.HandleFunc("/save/match/{match:[-a-zA-Z0-9]+}", isAdminMiddleware(saveEditMatchHandler))
r.HandleFunc("/addtournament", isAdminMiddleware(addTournamentHandler))
r.HandleFunc("/addpool/{tournament:[-a-zA-Z0-9]+}", isAdminMiddleware(addPoolHandler))
r.HandleFunc("/save/addpool", isAdminMiddleware(savePoolHandler))
r.HandleFunc("/tournaments", viewTournamentsHandler)
r.HandleFunc("/tournaments/{gametype}", viewTournamentsHandler)
r.HandleFunc("/save/addmatch", isAdminMiddleware(saveMatchHandler))
r.HandleFunc("/save/addplayer", isAdminMiddleware(savePlayerHandler))
r.HandleFunc("/save/editplayer/{playerNick:[-a-zA-Z0-9]+}", isAdminMiddleware(saveEditPlayerHandler))
r.HandleFunc("/save/addgametype", isAdminMiddleware(saveGameTypeHandler))
r.HandleFunc("/save/addtournament", isAdminMiddleware(saveTournamentHandler))
r.HandleFunc("/edit/tournament/{tournament:[-a-zA-Z0-9]+}", isAdminMiddleware(editTournamentHandler))
r.HandleFunc("/save/tournament/{tournament:[-a-zA-Z0-9]+}", isAdminMiddleware(saveEditTournamentHandler))
r.HandleFunc("/save/addtournamentmatch/{tournament:[-a-zA-Z0-9]+}", isAdminMiddleware(saveTournamentMatchesHandler))
r.HandleFunc("/tournament/addmatches/{tournament:[-a-zA-Z0-9]+}", isAdminMiddleware(addTournamentMatchesHandler))
r.HandleFunc("/tournament/{tournament:[-a-zA-Z0-9]+}", viewTournamentHandler)
r.HandleFunc("/tournament/delete/{tournament:[-a-zA-Z0-9]+}", isAdminMiddleware(deleteTournamentHandler))
r.HandleFunc("/save/tournament/delete/{tournament:[-a-zA-Z0-9]+}", isAdminMiddleware(saveDeleteTournamentHandler))
// Tournament results
r.HandleFunc("/tournamentresult/edit/{result:[-a-zA-Z0-9]+}", isAdminMiddleware(editTournamentResultHandler))
r.HandleFunc("/save/tournamentresult/edit/{result:[-a-zA-Z0-9]+}", isAdminMiddleware(saveEditTournamentResultHandler))
// Merge players
r.HandleFunc("/players/merge", isAdminMiddleware(mergePlayersHandler))
r.HandleFunc("/save/merge/players", isAdminMiddleware(saveMergePlayersHandler))
// First run
r.HandleFunc("/firstrun", firstRunHandler)
r.HandleFunc("/firstrun/save", saveFirstRunHandler)
// Faceoff
r.HandleFunc("/faceoff", faceoffHandler)
// Rankings
r.HandleFunc("/rankings", rankingsHandler)
r.HandleFunc("/rankings/{gametype}", rankingsHandler)
// auth
r.HandleFunc("/users", hasPermissionMiddleware(userListHandler, getPermissionLevels().CanModifyUsers))
r.HandleFunc("/profile", isAdminMiddleware(userProfileHandler))
r.HandleFunc("/adduser", hasPermissionMiddleware(registerUserHandler, getPermissionLevels().CanModifyUsers))
r.HandleFunc("/login", loginUserHandler)
r.HandleFunc("/save/login", saveLoginUserHandler)
r.HandleFunc("/save/logout", saveLogoutUserHandler)
r.HandleFunc("/save/adduser", hasPermissionMiddleware(saveRegisterUserHandler, getPermissionLevels().CanModifyUsers))
r.HandleFunc("/save/changepassword", isAdminMiddleware(saveChangePasswordHandler))
// API
api := r.PathPrefix("/api/v1").Subrouter()
api.Methods("OPTIONS").HandlerFunc(handleAPIPreflight)
api.HandleFunc("/gametypes", handleAPIGameTypes)
api.HandleFunc("/players", handleAPIPlayers)
api.HandleFunc("/players/search", handleAPIPlayersSearch)
api.HandleFunc("/players/{id:[-a-zA-Z0-9]+}", handleAPIPlayer)
api.HandleFunc("/players/{id:[-a-zA-Z0-9]+}/tournamentresults", handleAPIPlayerTournamentResults)
api.HandleFunc("/players/{id:[-a-zA-Z0-9]+}/matches", handleAPIPlayerMatches)
api.HandleFunc("/players/{p1:[-a-zA-Z0-9]+}/{p2:[-a-zA-Z0-9]+}/matches", handleAPIFaceoff)
fmt.Println("We're up and running!")
http.ListenAndServe(":3000", r)
}