forked from sagarkarira/coronavirus-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
203 lines (170 loc) · 5.96 KB
/
app.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
const express = require('express');
const helmet = require('helmet');
const morgan = require('morgan');
const chalk = require('chalk');
const {
getCountryTable,
getJSONData,
getJSONDataForCountry,
} = require('./lib/byCountry');
const { getCompleteTable, getGraph } = require('./lib/corona');
const { lookupCountry, htmlTemplate, footer } = require('./lib/helpers');
const { getLiveUpdates } = require('./lib/reddit.js');
const { getWorldoMetersTable } = require('./lib/worldoMeters.js');
const { getUsaStats } = require('./lib/country/us.js');
const { helpContent, countryNotFound, stateCountryNotFound } = require('./lib/constants');
const app = express();
const port = process.env.PORT || 3001;
const IS_CURL_RE = /\bcurl\b/im;
function errorHandler(error, req, res) {
console.error(error);
const body = `
I am sorry. Something went wrong. Please report it\n
${error.message}
${footer(new Date)}
`;
if (req.isCurl) {
return body;
}
return res.status(500).send(htmlTemplate(body));
}
app.set('json escape', true);
app.use(helmet({
dnsPrefetchControl: false,
frameguard: {
action: 'deny'
}
}));
app.use(helmet.hsts({
force: true,
includeSubDomains: true,
maxAge: 63072000, // 2 years
preload: true
}));
app.use(helmet.referrerPolicy({ policy: 'strict-origin-when-cross-origin' }));
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
app.use((req, res, next) => {
res.setHeader('Cache-Control', 'no-cache');
req.isCurl = IS_CURL_RE.test(req.headers['user-agent']);
next();
});
app.get('/', (req, res) => {
const isCurl = req.isCurl;
const format = req.query.format ? req.query.format : '';
const minimal = req.query.minimal === 'true';
const emojis = req.query.emojis === 'true';
const top = req.query.top ? Number(req.query.top) : 1000;
const source = req.query.source ? Number(req.query.source) : 2;
if (source === 1) {
if (format.toLowerCase() === 'json') {
return getJSONData().then(result => {
return res.json(result);
}).catch(error => errorHandler(error, req, res));
}
return getCompleteTable({ isCurl, emojis, minimal, top })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, req, res));
}
return getWorldoMetersTable({ isCurl, emojis, minimal, top, format})
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, req, res));
});
app.get('/updates', (req, res) => {
const isCurl = req.isCurl;
const format = req.query.format ? req.query.format : '';
if (format.toLowerCase() === 'json') {
return getLiveUpdates({ json: true, isCurl }).then(result => {
return res.json(result);
}).catch(error => errorHandler(error, req, res));
}
return getLiveUpdates({ json: false, isCurl }).then(result => {
return res.send(result);
}).catch(error => errorHandler(error, req, res));
});
app.get(['/:country/graph', '/graph'], (req, res) => {
const { country } = req.params;
const isCurl = req.isCurl;
if (!country) {
return getGraph({ isCurl })
.then(result => res.send(result))
.catch(error => errorHandler(error, req, res));
}
const lookupObj = lookupCountry(country);
if (!lookupObj) {
return res.status(404).send(countryNotFound(isCurl));
}
return getGraph({countryCode: lookupObj.iso2, isCurl })
.then(result => res.send(result))
.catch(error => errorHandler(error, req, res));
});
app.get('/help', (req, res) => {
const isCurl = req.isCurl;
if (!isCurl) {
return res.send(htmlTemplate(helpContent));
}
return res.send(chalk.green(helpContent));
});
app.get('/states/:country', (req, res) => {
const { country } = req.params;
const isCurl = req.isCurl;
const format = req.query.format ? req.query.format : '';
const minimal = req.query.minimal === 'true';
const top = req.query.top ? Number(req.query.top) : 1000;
const lookupObj = lookupCountry(country);
if (!lookupObj) {
return res.status(404).send(stateCountryNotFound(isCurl));
}
if (lookupObj.iso2 === 'US') {
return getUsaStats({ isCurl, minimal, top, format})
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, req, res));
}
});
app.get('/:country', (req, res) => {
const { country } = req.params;
const isCurl = req.isCurl;
const format = req.query.format ? req.query.format : '';
const minimal = req.query.minimal === 'true';
const emojis = req.query.emojis === 'true';
const source = req.query.source ? Number(req.query.source) : 2;
if (!country || country.toUpperCase() === 'ALL') {
if (format.toLowerCase() === 'json') {
return getWorldoMetersTable({ isCurl, emojis, minimal, format }).then(result => {
return res.json(result);
}).catch(error => errorHandler(error, req, res));
}
return getWorldoMetersTable({ isCurl, emojis, minimal })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, req, res));
}
if (source === 1) {
const lookupObj = lookupCountry(country);
if (!lookupObj) {
return res.status(404).send(countryNotFound(isCurl));
}
const { iso2 } = lookupObj;
if (format.toLowerCase() === 'json') {
return getJSONDataForCountry(iso2).then(result => {
return res.json(result);
}).catch(error => errorHandler(error, req, res));
}
return getCountryTable({ countryCode: iso2, isCurl, emojis, minimal })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, req, res));
}
const lookupObj = lookupCountry(country);
if (!lookupObj) {
return res.status(404).send(countryNotFound(isCurl));
}
const { iso2 } = lookupObj;
return getWorldoMetersTable({ countryCode: iso2, isCurl, emojis, minimal, format })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, req, res));
});
app.listen(port, () => console.log(`Running on ${port}`));