forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmockSiteRouter.tsx
525 lines (471 loc) · 16.2 KB
/
mockSiteRouter.tsx
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import express, { Router } from "express"
import path from "path"
import {
renderFrontPage,
renderGdocsPageBySlug,
renderPageBySlug,
renderChartsPage,
renderMenuJson,
renderSearchPage,
renderDonatePage,
entriesByYearPage,
makeAtomFeed,
feedbackPage,
renderNotFoundPage,
renderBlogByPageNum,
countryProfileCountryPage,
renderExplorerPage,
makeAtomFeedNoTopicPages,
renderDynamicCollectionPage,
renderTopChartsCollectionPage,
renderDataInsightsIndexPage,
renderThankYouPage,
makeDataInsightsAtomFeed,
} from "../baker/siteRenderers.js"
import {
BAKED_BASE_URL,
BASE_DIR,
BAKED_SITE_DIR,
LEGACY_WORDPRESS_IMAGE_URL,
} from "../settings/serverSettings.js"
import { expectInt, renderToHtmlPage } from "../serverUtils/serverUtil.js"
import {
countryProfilePage,
countriesIndexPage,
} from "../baker/countryProfiles.js"
import { makeSitemap } from "../baker/sitemap.js"
import {
getChartConfigBySlug,
getChartVariableData,
} from "../db/model/Chart.js"
import { countryProfileSpecs } from "../site/countryProfileProjects.js"
import { ExplorerAdminServer } from "../explorerAdminServer/ExplorerAdminServer.js"
import { grapherToSVG } from "../baker/GrapherImageBaker.js"
import { getVariableData, getVariableMetadata } from "../db/model/Variable.js"
import { MultiEmbedderTestPage } from "../site/multiembedder/MultiEmbedderTestPage.js"
import { JsonError } from "@ourworldindata/utils"
import { GIT_CMS_DIR } from "../gitCms/GitCmsConstants.js"
import { EXPLORERS_ROUTE_FOLDER } from "../explorer/ExplorerConstants.js"
import { getExplorerRedirectForPath } from "../explorerAdminServer/ExplorerRedirects.js"
import { explorerUrlMigrationsById } from "../explorer/urlMigrations/ExplorerUrlMigrations.js"
import { generateEmbedSnippet } from "../site/viteUtils.js"
import {
renderPreviewDataPageOrGrapherPage,
renderDataPageV2,
} from "../baker/GrapherBaker.js"
import { GdocPost } from "../db/model/Gdoc/GdocPost.js"
import { GdocDataInsight } from "../db/model/Gdoc/GdocDataInsight.js"
import * as db from "../db/db.js"
import { calculateDataInsightIndexPageCount } from "../db/model/Gdoc/gdocUtils.js"
import {
getPlainRouteNonIdempotentWithRWTransaction,
getPlainRouteWithROTransaction,
} from "./plainRouterHelpers.js"
import { DEFAULT_LOCAL_BAKE_DIR } from "../site/SiteConstants.js"
import { DATA_INSIGHTS_ATOM_FEED_NAME } from "../site/gdocs/utils.js"
require("express-async-errors")
// todo: switch to an object literal where the key is the path and the value is the request handler? easier to test, reflect on, and manipulate
const mockSiteRouter = Router()
mockSiteRouter.use(express.urlencoded({ extended: true }))
mockSiteRouter.use(express.json())
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/atom.xml",
async (req, res, trx) => {
res.set("Content-Type", "application/xml")
const atomFeed = await makeAtomFeed(trx)
res.send(atomFeed)
}
)
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/atom-no-topic-pages.xml",
async (req, res, trx) => {
res.set("Content-Type", "application/xml")
const atomFeedNoTopicPages = await makeAtomFeedNoTopicPages(trx)
res.send(atomFeedNoTopicPages)
}
)
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
`/${DATA_INSIGHTS_ATOM_FEED_NAME}`,
async (_, res) => {
res.set("Content-Type", "application/xml")
const atomFeedDataInsights = await db.knexReadonlyTransaction((knex) =>
makeDataInsightsAtomFeed(knex)
)
res.send(atomFeedDataInsights)
}
)
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/sitemap.xml",
async (req, res, trx) => {
res.set("Content-Type", "application/xml")
const sitemap = await makeSitemap(explorerAdminServer, trx)
res.send(sitemap)
}
)
getPlainRouteWithROTransaction(
mockSiteRouter,
"/entries-by-year",
async (req, res, trx) => res.send(await entriesByYearPage(trx))
)
getPlainRouteWithROTransaction(
mockSiteRouter,
`/entries-by-year/:year`,
async (req, res, trx) =>
res.send(await entriesByYearPage(trx, parseInt(req.params.year)))
)
mockSiteRouter.get(
"/grapher/data/variables/data/:variableId.json",
async (req, res) => {
res.set("Access-Control-Allow-Origin", "*")
res.json((await getVariableData(expectInt(req.params.variableId))).data)
}
)
mockSiteRouter.get(
"/grapher/data/variables/metadata/:variableId.json",
async (req, res) => {
res.set("Access-Control-Allow-Origin", "*")
const variableData = await getVariableData(
expectInt(req.params.variableId)
)
res.json(variableData.metadata)
}
)
mockSiteRouter.get("/assets/embedCharts.js", async (req, res) => {
res.contentType("text/javascript").send(generateEmbedSnippet())
})
const explorerAdminServer = new ExplorerAdminServer(GIT_CMS_DIR)
getPlainRouteWithROTransaction(
mockSiteRouter,
`/${EXPLORERS_ROUTE_FOLDER}/:slug`,
async (req, res, trx) => {
res.set("Access-Control-Allow-Origin", "*")
const explorers = await explorerAdminServer.getAllPublishedExplorers()
const explorerProgram = explorers.find(
(program) => program.slug === req.params.slug
)
if (explorerProgram) {
const explorerPage = await renderExplorerPage(explorerProgram, trx)
res.send(explorerPage)
} else
throw new JsonError(
"A published explorer with that slug was not found",
404
)
}
)
getPlainRouteWithROTransaction(
mockSiteRouter,
"/*",
async (req, res, trx, next) => {
const explorerRedirect = getExplorerRedirectForPath(req.path)
// If no explorer redirect exists, continue to next express handler
if (!explorerRedirect) return next!()
const { migrationId, baseQueryStr } = explorerRedirect
const { explorerSlug } = explorerUrlMigrationsById[migrationId]
const program =
await explorerAdminServer.getExplorerFromSlug(explorerSlug)
const explorerPage = await renderExplorerPage(program, trx, {
explorerUrlMigrationId: migrationId,
baseQueryStr,
})
res.send(explorerPage)
}
)
getPlainRouteWithROTransaction(
mockSiteRouter,
"/collection/top-charts",
async (_, res, trx) => {
return res.send(await renderTopChartsCollectionPage(trx))
}
)
mockSiteRouter.get("/collection/custom", async (_, res) => {
return res.send(await renderDynamicCollectionPage())
})
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/grapher/:slug",
async (req, res, trx) => {
const entity = await getChartConfigBySlug(trx, req.params.slug)
if (!entity) throw new JsonError("No such chart", 404)
// XXX add dev-prod parity for this
res.set("Access-Control-Allow-Origin", "*")
const previewDataPageOrGrapherPage =
await renderPreviewDataPageOrGrapherPage(entity.config, trx)
res.send(previewDataPageOrGrapherPage)
}
)
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/",
async (req, res, trx) => {
const frontPage = await renderFrontPage(trx)
res.send(frontPage)
}
)
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/donate",
async (req, res, trx) => res.send(await renderDonatePage(trx))
)
mockSiteRouter.get("/thank-you", async (req, res) =>
res.send(await renderThankYouPage())
)
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/data-insights/:pageNumberOrSlug?",
async (req, res, trx) => {
const totalPageCount = calculateDataInsightIndexPageCount(
await db
.getPublishedDataInsights(trx)
.then((insights) => insights.length)
)
async function renderIndexPage(pageNumber: number) {
const dataInsights = await GdocDataInsight.getPublishedDataInsights(
trx,
pageNumber
)
// calling fetchImageMetadata 20 times makes me sad, would be nice if we could cache this
await Promise.all(
dataInsights.map((insight) => insight.loadState(trx))
)
return renderDataInsightsIndexPage(
dataInsights,
pageNumber,
totalPageCount,
true
)
}
const pageNumberOrSlug = req.params.pageNumberOrSlug
if (!pageNumberOrSlug) {
return res.send(await renderIndexPage(0))
}
// pageNumber is 1-indexed, but DB operations are 0-indexed
const pageNumber = parseInt(pageNumberOrSlug) - 1
if (!isNaN(pageNumber)) {
if (pageNumber <= 0 || pageNumber >= totalPageCount) {
return res.redirect("/data-insights")
}
return res.send(await renderIndexPage(pageNumber))
}
const slug = pageNumberOrSlug
try {
return res.send(await renderGdocsPageBySlug(trx, slug, true))
} catch (e) {
console.error(e)
}
return new JsonError(`Data insight with slug "${slug}" not found`, 404)
}
)
getPlainRouteWithROTransaction(
mockSiteRouter,
"/charts",
async (req, res, trx) => {
const explorerAdminServer = new ExplorerAdminServer(GIT_CMS_DIR)
res.send(await renderChartsPage(trx, explorerAdminServer))
}
)
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/datapage-preview/:id",
async (req, res, trx) => {
const variableId = expectInt(req.params.id)
const variableMetadata = await getVariableMetadata(variableId)
res.send(
await renderDataPageV2(
{
variableId,
variableMetadata,
isPreviewing: true,
useIndicatorGrapherConfigs: true,
},
trx
)
)
}
)
countryProfileSpecs.forEach((spec) =>
getPlainRouteWithROTransaction(
mockSiteRouter,
`/${spec.rootPath}/:countrySlug`,
async (req, res, trx) => {
const countryPage = await countryProfileCountryPage(
spec,
req.params.countrySlug,
trx
)
res.send(countryPage)
}
)
)
mockSiteRouter.get("/search", async (req, res) =>
res.send(await renderSearchPage())
)
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/latest",
async (req, res, trx) => {
const latest = await renderBlogByPageNum(1, trx)
res.send(latest)
}
)
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/latest/page/:pageno",
async (req, res, trx) => {
const pagenum = parseInt(req.params.pageno, 10)
if (!isNaN(pagenum)) {
const latestPageNum = await renderBlogByPageNum(
isNaN(pagenum) ? 1 : pagenum,
trx
)
res.send(latestPageNum)
} else throw new Error("invalid page number")
}
)
mockSiteRouter.get("/headerMenu.json", async (req, res) => {
res.contentType("application/json")
res.send(await renderMenuJson())
})
mockSiteRouter.use(
"/images/published",
express.static(path.join(DEFAULT_LOCAL_BAKE_DIR, "images/published"), {
fallthrough: false,
})
)
mockSiteRouter.use(
// Not all /app/uploads paths are going through formatting
// and being rewritten as /uploads. E.g. blog index images paths
// on front page.
["/uploads", "/app/uploads"],
(req, res) => {
const assetPath = req.path.replace(/^\/(app\/)?uploads\//, "")
// Ensure no trailing slash on the base URL and manage the slash between URLs safely
const baseUrl = LEGACY_WORDPRESS_IMAGE_URL.replace(/\/+$/, "")
const path = assetPath.replace(/^\/+/, "")
const assetUrl = `${baseUrl}/${path}`
res.redirect(assetUrl)
}
)
mockSiteRouter.use(
"/exports",
express.static(path.join(BAKED_SITE_DIR, "exports"))
)
mockSiteRouter.use("/assets", express.static("dist/assets"))
// TODO: this used to be a mockSiteRouter.use call but otherwise it looked like a route and
// it didn't look like it was making use of any middleware - if this causese issues then
// this has to be reverted to a use call
getPlainRouteWithROTransaction(
mockSiteRouter,
"/grapher/exports/:slug.svg",
async (req, res, trx) => {
const grapher = await getChartConfigBySlug(trx, req.params.slug)
const vardata = await getChartVariableData(grapher.config)
res.setHeader("Content-Type", "image/svg+xml")
res.send(await grapherToSVG(grapher.config, vardata))
}
)
mockSiteRouter.use(
"/fonts",
express.static(path.join(BASE_DIR, "public/fonts"), {
setHeaders: (res) => {
res.set("Access-Control-Allow-Origin", "*")
},
})
)
mockSiteRouter.use("/", express.static(path.join(BASE_DIR, "public")))
mockSiteRouter.get("/countries", async (req, res) =>
res.send(await countriesIndexPage(BAKED_BASE_URL))
)
getPlainRouteWithROTransaction(
mockSiteRouter,
"/country/:countrySlug",
async (req, res, trx) =>
res.send(
await countryProfilePage(
trx,
req.params.countrySlug,
BAKED_BASE_URL
)
)
)
mockSiteRouter.get("/feedback", async (req, res) =>
res.send(await feedbackPage())
)
mockSiteRouter.get("/multiEmbedderTest", async (req, res) =>
res.send(
renderToHtmlPage(
MultiEmbedderTestPage(req.query.globalEntitySelector === "true")
)
)
)
getPlainRouteWithROTransaction(
mockSiteRouter,
"/dods.json",
async (_, res, trx) => {
res.set("Access-Control-Allow-Origin", "*")
const { details, parseErrors } =
await GdocPost.getDetailsOnDemandGdoc(trx)
if (parseErrors.length) {
console.error(
`Error(s) parsing details: ${parseErrors
.map((e) => e.message)
.join(", ")}`
)
}
res.send(details)
}
)
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/team/:authorSlug",
async (req, res, trx) => {
try {
// We assume here that author slugs are unique across all gdocs (not
// just author gdocs)
const page = await renderGdocsPageBySlug(trx, req.params.authorSlug)
res.send(page)
return
} catch (e) {
console.error(e)
res.status(404).send(renderNotFoundPage())
}
}
)
// TODO: this transaction is only RW because somewhere inside it we fetch images
getPlainRouteNonIdempotentWithRWTransaction(
mockSiteRouter,
"/*",
async (req, res, trx) => {
const slug = req.path.replace(/^\//, "")
try {
const page = await renderGdocsPageBySlug(trx, slug)
res.send(page)
return
} catch (e) {
console.error(e)
}
try {
const page = await renderPageBySlug(slug, trx)
res.send(page)
} catch (e) {
console.error(e)
res.status(404).send(renderNotFoundPage())
}
}
)
export { mockSiteRouter }