167 lines
5.4 KiB
JavaScript
167 lines
5.4 KiB
JavaScript
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
const DATA_FILE = path.join(__dirname, 'data.json');
|
|
|
|
// Load data
|
|
function loadData() {
|
|
try {
|
|
return JSON.parse(fs.readFileSync(DATA_FILE, 'utf8'));
|
|
} catch (e) {
|
|
return { events: [], reports: [] };
|
|
}
|
|
}
|
|
|
|
function saveData(data) {
|
|
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
|
|
}
|
|
|
|
const MIME_TYPES = {
|
|
'.html': 'text/html',
|
|
'.css': 'text/css',
|
|
'.js': 'application/javascript',
|
|
'.json': 'application/json',
|
|
'.png': 'image/png',
|
|
'.jpg': 'image/jpeg',
|
|
'.svg': 'image/svg+xml'
|
|
};
|
|
|
|
const server = http.createServer((req, res) => {
|
|
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
const pathname = url.pathname;
|
|
|
|
// CORS
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
res.writeHead(204);
|
|
return res.end();
|
|
}
|
|
|
|
// API routes
|
|
if (pathname.startsWith('/api/')) {
|
|
let data = loadData();
|
|
|
|
// GET /api/events - get all events
|
|
if (pathname === '/api/events' && req.method === 'GET') {
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify(data.events));
|
|
}
|
|
|
|
// POST /api/events - create event
|
|
if (pathname === '/api/events' && req.method === 'POST') {
|
|
let body = '';
|
|
req.on('data', (chunk) => { body += chunk; });
|
|
req.on('end', () => {
|
|
try {
|
|
const event = JSON.parse(body);
|
|
event.id = data.events.length ? Math.max(...data.events.map(e => e.id)) + 1 : 1;
|
|
data.events.push(event);
|
|
saveData(data);
|
|
res.writeHead(201, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify(event));
|
|
} catch (e) {
|
|
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify({ error: 'Invalid JSON' }));
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
// PUT /api/events/:id - update event
|
|
const eventMatch = pathname.match(/^\/api\/events\/(\d+)$/);
|
|
if (eventMatch && req.method === 'PUT') {
|
|
let body = '';
|
|
req.on('data', (chunk) => { body += chunk; });
|
|
req.on('end', () => {
|
|
try {
|
|
const updated = JSON.parse(body);
|
|
const idx = data.events.findIndex(e => e.id === parseInt(eventMatch[1]));
|
|
if (idx === -1) {
|
|
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify({ error: 'Not found' }));
|
|
}
|
|
data.events[idx] = { ...data.events[idx], ...updated };
|
|
saveData(data);
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify(data.events[idx]));
|
|
} catch (e) {
|
|
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify({ error: 'Invalid JSON' }));
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
// DELETE /api/events/:id
|
|
if (eventMatch && req.method === 'DELETE') {
|
|
data.events = data.events.filter(e => e.id !== parseInt(eventMatch[1]));
|
|
saveData(data);
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify({ success: true }));
|
|
}
|
|
|
|
// GET /api/reports - get all reports
|
|
if (pathname === '/api/reports' && req.method === 'GET') {
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify(data.reports));
|
|
}
|
|
|
|
// POST /api/reports - create report
|
|
if (pathname === '/api/reports' && req.method === 'POST') {
|
|
let body = '';
|
|
req.on('data', (chunk) => { body += chunk; });
|
|
req.on('end', () => {
|
|
try {
|
|
const report = JSON.parse(body);
|
|
report.id = data.reports.length ? Math.max(...data.reports.map(r => r.id)) + 1 : 1;
|
|
report.createdAt = new Date().toISOString();
|
|
data.reports.push(report);
|
|
saveData(data);
|
|
res.writeHead(201, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify(report));
|
|
} catch (e) {
|
|
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify({ error: 'Invalid JSON' }));
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
// DELETE /api/reports/:id
|
|
const reportMatch = pathname.match(/^\/api\/reports\/(\d+)$/);
|
|
if (reportMatch && req.method === 'DELETE') {
|
|
data.reports = data.reports.filter(r => r.id !== parseInt(reportMatch[1]));
|
|
saveData(data);
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify({ success: true }));
|
|
}
|
|
|
|
// Fallback API
|
|
res.writeHead(404, { 'Content-Type': 'application/json' });
|
|
return res.end(JSON.stringify({ error: 'Not found' }));
|
|
}
|
|
|
|
// Static files
|
|
let filePath = pathname === '/' ? '/index.html' : pathname;
|
|
const fullPath = path.join(__dirname, filePath);
|
|
|
|
fs.readFile(fullPath, (err, content) => {
|
|
if (err) {
|
|
res.writeHead(404);
|
|
return res.end('Not found');
|
|
}
|
|
const ext = path.extname(fullPath);
|
|
res.writeHead(200, { 'Content-Type': MIME_TYPES[ext] || 'application/octet-stream' });
|
|
res.end(content);
|
|
});
|
|
});
|
|
|
|
server.listen(PORT, () => {
|
|
console.log(`Server running on port ${PORT}`);
|
|
});
|