← back to Dw Boardroom Governance

src/api/server.ts

81 lines

/**
 * DW Boardroom Governance — API Server
 * Port 4020 | Isolated governance engine
 */

import express from 'express';
import cors from 'cors';
import http from 'http';
import dotenv from 'dotenv';
import { authMiddleware } from './middleware/auth';
import { initWebSocket } from '../ws/broadcast';
import { startEscalationMonitor } from '../agents/escalation';

// Route imports
import meetingRoutes from './routes/meetings';
import decisionRoutes from './routes/decisions';
import delegationRoutes from './routes/delegations';
import initiativeRoutes from './routes/initiatives';
import escalationRoutes from './routes/escalations';
import governanceRoutes from './routes/governance';

dotenv.config();

// Crash handlers
process.on('unhandledRejection', (reason: any) => {
  console.error('[Governance CRASH] Unhandled Rejection:', reason?.stack || reason);
});
process.on('uncaughtException', (err) => {
  console.error('[Governance CRASH] Uncaught Exception:', err?.stack || err);
});

const app = express();
const server = http.createServer(app);
const PORT = parseInt(process.env.PORT || '4020');

// Health check — no auth
app.get('/health', (_req, res) => {
  res.json({
    status: 'ok',
    agent: 'DW Boardroom Governance',
    port: PORT,
    uptime: Math.round(process.uptime()),
  });
});

// CORS
app.use(cors({
  origin: [
    process.env.FRONTEND_URL || 'http://45.61.58.125:4030',
    'http://localhost:4030',
    'http://127.0.0.1:4030',
    'http://45.61.58.125:4060',
  ],
  credentials: true,
}));

// Auth — after health, before routes
app.use(authMiddleware);
app.use(express.json({ limit: '1mb' }));

// Routes
app.use('/api/meetings', meetingRoutes);
app.use('/api/decisions', decisionRoutes);
app.use('/api/delegations', delegationRoutes);
app.use('/api/initiatives', initiativeRoutes);
app.use('/api/escalations', escalationRoutes);
app.use('/api/governance', governanceRoutes);

// WebSocket
initWebSocket(server);

// Start escalation monitor
startEscalationMonitor();

// Launch
server.listen(PORT, '0.0.0.0', () => {
  console.log(`[Governance] DW Boardroom Governance running on port ${PORT}`);
  console.log(`[Governance] Health: http://45.61.58.125:${PORT}/health`);
  console.log(`[Governance] API: http://45.61.58.125:${PORT}/api/governance/status`);
});