90 lines
2.9 KiB
JavaScript
90 lines
2.9 KiB
JavaScript
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const PORT = 3456;
|
|
const OMG_OUTPUT_DIR = '/Users/pauljohns/Library/CloudStorage/Box-Box/AUTOMATION_MAIN/OMG_NIFI/OMG_CORE_AC_INGEST';
|
|
|
|
const MIME_TYPES = {
|
|
'.html': 'text/html',
|
|
'.css': 'text/css',
|
|
'.js': 'application/javascript',
|
|
'.png': 'image/png',
|
|
'.jpg': 'image/jpeg',
|
|
'.svg': 'image/svg+xml',
|
|
'.ico': 'image/x-icon',
|
|
};
|
|
|
|
const server = http.createServer((req, res) => {
|
|
|
|
// ---- API: Save CSV to OMG folder ----
|
|
if (req.method === 'POST' && req.url === '/api/send-to-omg') {
|
|
let body = '';
|
|
req.on('data', chunk => { body += chunk; });
|
|
req.on('end', () => {
|
|
try {
|
|
const { filename, csv } = JSON.parse(body);
|
|
|
|
if (!filename || !csv) {
|
|
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({ success: false, error: 'Missing filename or csv data' }));
|
|
return;
|
|
}
|
|
|
|
// Ensure output directory exists
|
|
if (!fs.existsSync(OMG_OUTPUT_DIR)) {
|
|
fs.mkdirSync(OMG_OUTPUT_DIR, { recursive: true });
|
|
}
|
|
|
|
const filePath = path.join(OMG_OUTPUT_DIR, filename);
|
|
fs.writeFileSync(filePath, csv, 'utf8');
|
|
|
|
console.log(`[OK] Saved: ${filePath}`);
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({ success: true, path: filePath }));
|
|
|
|
} catch (err) {
|
|
console.error('[ERROR]', err.message);
|
|
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({ success: false, error: err.message }));
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
// ---- Serve static files ----
|
|
let filePath = req.url === '/' ? '/index.html' : req.url;
|
|
filePath = path.join(__dirname, filePath);
|
|
|
|
// Prevent directory traversal
|
|
if (!filePath.startsWith(__dirname)) {
|
|
res.writeHead(403);
|
|
res.end('Forbidden');
|
|
return;
|
|
}
|
|
|
|
const ext = path.extname(filePath).toLowerCase();
|
|
const contentType = MIME_TYPES[ext] || 'application/octet-stream';
|
|
|
|
fs.readFile(filePath, (err, data) => {
|
|
if (err) {
|
|
if (err.code === 'ENOENT') {
|
|
res.writeHead(404);
|
|
res.end('Not found');
|
|
} else {
|
|
res.writeHead(500);
|
|
res.end('Server error');
|
|
}
|
|
return;
|
|
}
|
|
res.writeHead(200, { 'Content-Type': contentType });
|
|
res.end(data);
|
|
});
|
|
});
|
|
|
|
server.listen(PORT, () => {
|
|
console.log(`\n Ferrero AC Booking Tool`);
|
|
console.log(` ──────────────────────`);
|
|
console.log(` Running at: http://localhost:${PORT}`);
|
|
console.log(` OMG output: ${OMG_OUTPUT_DIR}\n`);
|
|
});
|