import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import compression from 'compression';
import dotenv from 'dotenv';
import path from 'path';
import fs from 'fs';
import { mcpServer } from './mcp/mcpServer';
import { logger } from './utils/logger';
import { apiRoutes } from './api/routes';
import { errorHandler } from './api/middleware';
// Load environment variables
dotenv.config();
const app = express();
const PORT = process.env.PORT || 3001;
// Create required directories
const requiredDirs = [
'data/zips',
'data/extracted',
'data/vds',
'logs',
'temp',
];
for (const dir of requiredDirs) {
const fullPath = path.join(process.cwd(), dir);
if (!fs.existsSync(fullPath)) {
fs.mkdirSync(fullPath, { recursive: true });
logger.info(`Created directory: ${fullPath}`);
}
}
// Security middleware
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'"],
scriptSrc: ["'self'"],
imgSrc: ["'self'", "data:", "https:"],
},
},
}));
// CORS configuration
app.use(cors({
origin: process.env.CORS_ORIGIN || '*',
credentials: true,
}));
// Compression and parsing
app.use(compression());
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// Request logging
app.use((req, _res, next) => {
logger.info(`${req.method} ${req.path}`, {
ip: req.ip,
userAgent: req.get('User-Agent'),
});
next();
});
// API routes
app.use('/api', apiRoutes);
// Serve dashboard in production
if (process.env.NODE_ENV === 'production') {
const dashboardPath = path.join(process.cwd(), 'dashboard/dist');
app.use(express.static(dashboardPath));
app.get('*', (_req, res) => {
res.sendFile(path.join(dashboardPath, 'index.html'));
});
}
// Health check endpoint
app.get('/health', (_req, res) => {
res.json({
status: 'healthy',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
version: process.env.npm_package_version || '1.0.0',
});
});
// Error handling
app.use(errorHandler);
// Start server
const server = app.listen(PORT, () => {
logger.info(`Server running on port ${PORT}`, {
environment: process.env.NODE_ENV || 'development',
port: PORT,
});
});
// Initialize MCP server
mcpServer.start().catch((error) => {
logger.error('Failed to start MCP server', { error: error.message });
});
// Graceful shutdown
process.on('SIGTERM', () => {
logger.info('SIGTERM received, shutting down gracefully');
server.close(() => {
logger.info('Server closed');
process.exit(0);
});
});
process.on('SIGINT', () => {
logger.info('SIGINT received, shutting down gracefully');
server.close(() => {
logger.info('Server closed');
process.exit(0);
});
});
export default app;