#!/usr/bin/env node import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Express health endpoint const expressHealthEndpoint = ` // Health check endpoint app.get('/health', (req, res) => { res.json({ status: 'healthy', service: process.env.SERVICE_NAME || 'unknown', timestamp: new Date().toISOString(), uptime: process.uptime() }); }); `; // Hapi health endpoint const hapiHealthEndpoint = ` // Health check route server.route({ method: 'GET', path: '/health', handler: (request, h) => { return { status: 'healthy', service: process.env.SERVICE_NAME || 'unknown', timestamp: new Date().toISOString(), uptime: process.uptime() }; } }); `; // Services to update const services = [ { name: 'orchestrator', framework: 'express', port: 3001 }, { name: 'claude-agent', framework: 'express', port: 3002 }, { name: 'gramjs-adapter', framework: 'express', port: 3003 }, { name: 'safety-guard', framework: 'express', port: 3004 }, { name: 'analytics', framework: 'hapi', port: 3005 }, { name: 'compliance-guard', framework: 'express', port: 3006 }, { name: 'ab-testing', framework: 'express', port: 3007 } ]; async function addHealthEndpoints() { for (const service of services) { const indexPath = path.join(__dirname, '..', 'services', service.name, 'src', 'index.js'); try { let content = fs.readFileSync(indexPath, 'utf8'); // Check if health endpoint already exists if (content.includes('/health')) { console.log(`✓ ${service.name} already has health endpoint`); continue; } // Add health endpoint based on framework if (service.framework === 'express') { // Find where to insert (after express app creation) const appCreationMatch = content.match(/const app = express\(\);/); if (appCreationMatch) { const insertPos = content.indexOf(appCreationMatch[0]) + appCreationMatch[0].length; content = content.slice(0, insertPos) + expressHealthEndpoint + content.slice(insertPos); } } else if (service.framework === 'hapi') { // Find where to insert (after server creation) const serverCreationMatch = content.match(/const server = Hapi\.server\([^}]+\}\);/s); if (serverCreationMatch) { const insertPos = content.indexOf(serverCreationMatch[0]) + serverCreationMatch[0].length; content = content.slice(0, insertPos) + hapiHealthEndpoint + content.slice(insertPos); } } // Update SERVICE_NAME in Dockerfile const dockerfilePath = path.join(__dirname, '..', 'services', service.name, 'Dockerfile'); if (fs.existsSync(dockerfilePath)) { let dockerContent = fs.readFileSync(dockerfilePath, 'utf8'); if (!dockerContent.includes('SERVICE_NAME')) { dockerContent = dockerContent.replace( 'CMD ["node", "src/app.js"]', `ENV SERVICE_NAME=${service.name}\nCMD ["node", "src/app.js"]` ); fs.writeFileSync(dockerfilePath, dockerContent); } } fs.writeFileSync(indexPath, content); console.log(`✓ Added health endpoint to ${service.name}`); } catch (error) { console.error(`✗ Error updating ${service.name}:`, error.message); } } } addHealthEndpoints().then(() => { console.log('\nHealth endpoints added to all services!'); console.log('Please rebuild and restart the services.'); });