/
index.go
370 lines (331 loc) · 12.8 KB
/
index.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
package main
import (
"fmt"
"math/rand"
"net/http"
"net/url"
"os"
"time"
"github.com/gorilla/mux"
"context"
)
// ThemeVersion is the version of the current CSS-JS theme.
// It is the name of the folder containing the theme files.
const ThemeVersion = "default"
// ThemeDate is the prefix used for "revving" the static files and enable long-term HTTP cache.
// It MUST end with underscore _ (see app.yaml)
// #204: $GAE_VERSION automatically changes at each deployment.
var ThemeDate = os.Getenv("GAE_VERSION") + "_"
var r = mux.NewRouter()
func init() {
initEnv()
initToggles()
initRoutes()
// We want the random results to be different even if we reboot the server. Thus, we use
// the clock to seed the default generator.
// See https://programming-idioms.org/idiom/70/use-clock-as-random-generator-seed/346/go
rand.Seed(time.Now().UnixNano())
}
func initRoutes() {
if !toggles["online"] {
handle("/", makeWall("<i class=\"icon-wrench icon-2x\"> Under maintenance.</i>"))
} else {
handle("/", home)
handle("/home", home)
handle("/wall", makeWall("<i class=\"icon-wrench icon-2x\"> Coming soon.</i>"))
handle("/idiom/{idiomId}", idiomDetail)
handle("/idiom/{idiomId}/impl/{implId}", idiomDetail)
handle("/idiom/{idiomId}/{idiomTitle}", idiomDetail)
handle("/idiom/{idiomId}/diff/{v1}/{v2}", versionDiff)
handle("/idiom/{idiomId}/impl/{implId}/diff/{v1}/{v2}", versionDiff)
handle("/idiom/{idiomId}/{idiomTitle}/{implId}/{implLang}", idiomDetail)
handle("/about", aboutPage("page-about"))
handle("/coverage", aboutPage("page-about-language-coverage"))
handle("/all-idioms", aboutPage("page-about-all-idioms"))
handle("/feeds", aboutPage("page-about-feeds"))
handle("/cheatsheets", aboutPage("page-about-cheatsheets"))
handle("/backlogs", aboutPage("page-about-backlogs"))
handle("/see-also", aboutPage("page-about-see-also"))
handle("/contact", aboutPage("page-about-contact"))
handle("/history/{idiomId}", idiomHistory)
handle("/history/{idiomId}/impl/{implId}", implHistory)
handle("/revert", revertIdiomVersion)
handle("/admin-history-restore", restoreIdiomVersion)
handle("/random-idiom/having/{havingLang}", randomIdiomHaving)
handle("/random-idiom/not-having/{notHavingLang}", randomIdiomNotHaving)
handle("/random-idiom", randomIdiom)
handle("/search", searchRedirect)
handle("/search/{q}", search)
handle("/list-by-language/{langs}", listByLanguage)
handle("/missing-fields/{lang}", missingList)
handle("/next-idiom/{idiomId}", nextIdiom)
handle("/previous-idiom/{idiomId}", previousIdiom)
handle("/backlog/{lang}", backlogForLanguage)
handle("/backlog/{lang}/block/curation", backlogBlockCuration)
handle("/backlog/{lang}/block/docs-demos", backlogBlockDocsDemos)
handle("/backlog/{lang}/block/missing-impl", backlogBlockMissingImpl)
handle("/idiom-picture", idiomPicture)
handle("/rss-recently-created", rssRecentlyCreated)
handle("/rss-recently-updated", rssRecentlyUpdated)
handle("/rss-recent-changes", rssRecentChanges)
handle("/my/{nickname}/{langs}", bookmarkableUserURL)
handle("/my/{langs}", bookmarkableUserURL)
// handle("/cheatsheet/{lang}", cheatsheet)
handle("/cheatsheet/{langs:.+}", cheatsheetMulti)
r.PathPrefix("/cheatsheet").HandlerFunc(cheatsheetPageRedirect)
handleAjax("/typeahead-languages", typeaheadLanguages)
handleAjax("/supported-languages", supportedLanguages)
handleAjax("/ajax-other-implementations", ajaxOtherImplementations)
handleAjax("/ajax-impl-flag/{idiomId}/{implId}", ajaxImplFlag)
if toggles["writable"] {
// When not in "read-only" mode
handle("/idiom-save", idiomSave)
handle("/idiom-edit/{idiomId}", idiomEdit)
handle("/idiom-add-picture/{idiomId}", idiomAddPicture)
handle("/idiom-save-picture", idiomSavePicture)
handle("/impl-edit/{idiomId}/{implId}", implEdit)
//handle("/fake-idiom-save", fakeIdiomSave)
handle("/idiom-create", idiomCreate)
handle("/impl-create/{idiomId}", implCreate)
handle("/impl-create/{idiomId}/{lang}", implCreate)
handle("/impl-save", implSave)
// Ajax
handleAjax("/ajax-idiom-vote", ajaxIdiomVote)
handleAjax("/ajax-impl-vote", ajaxImplVote)
handleAjax("/ajax-demo-site-suggest", ajaxDemoSiteSuggest)
handleAjax("/ajax-user-message-box", userMessageBoxAjax)
handleAjax("/ajax-dismiss-user-message", dismissUserMessage)
handle("/about-block-project", ajaxAboutProject)
handle("/about-block-all-idioms", ajaxAboutAllIdioms)
handle("/about-block-language-coverage", ajaxAboutLanguageCoverage)
handle("/about-block-rss", ajaxAboutRss)
handle("/about-block-cheatsheets", ajaxAboutCheatsheets)
handle("/about-block-backlogs", ajaxAboutBacklogs)
handle("/about-block-see-also", ajaxAboutSeeAlso)
handle("/about-block-contact", ajaxAboutContact)
// Admin
handle("/admin", admin)
handle("/admin-data-export", adminExport)
handle("/admin-data-import", adminImport)
handle("/admin-resave-entities", adminResaveEntities)
handle("/admin-flagged", adminListFlaggedContent)
handleAjax("/admin-repair-history-versions", adminRepairHistoryVersions)
handleAjax("/admin-data-import-ajax", adminImportAjax)
handleAjax("/admin-reindex-ajax", adminReindexAjax)
handleAjax("/admin-refresh-toggles-ajax", ajaxRefreshToggles)
handleAjax("/admin-set-toggle-ajax", ajaxSetToggle)
handleAjax("/admin-create-relation-ajax", ajaxCreateRelation)
handleAjax("/admin-idiom-delete", idiomDelete)
handleAjax("/admin-impl-delete", implDelete)
handleAjax("/admin-send-message-for-user", sendMessageForUserAjax)
handleAjax("/admin-flag-resolve", ajaxAdminFlagResolve)
handleAjax("/admin-memcache-flush", ajaxAdminMemcacheFlush)
}
handleAjax("/api/idiom/{idiomId}", jsonIdiom)
handleAjax("/api/idioms/all", jsonAllIdioms)
handleAjax("/api/search/{q}", jsonSearch)
r.PathPrefix("/using/").HandlerFunc(using)
handle("/auth", handleAuth)
handle("/_ah/login_required", handleAuth)
}
http.Handle("/", r)
}
// Request will fail if path parameters are missing
var neededPathVariables = map[string][]string{
"/idiom/{idiomId}": {"idiomId"},
"/idiom/{idiomId}/impl/{implId}": {"idiomId"},
"/idiom/{idiomId}/{idiomTitle}": {"idiomId"},
"/idiom/{idiomId}/{idiomTitle}/{implId}/{implLang}": {"idiomId"},
"/search/{q}": {"q"},
"/my/{nickname}/{langs}": {"nickname", "langs"},
"/idiom-edit/{idiomId}": {"idiomId"},
"/idiom-add-picture/{idiomId}": {"idiomId"},
"/impl-edit/{idiomId}/{implId}": {"idiomId", "implId"},
"/impl-create/{idiomId}": {"idiomId"},
"/impl-create/{idiomId}/{lang}": {"idiomId"},
"/cheatsheet/{lang}": {"lang"},
}
// Request will fail if it doesn't provide the required GET or POST parameters
var neededParameters = map[string][]string{
"/typeahead-languages": { /*todo*/ },
"/idiom-save": {"idiom_title"},
"/idiom-save-picture": { /*todo*/ },
"/impl-save": {"idiom_id", "impl_code"},
"/revert": {"idiomId", "version"},
"/ajax-idiom-vote": {"idiomId", "choice"},
"/ajax-impl-vote": {"implId", "choice"},
"/ajax-demo-site-suggest": { /*todo*/ },
"/ajax-dismiss-user-message": {"key"},
"/admin-data-export": { /*todo*/ },
"/admin-data-import": { /*todo*/ },
"/admin-data-import-ajax": { /*todo*/ },
"/admin-set-toggle-ajax": {"toggle", "value"},
"/admin-create-relation-ajax": {"idiomAId", "idiomBId"},
"/admin-idiom-delete": {"idiomId"},
"/admin-impl-delete": {"idiomId", "implId"},
"/admin-send-message-for-user": {"username", "message"},
"/admin-flag-resolve": {"flagkey"},
"/api/idiom": {"idiomId"},
}
// Request will fail if corresponding toggle is off
var neededToggles = map[string][]string{
"/home": {"online"},
"/search": {"searchable"},
"/search/{q}": {"searchable"},
"/idiom-save": {"writable"},
"/idiom-edit/{idiomId}": {"writable", "writable", "idiomEditing"},
"/idiom-add-picture/{idiomId}": {"writable", "idiomEditing"},
"/idiom-save-picture": {"writable", "idiomEditing"},
"/impl-edit/{idiomId}/{implId}": {"writable", "implEditing"},
"/idiom-create": {"writable"},
"/impl-create/{idiomId}": {"writable", "implAddition"},
"/impl-create/{idiomId}/{lang}": {"writable", "implAddition"},
"/impl-save": {"writable"},
"/ajax-idiom-vote": {"writable"},
"/ajax-impl-vote": {"writable"},
"/admin": {"administrable"},
"/admin-data-export": {"administrable"},
"/admin-data-import": {"administrable"},
"/admin-data-import-ajax": {"administrable"},
"/admin-set-toggle-ajax": {"administrable"},
"/admin-create-relation-ajax": {"administrable"},
"/admin-idiom-delete": {"administrable"},
"/admin-impl-delete": {"administrable"},
}
type standardHandler func(w http.ResponseWriter, r *http.Request)
type betterHandler func(w http.ResponseWriter, r *http.Request) error
// Wrap HandleFunc with
// - error handling
// - mandatory path variables check
// - mandatory parameters check
// - toggles check
func handle(path string, h betterHandler) {
r.HandleFunc(path,
func(w http.ResponseWriter, r *http.Request) {
if isSpam(w, r) {
return
}
defer func() {
if msg := recover(); msg != nil {
errorPage(w, r, PiErrorf(http.StatusInternalServerError, "%v", msg))
return
}
}()
if configTime == "0" {
ctx := r.Context()
_ = refreshToggles(ctx)
// If it fails... well, ignore for now and continue with non-fresh toggles.
}
if err := muxVarsMissing(w, r, neededPathVariables[path]...); err != nil {
errorPage(w, r, err)
return
}
if err := togglesMissing(w, r, neededToggles[path]...); err != nil {
errorPage(w, r, err)
return
}
if err := parametersMissing(w, r, neededParameters[path]...); err != nil {
errorPage(w, r, err)
return
}
err := h(w, r)
if err != nil {
errorPage(w, r, err)
}
})
}
func handleAjax(path string, h betterHandler) {
r.HandleFunc(path,
func(w http.ResponseWriter, r *http.Request) {
if isSpam(w, r) {
return
}
defer func() {
if msg := recover(); msg != nil {
errorJSON(w, r, PiErrorf(http.StatusInternalServerError, "%v", msg))
return
}
}()
if configTime == "0" {
ctx := r.Context()
_ = refreshToggles(ctx)
// If it fails... well, ignore for now and continue with non-fresh toggles.
}
if err := muxVarsMissing(w, r, neededPathVariables[path]...); err != nil {
errorJSON(w, r, err)
return
}
if err := togglesMissing(w, r, neededToggles[path]...); err != nil {
errorJSON(w, r, err)
return
}
if err := parametersMissing(w, r, neededParameters[path]...); err != nil {
errorJSON(w, r, err)
return
}
// TODO: maybe we never want any other Content-Type so...?
// w.Header().Set("Content-Type", "application/json")
err := h(w, r)
if err != nil {
errorJSON(w, r, err)
}
})
}
var datastoreDao = GaeDatastoreAccessor{}
var memcachedDao = MemcacheDatastoreAccessor{datastoreDao}
var dao = memcachedDao
var daoVotes = GaeVotesAccessor{}
func parametersMissing(w http.ResponseWriter, r *http.Request, params ...string) error {
missing := []string{}
for _, param := range params {
if r.FormValue(param) == "" {
missing = append(missing, param)
}
}
if len(missing) > 0 {
return PiErrorf(http.StatusBadRequest, "Missing parameters : %s", missing)
}
return nil
}
// Looks in gorilla mux populated variables
func muxVarsMissing(w http.ResponseWriter, r *http.Request, params ...string) error {
missing := []string{}
muxvars := mux.Vars(r)
for _, param := range params {
if muxvars[param] == "" {
missing = append(missing, param)
}
}
if len(missing) > 0 {
return PiErrorf(http.StatusBadRequest, "Missing parameters : %s", missing)
}
return nil
}
func validateURLFormat(urlStr string) error {
u, err := url.Parse(urlStr)
if err != nil {
return err
}
if !u.IsAbs() {
return fmt.Errorf("Requires an absolute URL")
}
return nil
}
func validateURLFormatOrEmpty(urlStr string) error {
if urlStr == "" {
return nil
}
return validateURLFormat(urlStr)
}
/*
func logIf(err error, logfunc func(format string, args ...interface{}), when string) {
if err != nil {
logfunc("Problem on %v: %v", when, err.Error())
}
}
*/
func logIf(err error, logfunc func(ctx context.Context, format string, args ...interface{}), ctx context.Context, when string) {
if err != nil {
logfunc(ctx, "Problem on %v: %v", when, err.Error())
}
}