-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
145 lines (127 loc) · 4.5 KB
/
server.js
File metadata and controls
145 lines (127 loc) · 4.5 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const express = require('express');
const path = require('path');
const https = require('https');
const app = express();
const PORT = process.env.PORT || 3001;
const CACHE_DURATION = 5 * 60 * 1000;
let playerStatsCache = null;
let lastFetch = 0;
function fetchPlayerStats() {
return new Promise((resolve) => {
const currentOptions = {
hostname: 'iw4x.io:8080',
path: '/v1/stats/current',
method: 'GET',
headers: { 'User-Agent': 'IW4x-Website/1.0' }
};
const historyOptions = {
hostname: 'iw4x.io:8080',
path: '/v1/stats?period=14d&protocol=152&granularity=hourly',
method: 'GET',
headers: { 'User-Agent': 'IW4x-Website/1.0' }
};
let currentStats = null;
let historyStats = null;
let completed = 0;
function checkComplete() {
if (++completed === 2) {
const byProtocol = currentStats?.by_protocol?.['152'] || { players: 0, servers: 0 };
const eightDaysAgo = Date.now() - (8 * 24 * 60 * 60 * 1000);
const filteredData = historyStats?.data
?.filter(item => (item.timestamp * 1000) >= eightDaysAgo)
?.map(item => ({
timestamp: item.timestamp * 1000,
protocols: { '152': { players: item.player_count || 0 } }
}))
?.sort((a, b) => a.timestamp - b.timestamp) || [];
resolve({
players: byProtocol.players,
servers: byProtocol.servers,
history: {
hourly: filteredData
}
});
}
}
function makeRequest(options, callback) {
const req = https.request(options, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
try {
callback(null, JSON.parse(data));
} catch (error) {
callback(error, null);
}
});
});
req.on('error', callback);
req.setTimeout(10000, () => {
req.destroy();
callback(new Error('Timeout'), null);
});
req.end();
}
makeRequest(currentOptions, (error, data) => {
currentStats = error ? { by_protocol: { '152': { players: 0, servers: 0 } } } : data;
checkComplete();
});
makeRequest(historyOptions, (error, data) => {
historyStats = error ? { data: [] } : data;
checkComplete();
});
});
}
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', async (req, res) => {
const now = Date.now();
if (!playerStatsCache || (now - lastFetch) > CACHE_DURATION) {
try {
playerStatsCache = await fetchPlayerStats();
lastFetch = now;
} catch (error) {
if (!playerStatsCache) {
playerStatsCache = { players: 0, servers: 0, history: { hourly: [] } };
}
}
}
res.render('index', {
current_year: new Date().getFullYear(),
playerStats: playerStatsCache
});
});
app.get('/robots.txt', (req, res) => {
res.set('Content-Type', 'text/plain');
res.send(`User-agent: *
Allow: /
Sitemap: https://iw4x.io/sitemap.xml`);
});
app.get('/sitemap.xml', (req, res) => {
res.set('Content-Type', 'text/xml');
res.send(`<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://iw4x.io/</loc>
<lastmod>${new Date().toISOString().split('T')[0]}</lastmod>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>https://docs.iw4x.io/install/launcher</loc>
<lastmod>${new Date().toISOString().split('T')[0]}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>https://docs.iw4x.io/</loc>
<lastmod>${new Date().toISOString().split('T')[0]}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
</url>
</urlset>`);
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`IW4x website running on https://127.0.0.1:${PORT}`);
});