| import express from 'express';
|
| import cors from 'cors';
|
| import helmet from 'helmet';
|
| import rateLimit from 'express-rate-limit';
|
| import path from 'path';
|
| import http from 'http';
|
| import { Server as SocketServer } from 'socket.io';
|
| import { config } from './config';
|
| import { connectDatabase } from './config/database';
|
| import { logger } from './utils/logger';
|
| import { errorHandler } from './middleware/errorHandler';
|
| import { setupMCPServer } from './services/mcpServer';
|
|
|
|
|
| import authRoutes from './routes/auth';
|
| import projectRoutes from './routes/projects';
|
| import videoRoutes from './routes/videos';
|
| import presetRoutes from './routes/presets';
|
| import assetRoutes from './routes/assets';
|
| import adminRoutes from './routes/admin';
|
|
|
| const app = express();
|
| const server = http.createServer(app);
|
|
|
|
|
| const io = new SocketServer(server, {
|
| cors: {
|
| origin: ['http://localhost:5173', 'http://localhost:3000'],
|
| methods: ['GET', 'POST'],
|
| credentials: true,
|
| },
|
| });
|
|
|
|
|
| app.use(helmet({ crossOriginResourcePolicy: { policy: 'cross-origin' } }));
|
| app.use(cors({
|
| origin: ['http://localhost:5173', 'http://localhost:3000'],
|
| credentials: true,
|
| }));
|
| app.use(express.json({ limit: '10mb' }));
|
| app.use(express.urlencoded({ extended: true }));
|
|
|
|
|
| const limiter = rateLimit({
|
| windowMs: 15 * 60 * 1000,
|
| max: 200,
|
| message: { error: 'Too many requests, please try again later.' },
|
| });
|
| app.use('/api/', limiter);
|
|
|
|
|
| app.use('/uploads', express.static(path.resolve(config.upload.dir)));
|
| app.use('/generated', express.static(path.resolve(config.upload.generatedDir)));
|
|
|
|
|
| app.use('/api/auth', authRoutes);
|
| app.use('/api/projects', projectRoutes);
|
| app.use('/api/videos', videoRoutes);
|
| app.use('/api/presets', presetRoutes);
|
| app.use('/api/assets', assetRoutes);
|
| app.use('/api/admin', adminRoutes);
|
|
|
|
|
| app.get('/api/health', (_req, res) => {
|
| res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
| });
|
|
|
|
|
| app.use(errorHandler);
|
|
|
|
|
| setupMCPServer(io);
|
|
|
|
|
| async function start(): Promise<void> {
|
| try {
|
| await connectDatabase();
|
|
|
| server.listen(config.port, () => {
|
| logger.info(`Director.AI server running on port ${config.port}`);
|
| logger.info(`WebSocket server ready for CLI bridge connections`);
|
| logger.info(`Environment: ${config.nodeEnv}`);
|
| });
|
| } catch (error) {
|
| logger.error('Failed to start server:', error);
|
| process.exit(1);
|
| }
|
| }
|
|
|
| start();
|
|
|