20432f4b47
- Backend: notifications + shares routes, DB tables, SqliteAdapter methods - Frontend: NotificationPanel (bell icon + dropdown), ShareDialog, Dashboard integration - Styles: notification + share dialog CSS
82 lines
2.8 KiB
TypeScript
82 lines
2.8 KiB
TypeScript
/**
|
||
* 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');
|
||
const { registerNotificationRoutes } = await import('./routes/notifications.js');
|
||
const { registerShareRoutes } = await import('./routes/shares.js');
|
||
|
||
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);
|
||
registerNotificationRoutes(fastify, opts.db, authService);
|
||
registerShareRoutes(fastify, opts.db, authService);
|
||
|
||
// Yjs collaboration WebSocket
|
||
registerYjsWebSocket(fastify);
|
||
|
||
return fastify;
|
||
}
|