-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathshowers.js
85 lines (68 loc) · 2.14 KB
/
showers.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
const express = require('express');
const expressNunjucks = require('express-nunjucks');
const winston = require('winston');
const cloudData = require('./cloud_data');
const { getScriptUrls } = require('./assets');
const logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({ timestamp: true, colorize: true }),
],
});
const app = express();
const isDev = app.get('env') === 'development';
app.enable('trust proxy');
app.set('views', `${__dirname}/templates`);
app.use(express.static('public'));
expressNunjucks(app, {
watch: isDev,
noCache: isDev,
});
app.get('/', (req, res) => {
res.render('index', {
isDev,
scriptUrls: getScriptUrls(),
canonicalUrl: 'https://www.meteorshowers.org/',
// Default to Perseids - Javascript will override to next shower.
showerCloud: cloudData['Perseids'],
cloudDataJson: JSON.stringify(cloudData),
});
});
app.get('/view/:shower', (req, res) => {
let showerId = req.params.shower;
if (showerId.startsWith('iau-')) {
Object.keys(cloudData).map(key => {
const cloud = cloudData[key];
if (showerId.endsWith('-' + cloud.iau_number)) {
showerId = cloud.name;
}
});
}
res.render('index', {
shower: showerId,
isDev,
scriptUrls: getScriptUrls(),
canonicalUrl: `https://www.meteorshowers.org/view/${showerId}`,
showerCloud: cloudData[showerId.replace('-', ' ')],
cloudDataJson: JSON.stringify(cloudData),
});
});
const port = process.env.PORT || 8988;
const server = app.listen(port);
logger.info('NODE_ENV:', process.env.NODE_ENV);
logger.info('Running on port', port);
const gracefulShutdown = function gracefulShutdown() {
logger.info('Received kill signal, shutting down gracefully.');
server.close(() => {
logger.info('Closed out remaining connections.');
process.exit();
});
setTimeout(() => {
logger.error('Could not close connections in time, forcefully shutting down');
process.exit();
}, 10 * 1000);
};
// Listen for TERM signal .e.g. kill
process.on('SIGTERM', gracefulShutdown);
// Listen for INT signal e.g. Ctrl-C
process.on('SIGINT', gracefulShutdown);
module.exports = app;