This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
server.js
397 lines (361 loc) · 12.1 KB
/
server.js
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
/* eslint-disable @typescript-eslint/no-var-requires */
const express = require('express');
const next = require('next');
const dotenv = require('dotenv');
const compression = require('compression');
const cookiesMiddleware = require('universal-cookie-express');
const expressSitemapXml = require('express-sitemap-xml');
const fs = require('fs-extra');
const secure = require('express-force-https');
const morgan = require('morgan');
const basicAuth = require('express-basic-auth');
const request = require('request-promise');
const keyBy = require('lodash/keyBy');
const { createMiddleware: createPrometheusMiddleware } = require('@promster/express');
const { createServer } = require('@promster/server');
const dev = process.env.NODE_ENV !== 'production';
if (dev) {
dotenv.config();
}
const Cache = require('./common/lib/cache');
const {
getApps,
getAppMiningMonths,
getRankedBlockstackApps
} = require('./common/lib/api');
const getSitemapURLs = require('./common/lib/sitemap');
const RSSController = require('./common/controllers/rss-controller');
const slugify = require('./common/lib/slugify');
const AppsAggregator = require('./common/lib/aggregators/apps');
const MiningMonths = require('./common/lib/aggregators/mining-months');
const RankedApps = require('./common/lib/aggregators/ranked-apps');
const MiningApps = require('./common/lib/aggregators/mining-apps');
const port = parseInt(process.env.PORT, 10) || 3000;
const app = next({ dev });
const handle = app.getRequestHandler();
const apiServer = process.env.API_SERVER || 'https://api.app.co';
const pageCacheKey = req => {
// return `req`
const { path } = req;
const isAdmin = path.indexOf('admin') !== -1;
const isMaker = path.indexOf('maker') !== -1;
const isSubmit = path.indexOf('submit') !== -1;
if (isAdmin || isMaker || isSubmit) {
return null;
}
return `page-cache-key-${path}`;
};
async function renderAndCache(req, res, pagePath, serverData) {
try {
const cacheKey = pageCacheKey(req);
const USE_CACHE = true;
if (USE_CACHE && cacheKey && (await Cache.has(cacheKey)) && !dev) {
console.log('Cache hit:', req.path);
res.setHeader('x-cache', 'HIT');
return res.send(await Cache.getAsync(cacheKey));
} else if (!dev) {
console.log('Cache miss:', req.path);
}
const [
data,
blockstackRankedApps,
appMiningMonths,
appMiningApps
] = await Promise.all([
getApps(),
getRankedBlockstackApps(),
getAppMiningMonths(),
MiningApps.fetch()
]);
data.apiServer = apiServer;
const dataToPass = {
...data,
...serverData,
blockstackRankedApps,
appMiningMonths,
appMiningApps,
params: req.params
};
//
// Handle Blockstack signin request
const authResponseToken = req.query.authResponse;
if (authResponseToken) {
const authUrl = `${apiServer}/api/authenticate?authToken=${authResponseToken}`;
const authResp = await request({
method: 'POST',
uri: authUrl,
json: true
});
if (authResp.success) {
dataToPass.user = authResp;
req.universalCookies.set('jwt', authResp.token);
}
}
if (req.universalCookies.get('jwt')) {
const uri = `${apiServer}/api/maker/apps`;
const json = await request({
uri,
json: true,
auth: {
bearer: req.universalCookies.get('jwt')
}
});
dataToPass.maker = {
appIds: json.apps.map(_app => _app.id),
appEntities: keyBy(json.apps, 'id'),
loading: false
};
if (req.params.appId) {
dataToPass.maker.selectedAppId = parseInt(req.params.appId, 10);
}
}
const html = await app.renderToHTML(req, res, pagePath, dataToPass);
if (cacheKey) {
try {
await Cache.setAsync(cacheKey, html, 'EX', 60 * 60);
} catch (error) {
console.error('Error when saving page cache');
console.error(error);
}
}
return res.send(html);
} catch (err) {
console.error(err);
return app.renderError(err, req, res, pagePath);
}
}
const oldConsoleError = console.error;
const oldConsoleWarn = console.warn;
const checkMatch = (args, match) => {
if (!args[0] || !args[0].indexOf) return false;
return args[0].indexOf(match) !== -1;
};
console.error = (...args) => {
if (checkMatch(args, 'Warning: React does not recognize')) return;
if (checkMatch(args, 'Warning: Failed prop type')) return;
if (checkMatch(args, 'for a non-boolean attribute')) return;
if (checkMatch(args, 'Warning: Invalid value for prop')) return;
if (checkMatch(args, 'Failed to retrieve initialize state from localStorage'))
return;
if (checkMatch(args, 'Unable to persist state to localStorage')) return;
oldConsoleError(...args);
};
console.warn = (...args) => {
if (checkMatch(args, 'Failed to retrieve initialize state from localStorage'))
return;
if (checkMatch(args, 'Unable to persist state to localStorage')) return;
oldConsoleWarn(...args);
};
app.prepare().then(() => {
getApps(apiServer).then((apps) => {
const server = express();
server.use(createPrometheusMiddleware({ app: server }));
// Create `/metrics` endpoint on separate server
if (!dev) {
createServer({ port: 9153 }).then(() => console.log(`@promster/server started on port 9153.`));
}
if (!dev) {
server.use(secure);
server.use(morgan('combined'));
}
server.use(cookiesMiddleware());
server.use(compression());
if (process.env.AUTH_PASSWORD) {
server.use(
basicAuth({
users: { admin: process.env.AUTH_PASSWORD },
challenge: true
})
);
}
server.set('views', './common/server-views');
server.set('view engine', 'pug');
server.use((req, res, _next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', '*');
_next();
});
server.get('/', (req, res) => renderAndCache(req, res, '/'));
/**
* Mining Pages
*/
server.get('/mining', (req, res) => renderAndCache(req, res, '/mining'));
server.get('/mining/apps', (req, res) =>
renderAndCache(req, res, '/mining/apps')
);
server.get('/mining/faq', (req, res) =>
renderAndCache(req, res, '/mining/faq')
);
server.get('/mining/terms', (req, res) =>
renderAndCache(req, res, '/mining/terms')
);
server.get('/mining/privacy', (req, res) =>
renderAndCache(req, res, '/mining/privacy')
);
server.get('/mining/developer-instructions', (req, res) =>
renderAndCache(req, res, '/mining/developer-instructions')
);
server.get('/mining/reviewer-instructions', (req, res) =>
renderAndCache(req, res, '/mining/reviewer-instructions')
);
server.get('/mining/latest', async (req, res) => {
try {
const months = await getAppMiningMonths(apiServer);
const latest = months[months.length - 1];
const slug = slugify(latest.humanReadableDate);
res.redirect(`/mining/${slug}`);
} catch (error) {
console.error(error);
res.redirect('/mining');
}
});
server.get('/mining/:date', (req, res) => {
const { date } = req.params;
const [month, year] = date.split('-');
renderAndCache(req, res, '/mining/month-results', {
month,
year,
fetchMiningResults: true
});
});
/**
* App Pages
*/
server.get('/app/:appSlug', (req, res) => renderAndCache(req, res, '/'));
server.get('/apps', (req, res) => renderAndCache(req, res, '/'));
/**
* Platforms
*/
server.get('/platforms', (req, res) =>
renderAndCache(req, res, '/platforms')
);
server.get('/platforms/all', (req, res) =>
renderAndCache(req, res, '/platforms')
);
// redirect to top-level page
server.get('/platforms/:platform', (req, res) =>
res.redirect(`/${req.params.platform}`)
);
server.get('/platform/:platform', (req, res) =>
res.redirect(`/${req.params.platform}`)
);
/**
* Categories
*/
server.get('/categories', (req, res) =>
renderAndCache(req, res, '/categories')
);
server.get('/categories/all', (req, res) =>
renderAndCache(req, res, '/categories/all')
);
server.get('/categories/:category', (req, res) =>
renderAndCache(req, res, '/categories', { category: req.params.category })
);
server.get('/category/:category', (req, res) =>
res.redirect(`/categories/${req.params.category}`)
);
/**
* General Pages
*/
server.get('/faq', (req, res) => renderAndCache(req, res, '/faq'));
server.get('/submit', (req, res) => res.redirect('/submit-your-app'));
server.get('/submit-your-app', (req, res) =>
renderAndCache(req, res, '/submit-your-app')
);
server.get('/all', (req, res) => renderAndCache(req, res, '/all'));
server.get('/terms', (req, res) => renderAndCache(req, res, '/terms'));
server.get('/privacy', (req, res) => renderAndCache(req, res, '/privacy'));
/**
* Admin Pages
*/
server.get('/admin', (req, res) => renderAndCache(req, res, '/admin'));
server.get('/admin/app', (req, res) =>
renderAndCache(req, res, '/admin/app')
);
server.get('/admin/pending', (req, res) =>
renderAndCache(req, res, '/admin/pending')
);
server.get('/admin/mining/months', (req, res) =>
renderAndCache(req, res, '/admin/mining/months')
);
server.get('/admin/mining/months/:id', (req, res) =>
renderAndCache(req, res, '/admin/mining/month', {
monthId: req.params.id
})
);
server.get('/admin/mining/months/:id/upload-report', (req, res) =>
renderAndCache(req, res, '/admin/mining/upload-report', {
monthId: req.params.id
})
);
server.get(
'/admin/mining/months/:monthId/reviewers/:reviewerId',
(req, res) => renderAndCache(req, res, '/admin/mining/reviewer')
);
/**
* Maker pages
*/
server.get('/maker', (req, res) => res.redirect('maker/apps'));
server.get('/maker/apps', (req, res) =>
renderAndCache(req, res, '/maker/apps')
);
server.get('/maker/apps/blockstack-only', (req, res) =>
renderAndCache(req, res, '/maker/apps/blockstack-only')
);
server.get('/maker/apps/:appId', (req, res) =>
renderAndCache(req, res, '/maker/apps/:appId')
);
server.get('/maker/:accessToken', (req, res) =>
renderAndCache(req, res, '/maker/magic-link', {
accessToken: req.params.accessToken
})
);
server.get('/blockstack', (req, res) => {
res.redirect('https://www2.blockstack.org/apps');
});
apps.platforms.forEach(platform => {
server.get(`/${slugify(platform)}`, (req, res) => {
req.params.platform = platform;
return renderAndCache(req, res, '/platforms', { platform });
});
});
server.get('/clear-cache', async (req, res) => {
if (req.query.key === process.env.API_KEY) {
console.log('Clearing cache from API');
try {
await Promise.all([
AppsAggregator.set(),
MiningMonths.set(),
MiningApps.set()
]);
await RankedApps.set();
await Cache.reset();
res.json({ success: true });
} catch (error) {
console.error(error);
res.status(500).json({ success: false });
}
} else {
res.status(400).json({ success: false });
}
});
server.use('/rss', RSSController);
server.use(expressSitemapXml(getSitemapURLs(apiServer), 'https://app.co'));
server.get('/status', (_req, res) => {
res.json({ success: true });
});
server.get('/robots.txt', async (req, res) => {
const robotsFile =
dev || process.env.STAGING ? 'robots.staging.txt' : 'robots.txt';
const robotsPath = `./static/${robotsFile}`;
const robots = await fs.readFile(robotsPath);
res.header('Content-Type', 'text/plain');
res.status(200).send(robots);
});
server.get('*', (req, res) => handle(req, res));
server.listen(port, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});
});