feat: initial commit web-cad-neu with docker-compose, frontend and backend
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* 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 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);
|
||||
|
||||
// Yjs collaboration WebSocket
|
||||
registerYjsWebSocket(fastify);
|
||||
|
||||
return fastify;
|
||||
}
|
||||
Reference in New Issue
Block a user