42 lines
1.4 KiB
JavaScript
42 lines
1.4 KiB
JavaScript
const state = require('../../../infrastructure/state');
|
|
const { cleanupInterfaces } = require('../../../maintenance/cleanup');
|
|
const { logError } = require('../../../infrastructure/logger');
|
|
const { broadcast } = require('../websocket');
|
|
|
|
async function handleInterfacesRoutes(req, res) {
|
|
const urlPath = req.urlPath || new URL(req.url, `https://${req.headers.host}`).pathname;
|
|
const method = req.method;
|
|
|
|
if (method === 'GET' && urlPath === '/api/interfaces') {
|
|
try {
|
|
const interfaces = Array.from(state.domainToIPMap.entries()).map(([domain, ip]) => ({ domain, ip }));
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify(interfaces));
|
|
} catch (err) {
|
|
logError('Admin', `Failed to fetch interfaces: ${err.message}`);
|
|
res.writeHead(500);
|
|
res.end(JSON.stringify({ error: 'Failed to fetch interfaces' }));
|
|
}
|
|
return true;
|
|
}
|
|
|
|
if (method === 'POST' && urlPath === '/api/cleanup-interfaces') {
|
|
try {
|
|
await cleanupInterfaces();
|
|
broadcast({ type: 'update-interfaces' });
|
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
res.end('OK');
|
|
} catch (err) {
|
|
logError('Admin', `Failed to cleanup interfaces: ${err.message}`);
|
|
res.writeHead(500);
|
|
res.end(err.message);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
module.exports = { handleInterfacesRoutes };
|
|
|