const http = require('http');
const fs = require('fs');
const path = require('path');
const DATA_FILE = path.join(__dirname, 'data/mac_nerds.json');
const CATS_FILE = path.join(__dirname, 'data/mac_nerds_cats.json');
const PORT = 3001;
const RISKS = ['safe', 'warning', 'danger'];
function readData() { return JSON.parse(fs.readFileSync(DATA_FILE, 'utf8')); }
function writeData(d) { fs.writeFileSync(DATA_FILE, JSON.stringify(d, null, 4), 'utf8'); }
function readCats() { return JSON.parse(fs.readFileSync(CATS_FILE, 'utf8')); }
function writeCats(c) { fs.writeFileSync(CATS_FILE, JSON.stringify(c, null, 2), 'utf8'); }
function esc(s) {
return String(s).replace(/&/g,'&').replace(//g,'>').replace(/"/g,'"');
}
function html(data, cats) {
const catOptions = cats.map(c => ``).join('');
const catBadges = cats.map(c => `
${esc(c)}
`).join('');
const rows = data.map((cmd, i) => `
| ${esc(cmd.cat)} |
${esc(cmd.title)} |
${esc(cmd.cmd)} |
${esc(cmd.desc)} |
${cmd.risk} |
|
`).join('');
const json = JSON.stringify(data).replace(/
MAC NERDS // ADMIN
MAC NERDS
// LOKALES ADMIN-PANEL · localhost:${PORT}
| CAT | TITEL | BEFEHL | BESCHREIBUNG | RISIKO | |
${rows}
`;
}
const server = http.createServer((req, res) => {
const json = (status, obj) => {
res.writeHead(status, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(obj));
};
const body = (cb) => {
let b = '';
req.on('data', c => b += c);
req.on('end', () => cb(b));
};
if (req.method === 'GET' && req.url === '/') {
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(html(readData(), readCats()));
} else if (req.method === 'POST' && req.url === '/save') {
body(b => {
try { writeData(JSON.parse(b)); json(200, { ok: true }); }
catch(e) { json(400, { ok: false, error: e.message }); }
});
} else if (req.method === 'POST' && req.url === '/save-cats') {
body(b => {
try { writeCats(JSON.parse(b)); json(200, { ok: true }); }
catch(e) { json(400, { ok: false, error: e.message }); }
});
} else {
res.writeHead(404); res.end('Not found');
}
});
server.listen(PORT, '127.0.0.1', () => {
console.log(`\n MAC NERDS ADMIN → http://localhost:${PORT}\n`);
});