-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
112 lines (92 loc) · 3.1 KB
/
Copy pathserver.js
File metadata and controls
112 lines (92 loc) · 3.1 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
const http = require('node:http');
const fs = require('node:fs');
const path = require('node:path');
const PORT = process.env.PORT || 3000;
const DATA_DIR = path.join(__dirname, 'data');
const MAPS_DIR = path.join(__dirname, 'maps');
const MIME_TYPES = {
'.json': 'application/json; charset=utf-8',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.webp': 'image/webp',
'.svg': 'image/svg+xml',
};
function resolvePath(pathname) {
// Try data/ first, then maps/
const inData = path.join(DATA_DIR, pathname);
if (fs.existsSync(inData) && !fs.statSync(inData).isDirectory()) return inData;
const inMaps = path.join(MAPS_DIR, pathname);
if (fs.existsSync(inMaps) && !fs.statSync(inMaps).isDirectory()) return inMaps;
return null;
}
function serveIndex(res) {
// Build a simple JSON index of all available files
const files = [];
function walk(dir, base) {
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
const rel = base ? `${base}/${entry.name}` : entry.name;
if (entry.isDirectory()) walk(path.join(dir, entry.name), rel);
else files.push(rel);
}
}
walk(DATA_DIR, '');
walk(MAPS_DIR, 'maps');
res.writeHead(200, {
'Content-Type': 'application/json; charset=utf-8',
'Access-Control-Allow-Origin': '*',
'Cache-Control': 'no-cache',
});
res.end(JSON.stringify({ server: 'data.tarkovlab.org', files }, null, 2));
}
const server = http.createServer((req, res) => {
// CORS Headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === 'OPTIONS') {
res.writeHead(204);
res.end();
return;
}
const url = new URL(req.url, `http://${req.headers.host}`);
const pathname = decodeURIComponent(url.pathname);
// Serve favicon – redirect to TarkovLab logo CDN
if (pathname === '/favicon.ico') {
res.writeHead(302, { Location: 'https://logo.tarkovlab.org/tl-icon' });
res.end();
return;
}
// Serve index at /
if (pathname === '/' || pathname === '') {
serveIndex(res);
return;
}
const filePath = resolvePath(pathname);
if (!filePath) {
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'File Not Found', path: pathname }));
return;
}
const ext = path.extname(filePath).toLowerCase();
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
fs.readFile(filePath, (err, data) => {
if (err) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Internal Server Error' }));
return;
}
res.writeHead(200, {
'Content-Type': contentType,
'Cache-Control': 'public, max-age=3600',
});
res.end(data);
});
});
server.listen(PORT, () => {
console.log(`TarkovData server running on port ${PORT}`);
console.log(`Data : ${DATA_DIR}`);
console.log(`Maps : ${MAPS_DIR}`);
console.log(`Index: http://localhost:${PORT}/`);
console.log(`Quests: http://localhost:${PORT}/quests.json`);
});