This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid.js
121 lines (109 loc) · 3.46 KB
/
covid.js
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
if(process.argv.length < 4){
console.log('Required command: node covid.js <ip_binding> <port_binding>');
return false;
}
var express = require('express');
var app = express();
var tracker = require('./tracker');
var db = require('./database');
const fsys = require('fs');
if(process.argv[2] != 'proxy') {
var http = require('http').createServer(app);
app.use('/static', express.static('static'));
}
app.set('view engine', 'ejs');
app.use((req, res, next) => {
res.removeHeader('X-Powered-By');
res.removeHeader('Server');
next();
});
app.get('/', async (req, res) => {
let data = fsys.readFileSync(tracker.COUNTRIES_FILE);
res.render('website/index', {
title: 'Estadísticas COVID-19',
countries: JSON.parse(data)
})
});
app.get('/country', async (req, res) => {
let data = fsys.readFileSync(tracker.COUNTRIES_FILE);
res.render('website/overview', {
title: 'Estadísticas por países',
countries: JSON.parse(data)
});
});
app.get('/country/day', async (req, res) => {
let data = fsys.readFileSync(tracker.COUNTRIES_FILE);
res.render('website/daily', {
title: 'Estadística diaria',
countries: JSON.parse(data)
});
});
app.get('/country/week', async (req, res) => {
let data = fsys.readFileSync(tracker.COUNTRIES_FILE);
res.render('website/weekly', {
title: 'Estadística semanal',
countries: JSON.parse(data)
});
});
app.get('/country/month', async (req, res) => {
let data = fsys.readFileSync(tracker.COUNTRIES_FILE);
res.render('website/monthly', {
title: 'Estadística mensual',
countries: JSON.parse(data)
});
});
app.get('/list/countries', async (req, res) => {
res.append('Content-Type', 'application/json');
res.send(fsys.readFileSync(tracker.COUNTRIES_FILE));
});
app.get('/realtime/country/:country', async (req, res) => {
tracker.downloadData(req.params.country).then((data) => {
res.append('Content-Type', 'application/json');
res.send(JSON.stringify(data));
}).catch((err) => {
console.log(err);
res.send('Real-time data is not available');
});
});
app.get('/history/country/:period/:country', async (req, res) => {
let minDate = new Date(Date.now());
switch(req.params.period) {
case 'day':
minDate.setUTCDate(minDate.getUTCDate() - 2);
break;
case 'week':
minDate.setUTCDate(minDate.getUTCDate() - 8);
break;
case 'month':
minDate.setUTCMonth(minDate.getUTCMonth() - 1);
break;
default:
res.send('Not a valid time');
return;
}
try {
let response = await db.queryData(req.params.country, minDate.toISOString());
res.append('Content-Type', 'application/json');
res.send(JSON.stringify(response));
} catch(err) {
console.log(err);
res.send('Unable to retrieve the history');
}
});
if(process.argv[2] != 'proxy') {
http.listen(process.argv[3], process.argv[2], () => {
console.log('External server started');
tracker.updateDatabase();
setInterval(() => {
tracker.updateDatabase();
}, 1.2E6);
});
} else {
app.listen(process.argv[3], () => {
console.log('Internal server started (no static file is being served)');
tracker.updateDatabase();
setInterval(() => {
tracker.updateDatabase();
}, 1.2E6);
});
}