forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
130 lines (117 loc) · 3.95 KB
/
app.js
File metadata and controls
130 lines (117 loc) · 3.95 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
// node modules
const express = require('express'),
app = express(),
util = require('./bin/util'),
fs = require('fs'),
axios = require('axios'),
covid19 = require('./lib/cli');
// set port
const port = process.env.port || 7070;
// package.json info
const pkg = JSON.parse(fs.readFileSync('package.json'));
// api base url
const apiBaseURL = "https://corona.lmao.ninja";
// global route for covid19 tracker
app.get('/', async (req, res, next) => {
const userAgent = req.headers['user-agent'],
api = await axios.get(`${apiBaseURL}/all`),
data = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.covid19globaltracker(
data.cases, data.deaths,
data.recovered, data.updated
));
return null;
}
return next();
});
// for cmd and powershell
app.get(['/plain','/cmd','/basic'], async (req, res, next) => {
const userAgent = req.headers['user-agent'],
api = await axios.get(`${apiBaseURL}/all`),
data = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.plainglobaltracker(
data.cases, data.deaths,
data.recovered, data.updated
));
return null;
}
return next();
});
// help options
app.get(['/help','/manual','/cmd/help','/plain/help','/basic/help'], async (req, res, next) => {
const userAgent = req.headers['user-agent'];
if (util.isCommandline(userAgent)) {
await res.send(covid19.help());
return null;
}
return next();
});
// by country route for covid19 tracker
app.get('/:country', async (req, res, next) => {
const userAgent = req.headers['user-agent'],
countryData = req.params.country,
api = await axios.get(`${apiBaseURL}/countries/${countryData}`),
all = await axios.get(`${apiBaseURL}/all`),
u = all.data,
d = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.covid19countrytracker(
d.country, d.cases, d.todayCases,
d.deaths, d.todayDeaths, d.recovered,
d.active, d.critical, d.casesPerOneMillion,
u.updated
));
return null;
}
return next();
});
// by country route for covid19 tracker
app.get(['/plain/:country','/cmd/:country','/basic/:country'], async (req, res, next) => {
const userAgent = req.headers['user-agent'],
countryData = req.params.country,
api = await axios.get(`${apiBaseURL}/countries/${countryData}`),
all = await axios.get(`${apiBaseURL}/all`),
u = all.data,
d = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.plaincountrytracker(
d.country, d.cases, d.todayCases,
d.deaths, d.todayDeaths, d.recovered,
d.active, d.critical, d.casesPerOneMillion,
u.updated
));
return null;
}
return next();
});
// by historical chart by country
app.get('/history/:country/:chartType(cases|deaths)?', async (req, res, next) => {
const userAgent = req.headers['user-agent'],
countryData = req.params.country,
chartType = req.params.chartType || 'cases',
summary = await axios.get(`${apiBaseURL}/countries/${countryData}`),
history = await axios.get(`${apiBaseURL}/v2/historical/${summary.data.country}`),
all = await axios.get(`${apiBaseURL}/all`),
s = summary.data,
h = history.data;
u = all.data;
if (util.isCommandline(userAgent)) {
await res.send(
covid19.historyCountryTracker(
s.country, s.cases, s.todayCases,
s.deaths, s.todayDeaths, s.recovered,
s.active, s.critical, s.casesPerOneMillion,
u.updated, h, chartType
)
);
return null;
}
return next();
});
app.get('*', (req, res) => res.send(`
Welcome to COVID-19 Tracker CLI by Waren Gonzaga
Please visit: https://warengonza.ga/covid19-tracker-cli
\n`));
app.listen(port, () => console.log(`COVID-19 Tracker v${pkg.version} is listening on port ${port}!`));