Optimize architecture: Nginx now proxies everything to Node; added static file support for all assets

This commit is contained in:
DeNNii
2026-01-16 18:24:04 +11:00
parent 3f10aacf66
commit 9069fa2123
2 changed files with 47 additions and 23 deletions

View File

@@ -118,34 +118,60 @@ if (cluster.isMaster) {
}
// Serve static files for the UI
if (req.method === 'GET' && (req.url === '/' || req.url === '/index.html' || req.url === '/script.js' || req.url.startsWith('/script.js?') || req.url === '/styles.css' || req.url === '/worker.js')) {
if (req.method === 'GET') {
let requestPath = req.url.split('?')[0];
let filePath = '.' + requestPath;
if (requestPath === '/') filePath = './index.html';
const extname = path.extname(filePath);
let contentType = 'text/html';
switch (extname) {
case '.js': contentType = 'text/javascript'; break;
case '.css': contentType = 'text/css'; break;
// Basic security: don't allow accessing files outside the directory or sensitive files
const resolvedPath = path.resolve(filePath);
const rootPath = path.resolve('.');
if (!resolvedPath.startsWith(rootPath) || filePath.includes('..')) {
res.writeHead(403);
res.end('Forbidden');
return;
}
fs.access(filePath, fs.constants.F_OK, (err) => {
if (!err) {
const extname = path.extname(filePath).toLowerCase();
let contentType = 'text/html';
const mimeTypes = {
'.js': 'text/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.svg': 'image/svg+xml',
'.json': 'application/json'
};
contentType = mimeTypes[extname] || 'text/plain';
fs.readFile(filePath, (error, content) => {
if (error) {
if (error.code == 'ENOENT') {
res.writeHead(404);
res.end('File not found');
} else {
res.writeHead(500);
res.end('Error loading file: ' + error.code);
}
} else {
if (!error) {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
} else {
res.writeHead(500);
res.end('Server Error');
}
});
} else if (req.url === '/health' || req.url === '//health' || (req.url === '/git-info' || req.url === '//git-info')) {
// Handled by other logic (keep going)
} else if (req.url === '/') {
// Fallback for root if index.html doesn't exist? (Unlikely)
} else {
// Not a static file, maybe fall through to POST check
}
});
// Special handling for health and git-info which are GET but not files
if (req.url.includes('/health') || req.url.includes('/git-info')) {
// Let it fall through to those handlers
} else {
return;
}
}
// Only allow POST requests to the proxy
if (req.method !== 'POST') {

View File

@@ -38,11 +38,9 @@ class StressTestingTool {
requestBody: null,
thinkTime: 1000,
proxyUrl:
window.location.protocol === "file:" ||
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1"
? "http://localhost:3000"
: "/proxy",
window.location.protocol.startsWith('http')
? window.location.origin
: "http://localhost:3000",
// Crawler settings
crawlerEnabled: false,