Skip to content

Commit 23876b3

Browse files
committed
removed caching from zeit, added table in by country api too
1 parent 7a77b4a commit 23876b3

File tree

6 files changed

+71
-26
lines changed

6 files changed

+71
-26
lines changed

app.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,40 @@ function errorHandler(error, res) {
1616
}
1717

1818
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
19+
app.use((req, res, next) => {
20+
res.setHeader('Cache-Control', 'no-cache');
21+
next();
22+
});
1923

2024
app.get('/', (req, res) => {
21-
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null
25+
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
2226
const format = req.query.format ? req.query.format : '';
2327

2428
if (format.toLowerCase() === 'json') {
2529
return getJSONData().then(result => {
26-
res.setHeader('Cache-Control', 's-maxage=900');
2730
return res.json(result);
2831
}).catch(error => errorHandler(error, res));
2932
}
3033

31-
return getCompleteTable({isCurl}).then(result => {
32-
res.setHeader('Cache-Control', 's-maxage=900');
34+
return getCompleteTable({ isCurl }).then(result => {
3335
return res.send(result);
3436
}).catch(error => errorHandler(error, res));
3537
});
3638

3739
app.get('/:country', (req, res) => {
3840
const { country } = req.params;
41+
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
3942
const format = req.query.format ? req.query.format : '';
4043

4144
if (!country || 'ALL' === country.toUpperCase()) {
4245
if (format.toLowerCase() === 'json') {
4346
return getJSONData().then(result => {
44-
res.setHeader('Cache-Control', 's-maxage=900');
4547
return res.json(result);
4648
}).catch(error => errorHandler(error, res));
4749
}
4850

4951

50-
return getCompleteTable().then(result => {
51-
res.setHeader('Cache-Control', 's-maxage=900');
52+
return getCompleteTable({ isCurl }).then(result => {
5253
return res.send(result);
5354
}).catch(error => errorHandler(error, res));
5455
}
@@ -70,13 +71,11 @@ app.get('/:country', (req, res) => {
7071

7172
if (format.toLowerCase() === 'json') {
7273
return getJSONDataForCountry(iso2).then(result => {
73-
res.setHeader('Cache-Control', 's-maxage=900');
7474
return res.json(result);
7575
}).catch(error => errorHandler(error, res));
7676
}
7777

78-
return getCountryTable(iso2).then(result => {
79-
res.setHeader('Cache-Control', 's-maxage=900');
78+
return getCountryTable({ countryCode: iso2, isCurl }).then(result => {
8079
return res.send(result);
8180
}).catch(error => errorHandler(error, res));
8281
});

bin/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#!/usr/bin/env node
2-
const yargonaut = require('yargonaut')
3-
.style('green');
2+
const yargonaut = require('yargonaut').style('green');
43
const yargs = require('yargs');
54
const chalk = require('chalk');
65
const { getCompleteTable } = require('../lib/corona');
76
const { getCountryTable } = require('../lib/byCountry');
87
const { lookupCountry } = require('../lib/helpers');
98

109
const { argv } = yargs
11-
.command('$0 [country]','Tool to COVID-19 statistics for the world or the given country', yargs =>
10+
.command('$0 [country]','Tool to track COVID-19 statistics for the world or the given country', yargs =>
1211
yargs.positional('country', {
1312
coerce(arg) {
1413
if ('ALL' === arg.toUpperCase()) {
@@ -51,7 +50,7 @@ const { emojis, country } = argv;
5150
(
5251
country === 'ALL'
5352
? getCompleteTable({emojis})
54-
: getCountryTable(country, emojis)
53+
: getCountryTable({ countryCode: country, emojis })
5554
)
5655
.then(console.log)
5756
.catch(console.error);

lib/byCountry.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const _ = require('lodash');
44
const chalk = require('chalk');
55
const helpers = require('./helpers');
66
const api = require('./api');
7+
const stripAnsi = require('strip-ansi');
78
const {
89
getCountry,
910
getConfirmed,
@@ -79,7 +80,7 @@ exports.getJSONDataForCountry = async (countryCode) => {
7980
return countryData;
8081
}
8182

82-
exports.getCountryTable = async (countryCode, emojis = false) => {
83+
exports.getCountryTable = async ({countryCode, emojis = false, isCurl = true}) => {
8384
const head = [
8485
'',
8586
'State',
@@ -147,5 +148,30 @@ exports.getCountryTable = async (countryCode, emojis = false) => {
147148
});
148149
}
149150
const lastUpdated = countryData[0].lastUpdated;
151+
if (!isCurl) {
152+
const template = `<!DOCTYPE html>
153+
<html lang="en">
154+
<head>
155+
<meta charset="UTF-8">
156+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
157+
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono&display=swap" rel="stylesheet">
158+
<title>Coronavirus Tracker</title>
159+
<style>
160+
body {
161+
background: #0D0208;
162+
color: #00FF41;
163+
}
164+
pre {
165+
font-family: 'Roboto Mono', monospace;
166+
white-space: pre;
167+
}
168+
</style>
169+
</head>
170+
<body>
171+
<pre>${table.toString() + footer(lastUpdated)}</pre>
172+
</body>
173+
</html>`;
174+
return stripAnsi(template);
175+
}
150176
return table.toString() + footer(lastUpdated);
151177
}

lib/corona.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const _ = require('lodash');
44
const chalk = require('chalk');
55
const helpers = require('./helpers');
66
const api = require('./api');
7+
const stripAnsi = require('strip-ansi');
78
const {
89
getCountry,
910
getConfirmed,
@@ -79,7 +80,7 @@ function extraStats(dataArr) {
7980
);
8081
}
8182

82-
exports.getCompleteTable = async ({isCurl, emojis = false}) => {
83+
exports.getCompleteTable = async ({isCurl = true, emojis = false}) => {
8384
const head = [
8485
'',
8586
'Country',
@@ -148,6 +149,10 @@ exports.getCompleteTable = async ({isCurl, emojis = false}) => {
148149
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono&display=swap" rel="stylesheet">
149150
<title>Coronavirus Tracker</title>
150151
<style>
152+
body {
153+
background: #0D0208;
154+
color: #00FF41;
155+
}
151156
pre {
152157
font-family: 'Roboto Mono', monospace;
153158
white-space: pre;
@@ -158,7 +163,7 @@ exports.getCompleteTable = async ({isCurl, emojis = false}) => {
158163
<pre>${table.toString() + footer(lastUpdated)}</pre>
159164
</body>
160165
</html>`
161-
return template;
166+
return stripAnsi(template);
162167
}
163168
return table.toString() + footer(lastUpdated);
164169
}

package-lock.json

Lines changed: 24 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"moment": "^2.24.0",
3030
"morgan": "^1.9.1",
3131
"node-cache": "^5.1.0",
32+
"strip-ansi": "^6.0.0",
3233
"yargonaut": "^1.1.4",
3334
"yargs": "15.3.1"
3435
},

0 commit comments

Comments
 (0)