forked from sagarkarira/coronavirus-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
168 lines (141 loc) · 4.94 KB
/
app.js
File metadata and controls
168 lines (141 loc) · 4.94 KB
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
const express = require('express');
const morgan = require('morgan');
const chalk = require('chalk');
const {
getCountryTable,
getJSONData,
getJSONDataForCountry,
} = require('./lib/byCountry');
const { getCompleteTable, getGraph } = require('./lib/corona');
const { lookupCountry, htmlTemplate } = require('./lib/helpers');
const { getLiveUpdates } = require('./lib/reddit.js');
const { getWorldoMetersTable } = require('./lib/worldoMeters.js');
const { helpContent } = require('./lib/constants');
const app = express();
const port = process.env.PORT || 3001;
const IS_CURL_RE = /\bcurl\b/im;
function errorHandler(error, res) {
console.error(error);
return res.send(`
I am sorry. Something went wrong. Please report it\n
${error.message}
`);
}
app.set('json escape', true);
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');
next();
});
app.get('/', (req, res) => {
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
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) : 1;
if (source === 2) {
return getWorldoMetersTable({ isCurl, emojis, minimal, top, format})
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
}
if (format.toLowerCase() === 'json') {
return getJSONData().then(result => {
return res.json(result);
}).catch(error => errorHandler(error, res));
}
return getCompleteTable({ isCurl, emojis, minimal, top })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
});
app.get('/updates', (req, res) => {
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
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, res));
}
return getLiveUpdates({ json: false, isCurl }).then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
});
app.get(['/:country/graph', '/graph'], (req, res) => {
const { country } = req.params;
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
if (!country) {
return getGraph({ isCurl })
.then(result => res.send(result))
.catch(error => errorHandler(error, res));
}
const lookupObj = lookupCountry(country);
if (!lookupObj) {
return res.send(`
Country not found.
Try the full country name or country code.
Example:
- /UK: for United Kingdom
- /US: for United States of America.
- /Italy: for Italy.
`);
}
return getGraph({countryCode: lookupObj.iso2, isCurl })
.then(result => res.send(result))
.catch(error => errorHandler(error, res));
});
app.get('/help', (req, res) => {
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
if (!isCurl) {
return res.send(htmlTemplate(helpContent));
}
return res.send(chalk.green(helpContent));
});
app.get('/:country', (req, res) => {
const { country } = req.params;
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
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) : 1;
if (!country || country.toUpperCase() === 'ALL') {
if (format.toLowerCase() === 'json') {
return getJSONData().then(result => {
return res.json(result);
}).catch(error => errorHandler(error, res));
}
return getCompleteTable({ isCurl, emojis, minimal })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
}
const lookupObj = lookupCountry(country);
if (!lookupObj) {
return res.send(`
Country not found.
Try the full country name or country code.
Example:
- /UK: for United Kingdom
- /US: for United States of America.
- /Italy: for Italy.
`);
}
const { iso2 } = lookupObj;
if (source === 2) {
return getWorldoMetersTable({ countryCode: iso2, isCurl, emojis, minimal, format })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
}
if (format.toLowerCase() === 'json') {
return getJSONDataForCountry(iso2).then(result => {
return res.json(result);
}).catch(error => errorHandler(error, res));
}
return getCountryTable({ countryCode: iso2, isCurl, emojis, minimal })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
});
app.listen(port, () => console.log(`Running on ${port}`));