index.ts•2.59 kB
import express from 'express';
import cors from 'cors';
import swaggerUi from 'swagger-ui-express';
// import dotenv from 'dotenv'; // Elimina esta línea
import path from 'path';
import { MCPServer } from './services/mcp/mcp.server';
import { InfrastructureService } from './services/infrastructure.service';
import { createInfrastructureRoutes } from './routes/infrastructure.routes';
import { createChatRoutes } from './routes/chat.routes';
import { errorHandler } from './middleware/error.middleware';
import { swaggerSpec } from './config/swagger.config';
import { logger } from './utils/logger';
// Load environment variables (ya no se usa dotenv.config() aquí)
// dotenv.config(); // Elimina esta línea
// Initialize Express app
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Serve static files (Chat GUI)
app.use(express.static(path.join(__dirname, '..', 'public')));
// Initialize MCP Server
const mcpServer = new MCPServer();
// Initialize Infrastructure Service
const infrastructureService = new InfrastructureService(mcpServer);
// API Documentation
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// Health check endpoint
app.get('/health', (req, res) => {
res.json({
status: 'healthy',
timestamp: new Date().toISOString(),
service: 'AWS Infrastructure MCP',
});
});
// Routes
app.use('/api/infrastructure', createInfrastructureRoutes(infrastructureService));
app.use('/api/chat', createChatRoutes(infrastructureService));
// Serve Chat GUI on root
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '..', 'public', 'index.html'));
});
// Error handling middleware
app.use(errorHandler);
// Start server
const startServer = async () => {
try {
// Start MCP server if running as standalone
if (process.env.MCP_MODE === 'standalone') {
await mcpServer.start();
}
// Start Express server
app.listen(PORT, () => {
logger.info(`Server running on port ${PORT}`);
logger.info(`Chat GUI available at http://localhost:${PORT}`);
logger.info(`API Documentation available at http://localhost:${PORT}/api-docs`);
});
} catch (error) {
logger.error('Failed to start server:', error);
process.exit(1);
}
};
startServer();
// Graceful shutdown
process.on('SIGINT', () => {
logger.info('Shutting down gracefully...');
process.exit(0);
});
process.on('SIGTERM', () => {
logger.info('Shutting down gracefully...');
process.exit(0);
});