-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserver.js
102 lines (80 loc) · 2.92 KB
/
server.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
const express = require('express');
const app = express();
const port = process.env.PORT || 5000;
const url = require('url');
const child_process = require('child_process');
const path = require('path');
const rp = require('request-promise');
const fs = require('fs');
const moment = require('moment')
function download_file_with_wget(file_url, DOWNLOAD_DIR, DOWNLOADABLE_EXTENTIONS) {
return new Promise((resolve, reject) => {
DOWNLOAD_DIR = DOWNLOAD_DIR || 'elections/data/';
DOWNLOADABLE_EXTENTIONS = DOWNLOADABLE_EXTENTIONS || ['.json'];
FILE_URL = DOWNLOAD_DIR + '/elections.lk.presidential.2019.json';
const file_name = url.parse(file_url).pathname.split('/').pop()
var options = {
uri: file_url,
json: true,
auth:{
username: process.env.username,
password: process.env.password
}
};
rp(options)
.then((res) => {
console.log("Result recieved")
res = res.map((o) => {
return {
...o,
timestamp: (moment(o.timestamp).toDate().getTime()) / 1000
// TODO: [BUG] Requires date in format of 757399980.0 but actual format recieved by url is 2019-11-13T13:00:53.566+0000
// FIXED: by adding the above moment script. Remove if the date format is fixed from the original url
}
}).filter((o)=> !!o.pd_code)
fs.writeFile(FILE_URL, JSON.stringify(res, null, 2), (err) => {
if (err) {
console.log("File save failed")
console.error(err);
reject(err)
}
console.log("File was saved!");
resolve(file_name);
});
})
.catch(function (err) {
console.log("Result recieve failed")
console.error(err);
reject(err);
});
})
};
if (process.env.RESULT_URL) {
download_file_with_wget(process.env.RESULT_URL).then(file => console.log(file)).catch(e => console.log(e));
setInterval(() => {
download_file_with_wget(process.env.RESULT_URL).then(file => console.log(file)).catch(e => console.log(e));
}, process.env.REFRESH_INTERVAL || 300000)
} else {
const file = 'elections/data/elections.lk.presidential.2019.json';
fs.writeFile(file, JSON.stringify([], null, 2), (err) => {
if (err) {
console.log("Empty File save failed")
console.error(err);
}
console.log("Empty File was saved!");
});
}
// create a GET route
app.get('/express_backend', (req, res) => {
res.send({ express: 'YOUR EXPRESS BACKEND IS CONNECTED TO REACT' });
});
app.use(express.static(path.join(__dirname, './build')));
app.use(express.static(path.join(__dirname, './elections')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, './build', 'index.html'));
});
app.get('/healthz', function (req, res) {
res.status(200).send('healthy');
});
// console.log that your server is up and running
app.listen(port, () => console.log(`Listening on port ${port}`));