mirror of
https://github.com/DeNNiiInc/Website-Stress-Test.git
synced 2026-04-17 12:36:00 +00:00
Optimize architecture: Nginx now proxies everything to Node; added static file support for all assets
This commit is contained in:
@@ -118,33 +118,59 @@ if (cluster.isMaster) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Serve static files for the UI
|
// 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 requestPath = req.url.split('?')[0];
|
||||||
let filePath = '.' + requestPath;
|
let filePath = '.' + requestPath;
|
||||||
if (requestPath === '/') filePath = './index.html';
|
if (requestPath === '/') filePath = './index.html';
|
||||||
|
|
||||||
const extname = path.extname(filePath);
|
// Basic security: don't allow accessing files outside the directory or sensitive files
|
||||||
let contentType = 'text/html';
|
const resolvedPath = path.resolve(filePath);
|
||||||
switch (extname) {
|
const rootPath = path.resolve('.');
|
||||||
case '.js': contentType = 'text/javascript'; break;
|
|
||||||
case '.css': contentType = 'text/css'; break;
|
if (!resolvedPath.startsWith(rootPath) || filePath.includes('..')) {
|
||||||
|
res.writeHead(403);
|
||||||
|
res.end('Forbidden');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.readFile(filePath, (error, content) => {
|
fs.access(filePath, fs.constants.F_OK, (err) => {
|
||||||
if (error) {
|
if (!err) {
|
||||||
if (error.code == 'ENOENT') {
|
const extname = path.extname(filePath).toLowerCase();
|
||||||
res.writeHead(404);
|
let contentType = 'text/html';
|
||||||
res.end('File not found');
|
const mimeTypes = {
|
||||||
} else {
|
'.js': 'text/javascript',
|
||||||
res.writeHead(500);
|
'.css': 'text/css',
|
||||||
res.end('Error loading file: ' + error.code);
|
'.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) {
|
||||||
|
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 {
|
} else {
|
||||||
res.writeHead(200, { 'Content-Type': contentType });
|
// Not a static file, maybe fall through to POST check
|
||||||
res.end(content, 'utf-8');
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return;
|
|
||||||
|
// 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
|
// Only allow POST requests to the proxy
|
||||||
|
|||||||
@@ -38,11 +38,9 @@ class StressTestingTool {
|
|||||||
requestBody: null,
|
requestBody: null,
|
||||||
thinkTime: 1000,
|
thinkTime: 1000,
|
||||||
proxyUrl:
|
proxyUrl:
|
||||||
window.location.protocol === "file:" ||
|
window.location.protocol.startsWith('http')
|
||||||
window.location.hostname === "localhost" ||
|
? window.location.origin
|
||||||
window.location.hostname === "127.0.0.1"
|
: "http://localhost:3000",
|
||||||
? "http://localhost:3000"
|
|
||||||
: "/proxy",
|
|
||||||
|
|
||||||
// Crawler settings
|
// Crawler settings
|
||||||
crawlerEnabled: false,
|
crawlerEnabled: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user