-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
67 lines (56 loc) · 1.75 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
const express = require('express');
const request = require('request-promise-native');
const app = express();
app.use(express.static('static'));
const requestCF = url => request({
url,
headers: {
'X-Auth-Email': process.env.CF_EMAIL,
'X-Auth-Key': process.env.CF_KEY,
'Content-Type': 'application/json',
},
});
const cachePromise = (producer, interval) => {
let lastUpdatedAt;
let lastUpdatedPromise;
return function() {
const now = Date.now();
const hasExpired = !lastUpdatedAt || lastUpdatedAt + interval < now;
if (hasExpired) {
lastUpdatedAt = now;
return lastUpdatedPromise = producer().catch(err => {
lastUpdatedAt = null;
lastUpdatedPromise = null;
throw err;
});
} else {
return lastUpdatedPromise;
}
};
};
const getStats = cachePromise(async () => {
console.log('Fetching CF stats...');
const [cfData, npmData] = await Promise.all([
requestCF(`https://api.cloudflare.com/client/v4/zones/${process.env.CF_ZONE}/analytics/dashboard?since=-43200&continuous=true`),
request('https://api.npmjs.org/downloads/point/last-month')
]).then(result => result.map(item => JSON.parse(item)));
const yarnDownloads = cfData.result.totals.requests.content_type['octet-stream'];
const npmDownloads = npmData.downloads;
return {
cfStats: cfData.result,
yarnDownloads: yarnDownloads,
percentage: Math.round(yarnDownloads / npmDownloads * 10000) / 100,
};
}, 60 * 1000 * 60);
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
getStats().then(data => {
res.render('index', data);
}, () => {
res.end('Server error');
});
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}`)
});