const express = require('express'); const path = require('path'); const SporeApiClient = require('./src/client'); const app = express(); const PORT = process.env.PORT || 3001; // Initialize the SPORE API client const sporeClient = new SporeApiClient('http://10.0.1.60'); // Serve static files from public directory app.use(express.static(path.join(__dirname, 'public'))); // Serve the main HTML page app.get('/', (req, res) => { res.sendFile(path.join(__dirname, 'public', 'index.html')); }); // API endpoint to get cluster members app.get('/api/cluster/members', async (req, res) => { try { const members = await sporeClient.getClusterStatus(); res.json(members); } catch (error) { console.error('Error fetching cluster members:', error); res.status(500).json({ error: 'Failed to fetch cluster members', message: error.message }); } }); // API endpoint to get task status app.get('/api/tasks/status', async (req, res) => { try { const taskStatus = await sporeClient.getTaskStatus(); res.json(taskStatus); } catch (error) { console.error('Error fetching task status:', error); res.status(500).json({ error: 'Failed to fetch task status', message: error.message }); } }); // API endpoint to get system status app.get('/api/node/status', async (req, res) => { try { const systemStatus = await sporeClient.getSystemStatus(); res.json(systemStatus); } catch (error) { console.error('Error fetching system status:', error); res.status(500).json({ error: 'Failed to fetch system status', message: error.message }); } }); // Proxy endpoint to get status from a specific node app.get('/api/node/status/:ip', async (req, res) => { try { const nodeIp = req.params.ip; // Create a temporary client for the specific node const nodeClient = new SporeApiClient(`http://${nodeIp}`); const nodeStatus = await nodeClient.getSystemStatus(); res.json(nodeStatus); } catch (error) { console.error(`Error fetching status from node ${req.params.ip}:`, error); res.status(500).json({ error: `Failed to fetch status from node ${req.params.ip}`, message: error.message }); } }); // Start the server app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });