Skip to content

Commit d062ea3

Browse files
authored
Merge pull request OSSPhilippines#58 from jkga/feature/web-support
Feature/web support
2 parents 9917568 + 14df780 commit d062ea3

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed

app.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const express = require('express'),
55
axios = require('axios'),
66
covid19 = require('./lib/cli'),
77
covid19GFX = require('./lib/cli/gfx'),
8+
EventEmitter = require('events'),
89
pkg = require('./package.json'), // package.json info
910
apiBaseURL = "https://corona.lmao.ninja/v2", // NovelCOVID API
1011
port = process.env.port || 7070; // set port
@@ -153,6 +154,59 @@ app.get('/history/charts/:country/:chartSize(sm|md|lg)?', async (req, res, next)
153154
return next();
154155
});
155156

157+
158+
// historical chart by country
159+
app.get('/history/web/charts/:country?/:chartSize(sm|md|lg)?', async (req, res, next) => {
160+
const userAgent = req.headers['user-agent'],
161+
countryData = req.params.country || 'ph',
162+
chartType = req.params.chartType || 'cases',
163+
chartSize = req.params.chartSize || 'sm',
164+
summary = await axios.get(`${apiBaseURL}/countries/${countryData}`),
165+
history = await axios.get(`${apiBaseURL}/historical/${summary.data.country}?lastdays=all`),
166+
s = summary.data,
167+
h = history.data,
168+
terminator = `\r\n`+'\033[?25h',
169+
xtermHTMLTemplate = require('./lib/cli/gfx/terminal-html-template'),
170+
bufferEmitter = new EventEmitter();
171+
172+
let customHttpResponse = {
173+
send: (data) => {
174+
bufferEmitter.emit('screenBuffer', data)
175+
this.data = data
176+
return this;
177+
},
178+
get: () => {
179+
return this.data
180+
}
181+
};
182+
183+
covid19GFX.historyCountryTracker(
184+
req, customHttpResponse,
185+
s.country, s.cases, s.todayCases,
186+
s.deaths, s.todayDeaths, s.recovered,
187+
s.active, s.critical, s.casesPerOneMillion,
188+
s.updated, h, chartType, s.countryInfo, chartSize
189+
);
190+
191+
let screenOutput = (await new Promise((resolve, reject) => {
192+
bufferEmitter.on('screenBuffer', (screen) => {
193+
resolve(screen)
194+
})
195+
setTimeout(() => {
196+
resolve('')
197+
},5000)
198+
}));
199+
200+
201+
if(!screenOutput.length || !h) res.send('Unable to load the chart.Please refresh the page')
202+
203+
return util.isCommandline(userAgent) ? res.send(screenOutput+terminator) : res.send(xtermHTMLTemplate(screenOutput.replace(terminator, '')))
204+
205+
});
206+
207+
app.use('/xterm', express.static(__dirname + '/node_modules/xterm'));
208+
app.use('/xterm/addons', express.static(__dirname + '/node_modules/xterm-addon-fit'));
209+
156210
// route for web browser clients
157211
app.get('*', (req, res) => res.send(`
158212
Welcome to COVID-19 Tracker CLI v${pkg.version} by Waren Gonzaga\n

lib/cli/gfx/template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ exports.load = (grid, data, chartStyle) => {
3636
[
3737
['',data.cases, data.deaths, data.recovered, data.active, data.casesPerOneMillion],
3838
['','Today Cases', 'Today Deaths', 'Critical', 'Mortality %', 'Recovery %'],
39-
['',data.todayCases, data.todayDeaths, data.critical, data.mortalityPercentage, data.recoveredPercentage]
39+
['',data.todayCases, data.todayDeaths, data.critical, parseFloat(data.mortalityPercentage).toFixed(2), parseFloat(data.recoveredPercentage).toFixed(2)]
4040
]
4141
})
4242

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
const html = (data) => {
3+
return (`<!doctype html>
4+
<html>
5+
<head>
6+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm.min.js"></script>
7+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/xterm-addon-fit.min.js"></script>
8+
</head>
9+
<body>
10+
<div id="terminal" style='width: 105%;height: 100vh;padding-left: 2px;'></div>
11+
</body>
12+
<script>
13+
var term = new Terminal();
14+
var fitAddon = new FitAddon.FitAddon()
15+
term.loadAddon(fitAddon);
16+
term.open(document.getElementById('terminal'));
17+
fitAddon.fit();
18+
term.write('${data}')
19+
</script>
20+
<style>
21+
body{
22+
background: #000;
23+
margin:0;padding:0;
24+
overflow:hidden;
25+
}
26+
textarea, .xterm-char-measure-element, .xterm-scroll-area{
27+
display: none;
28+
}
29+
</style>
30+
</html>`)
31+
}
32+
33+
module.exports = html

package-lock.json

Lines changed: 13 additions & 3 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
"express": "^4.17.1",
4040
"figlet": "^1.3.0",
4141
"ora": "^4.0.3",
42+
"xterm": "^4.9.0",
43+
"xterm-addon-fit": "^0.3.0",
4244
"yargs": "^15.3.1"
4345
},
4446
"devDependencies": {

0 commit comments

Comments
 (0)