#!/usr/bin/env node
/**
* mozdIT Content Editor Server v2
* Szerkesztő felület a JSON tartalom fájlokhoz
* Támogatja: szöveg szerkesztés, tömbelem hozzáadás/törlés
* Futtatás: node content-editor.js
* Megnyitás: http://localhost:4001
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const PORT = 4001;
const CONTENT_DIR = path.join(__dirname, 'proto', 'src', 'content');
const FILES = {
common: path.join(CONTENT_DIR, 'common.json'),
home: path.join(CONTENT_DIR, 'pages', 'home.json'),
about: path.join(CONTENT_DIR, 'pages', 'about.json'),
services: path.join(CONTENT_DIR, 'pages', 'services.json'),
contact: path.join(CONTENT_DIR, 'pages', 'contact.json'),
adatvedelem: path.join(CONTENT_DIR, 'pages', 'adatvedelem.json'),
hasznalatiFeltetelek: path.join(CONTENT_DIR, 'pages', 'hasznalati-feltetelek.json'),
};
const FILE_LABELS = {
common: '⚙️ Közös szövegek',
home: '🏠 Kezdőlap',
about: '👥 Rólunk',
services: '🛠️ Szolgáltatások',
contact: '📬 Kapcsolat',
adatvedelem: '🔒 Adatvédelem',
hasznalatiFeltetelek: '⚖️ ÁSZF',
};
const HTML = (activeFile, jsonData, message) => `
mozdIT — Tartalom Szerkesztő
${message ? `${message.text}
` : ''}
mozdIT Tartalom Szerkesztő
— JSON fájlok szerkesztése vizuálisan
📝 Szerkeszd a mezőket. Tömbökből elemet törölhetsz (❌) vagy hozzáadhatsz (➕). Mentés gomb menti a fájlt.
`;
// ── Server ───────────────────────────────────────────────────────────────────
const CMS_USER = process.env.CMS_USER || 'admin';
const CMS_PASS = process.env.CMS_PASS || 'mozdit2026';
const server = http.createServer(async (req, res) => {
// Basic Auth verification
const b64auth = (req.headers.authorization || '').split(' ')[1] || '';
const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':');
if (login !== CMS_USER || password !== CMS_PASS) {
res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="mozdIT CMS"' });
res.end('Access denied');
return;
}
const u = new URL(req.url, `http://localhost:${PORT}`);
const fileKey = u.searchParams.get('file') || 'home';
const activeFile = FILES[fileKey] ? fileKey : 'home';
// POST /save — JSON body
if (req.method === 'POST' && u.pathname === '/save') {
let body = '';
req.on('data', c => body += c);
req.on('end', () => {
try {
const data = JSON.parse(body);
fs.writeFileSync(FILES[activeFile], JSON.stringify(data, null, 2) + '\n', 'utf8');
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: true }));
} catch (e) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: false, error: e.message }));
}
});
return;
}
// POST /publish — Git Commit & Push
if (req.method === 'POST' && u.pathname === '/publish') {
exec('git add . && git commit -m "content: frissítve a CMS-ből" && git push', { cwd: CONTENT_DIR }, (error, stdout, stderr) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
if (error) {
// If there's nothing to commit, it's fine
if (stdout.includes('nothing to commit') || stdout.includes('working tree clean')) {
res.end(JSON.stringify({ ok: true, output: 'No changes to commit' }));
} else {
res.end(JSON.stringify({ ok: false, error: stderr || stdout || error.message }));
}
} else {
// If a deploy.sh script exists, run it optionally in background
exec('cd ../../../ && ./deploy.sh production > deploy.log 2>&1 &');
res.end(JSON.stringify({ ok: true, output: stdout }));
}
});
return;
}
// GET / — editor UI
let message = null;
let jsonData = '{}';
try {
jsonData = fs.readFileSync(FILES[activeFile], 'utf8').trim();
} catch (e) {
message = { type: 'err', text: 'Fájl olvasási hiba: ' + e.message };
}
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(HTML(activeFile, jsonData, message));
});
server.listen(PORT, () => {
console.log(`\n✅ mozdIT Tartalom Szerkesztő fut: http://localhost:${PORT}\n`);
console.log(' Szerkeszthető fájlok:');
Object.entries(FILE_LABELS).forEach(([k, l]) => {
const rel = k === 'common' ? 'common.json' : `pages/${k}.json`;
console.log(` • ${l}: proto/src/content/${rel}`);
});
console.log('\n Ctrl+C a leállításhoz\n');
});