Skip to content

Commit 66bd918

Browse files
authored
Merge pull request #28 from archcorsair/renderHTMLResponse
Render monospaced table in HTML when not curl request
2 parents f9a0ec7 + 23876b3 commit 66bd918

File tree

7 files changed

+96
-29
lines changed

7 files changed

+96
-29
lines changed

app.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +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) => {
25+
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null;
2126
const format = req.query.format ? req.query.format : '';
2227

2328
if (format.toLowerCase() === 'json') {
2429
return getJSONData().then(result => {
25-
res.setHeader('Cache-Control', 's-maxage=900');
2630
return res.json(result);
2731
}).catch(error => errorHandler(error, res));
2832
}
2933

30-
return getCompleteTable().then(result => {
31-
res.setHeader('Cache-Control', 's-maxage=900');
34+
return getCompleteTable({ isCurl }).then(result => {
3235
return res.send(result);
3336
}).catch(error => errorHandler(error, res));
3437
});
3538

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

4044
if (!country || 'ALL' === country.toUpperCase()) {
4145
if (format.toLowerCase() === 'json') {
4246
return getJSONData().then(result => {
43-
res.setHeader('Cache-Control', 's-maxage=900');
4447
return res.json(result);
4548
}).catch(error => errorHandler(error, res));
4649
}
4750

4851

49-
return getCompleteTable().then(result => {
50-
res.setHeader('Cache-Control', 's-maxage=900');
52+
return getCompleteTable({ isCurl }).then(result => {
5153
return res.send(result);
5254
}).catch(error => errorHandler(error, res));
5355
}
@@ -69,13 +71,11 @@ app.get('/:country', (req, res) => {
6971

7072
if (format.toLowerCase() === 'json') {
7173
return getJSONDataForCountry(iso2).then(result => {
72-
res.setHeader('Cache-Control', 's-maxage=900');
7374
return res.json(result);
7475
}).catch(error => errorHandler(error, res));
7576
}
7677

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

bin/index.js

Lines changed: 4 additions & 5 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()) {
@@ -50,8 +49,8 @@ const { argv } = yargs
5049
const { emojis, country } = argv;
5150
(
5251
country === 'ALL'
53-
? getCompleteTable(emojis)
54-
: getCountryTable(country, emojis)
52+
? getCompleteTable({emojis})
53+
: getCountryTable({ countryCode: country, emojis })
5554
)
5655
.then(console.log)
5756
.catch(console.error);

lib/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const NodeCache = require( "node-cache" );
1+
const NodeCache = require('node-cache');
22
const axios = require('axios');
33
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 600 } );
44
const CORONA_ALL_KEY = 'coronaAll';

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: 28 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 (emojis = false) => {
83+
exports.getCompleteTable = async ({isCurl = true, emojis = false}) => {
8384
const head = [
8485
'',
8586
'Country',
@@ -139,5 +140,30 @@ exports.getCompleteTable = async (emojis = false) => {
139140
table.push({ [rank++]: values })
140141
});
141142
const lastUpdated = countryData[0].lastUpdated;
143+
if (!isCurl) {
144+
const template = `<!DOCTYPE html>
145+
<html lang="en">
146+
<head>
147+
<meta charset="UTF-8">
148+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
149+
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono&display=swap" rel="stylesheet">
150+
<title>Coronavirus Tracker</title>
151+
<style>
152+
body {
153+
background: #0D0208;
154+
color: #00FF41;
155+
}
156+
pre {
157+
font-family: 'Roboto Mono', monospace;
158+
white-space: pre;
159+
}
160+
</style>
161+
</head>
162+
<body>
163+
<pre>${table.toString() + footer(lastUpdated)}</pre>
164+
</body>
165+
</html>`
166+
return stripAnsi(template);
167+
}
142168
return table.toString() + footer(lastUpdated);
143-
}
169+
}

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"corona": "./bin/index.js"
88
},
99
"scripts": {
10-
"dev": "nodemon app.js",
10+
"dev": "nodemon --inspect app.js",
1111
"start": "node app.js",
1212
"test": "echo \"Error: no test specified\" && exit 1"
1313
},
@@ -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)