const express = require('express'); const router = express.Router(); const { getContainerData } = require('../utils/dockerInspector'); const os = require('os'); router.get('/', async (req, res) => { try { const containers = await getContainerData(); const hostInterfaces = os.networkInterfaces(); const ip = hostInterfaces.en0?.find(i => i.family === 'IPv4')?.address || hostInterfaces.eth0?.find(i => i.family === 'IPv4')?.address || '127.0.0.1'; const rows = containers .map(c => { const [host, container] = c.name.split('-', 2); if (!host || !container) return null; const safeContainer = container.replace(/_/g, ''); const safeHost = host.replace(/_/g, ''); const hasPorts = c.ports && c.ports.length > 0; const url = hasPorts ? `${ip}:${c.ports[0]}` : null; const domain = `${safeContainer}.${safeHost}.bray.io`; if (!url) return null; return { name: safeContainer, url, domain, }; }) .filter(Boolean); // Create a pretty text table const header = `NAME | URL | DOMAIN`; const divider = `--------------------|------------------------|-----------------------------`; const lines = rows.map(row => `${row.name.padEnd(20)}| ${row.url.padEnd(24)}| ${row.domain}` ); const output = [header, divider, ...lines].join('\n'); res.setHeader('Content-Type', 'text/plain'); res.send(output); } catch (err) { console.error('Error building domain list:', err); res.status(500).send('Failed to generate domain mappings'); } }); module.exports = router;