2026-06-26 10:50:24 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Server – Fastify configuration with CORS, static, and WebSocket
|
|
|
|
|
|
*/
|
|
|
|
|
|
import Fastify from 'fastify';
|
|
|
|
|
|
import cors from '@fastify/cors';
|
|
|
|
|
|
import staticPlugin from '@fastify/static';
|
|
|
|
|
|
import websocket from '@fastify/websocket';
|
|
|
|
|
|
import { join, dirname } from 'path';
|
|
|
|
|
|
import { fileURLToPath } from 'url';
|
|
|
|
|
|
import type { DatabaseInterface } from './database/DatabaseInterface.js';
|
|
|
|
|
|
import { AuthService } from './auth/AuthService.js';
|
|
|
|
|
|
import { registerYjsWebSocket, initYjsPersistence, closeYjsPersistence } from './websocket/yjsServer.js';
|
|
|
|
|
|
|
|
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
|
|
|
|
|
|
|
|
export interface ServerOptions {
|
|
|
|
|
|
port?: number;
|
|
|
|
|
|
host?: string;
|
|
|
|
|
|
db: DatabaseInterface;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function createServer(opts: ServerOptions) {
|
|
|
|
|
|
const fastify = Fastify({ logger: true });
|
|
|
|
|
|
|
|
|
|
|
|
// CORS
|
|
|
|
|
|
await fastify.register(cors, {
|
|
|
|
|
|
origin: true,
|
|
|
|
|
|
methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// WebSocket
|
|
|
|
|
|
await fastify.register(websocket, {
|
|
|
|
|
|
options: { maxPayload: 1048576 },
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Static files (serve frontend build if exists)
|
|
|
|
|
|
const frontendDist = join(__dirname, '..', '..', 'frontend', 'dist');
|
|
|
|
|
|
try {
|
|
|
|
|
|
await fastify.register(staticPlugin, {
|
|
|
|
|
|
root: frontendDist,
|
|
|
|
|
|
prefix: '/',
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// Frontend dist may not exist in dev mode
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Health check
|
|
|
|
|
|
fastify.get('/api/health', async () => ({ status: 'ok', timestamp: new Date().toISOString() }));
|
|
|
|
|
|
|
|
|
|
|
|
// Register route modules
|
|
|
|
|
|
const { registerProjectRoutes } = await import('./routes/projects.js');
|
|
|
|
|
|
const { registerDrawingRoutes } = await import('./routes/drawings.js');
|
|
|
|
|
|
const { registerLayerRoutes } = await import('./routes/layers.js');
|
|
|
|
|
|
const { registerElementRoutes } = await import('./routes/elements.js');
|
|
|
|
|
|
const { registerBlockRoutes } = await import('./routes/blocks.js');
|
|
|
|
|
|
const { registerSettingsRoutes } = await import('./routes/settings.js');
|
|
|
|
|
|
const { registerAuthRoutes } = await import('./routes/auth.js');
|
|
|
|
|
|
const { registerUserRoutes } = await import('./routes/users.js');
|
|
|
|
|
|
const { registerAIRoutes } = await import('./routes/ai.js');
|
2026-06-28 13:52:54 +02:00
|
|
|
|
const { registerNotificationRoutes } = await import('./routes/notifications.js');
|
|
|
|
|
|
const { registerShareRoutes } = await import('./routes/shares.js');
|
2026-06-26 10:50:24 +02:00
|
|
|
|
|
|
|
|
|
|
const authService = new AuthService(opts.db);
|
|
|
|
|
|
|
|
|
|
|
|
registerAuthRoutes(fastify, authService);
|
|
|
|
|
|
registerProjectRoutes(fastify, opts.db);
|
|
|
|
|
|
registerDrawingRoutes(fastify, opts.db);
|
|
|
|
|
|
registerLayerRoutes(fastify, opts.db);
|
|
|
|
|
|
registerElementRoutes(fastify, opts.db);
|
|
|
|
|
|
registerBlockRoutes(fastify, opts.db);
|
|
|
|
|
|
registerSettingsRoutes(fastify, opts.db);
|
|
|
|
|
|
registerUserRoutes(fastify, opts.db, authService);
|
|
|
|
|
|
registerAIRoutes(fastify, authService);
|
2026-06-28 13:52:54 +02:00
|
|
|
|
registerNotificationRoutes(fastify, opts.db, authService);
|
|
|
|
|
|
registerShareRoutes(fastify, opts.db, authService);
|
2026-06-26 10:50:24 +02:00
|
|
|
|
|
|
|
|
|
|
// Yjs collaboration WebSocket
|
|
|
|
|
|
registerYjsWebSocket(fastify);
|
|
|
|
|
|
|
|
|
|
|
|
return fastify;
|
|
|
|
|
|
}
|