diff --git a/backend/src/index.ts b/backend/src/index.ts index e8b1047..2549c8b 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -5,7 +5,6 @@ import fastifyWebSocket from '@fastify/websocket'; import fastifyMultipart from '@fastify/multipart'; import fastifyStatic from '@fastify/static'; import webpush from 'web-push'; -import path from 'path'; import { initDB } from './db.js'; import { setupWebSocket } from './ws.js'; import authRoutes from './routes/auth.js'; @@ -15,57 +14,68 @@ import messageRoutes from './routes/messages.js'; import userRoutes from './routes/users.js'; import pushRoutes from './routes/push.js'; -const app = Fastify({ logger: true }); +async function main() { + const app = Fastify({ logger: true }); -// VAPID -webpush.setVapidDetails( - `mailto:${process.env.VAPID_EMAIL || 'admin@janichat.ru'}`, - process.env.VAPID_PUBLIC_KEY!, - process.env.VAPID_PRIVATE_KEY! -); - -// Plugins -await app.register(fastifyCors, { - origin: (process.env.ALLOWED_ORIGINS || '').split(','), - credentials: true, -}); - -await app.register(fastifyJwt, { secret: process.env.JWT_SECRET! }); - -await app.register(fastifyWebSocket); - -await app.register(fastifyMultipart, { - limits: { fileSize: 50 * 1024 * 1024 }, -}); - -await app.register(fastifyStatic, { - root: '/uploads', - prefix: '/uploads/', -}); - -// Auth decorator -app.decorate('authenticate', async function(request: any, reply: any) { - try { - await request.jwtVerify(); - } catch { - reply.status(401).send({ error: 'Unauthorized' }); + // VAPID + if (process.env.VAPID_PUBLIC_KEY && process.env.VAPID_PRIVATE_KEY) { + webpush.setVapidDetails( + `mailto:${process.env.VAPID_EMAIL || 'admin@janichat.ru'}`, + process.env.VAPID_PUBLIC_KEY, + process.env.VAPID_PRIVATE_KEY + ); } + + // Plugins + await app.register(fastifyCors, { + origin: (process.env.ALLOWED_ORIGINS || '').split(','), + credentials: true, + }); + + await app.register(fastifyJwt, { secret: process.env.JWT_SECRET! }); + + await app.register(fastifyWebSocket); + + await app.register(fastifyMultipart, { + limits: { fileSize: 50 * 1024 * 1024 }, + }); + + await app.register(fastifyStatic, { + root: '/uploads', + prefix: '/uploads/', + }); + + // Auth decorator + app.decorate('authenticate', async function(request: any, reply: any) { + try { + await request.jwtVerify(); + } catch { + reply.status(401).send({ error: 'Unauthorized' }); + } + }); + + // Routes + await app.register(authRoutes, { prefix: '/api/auth' }); + await app.register(adminRoutes, { prefix: '/api/admin' }); + await app.register(chatRoutes, { prefix: '/api/chats' }); + await app.register(messageRoutes, { prefix: '/api/messages' }); + await app.register(userRoutes, { prefix: '/api/users' }); + await app.register(pushRoutes, { prefix: '/api/push' }); + + // Health check + app.get('/health', async () => ({ ok: true })); + + // WebSocket + setupWebSocket(app); + + // Init DB + await initDB(); + + // Start + await app.listen({ port: parseInt(process.env.PORT || '3000'), host: '0.0.0.0' }); +} + +main().catch(err => { + console.error(err); + process.exit(1); }); - -// Routes -await app.register(authRoutes, { prefix: '/api/auth' }); -await app.register(adminRoutes, { prefix: '/api/admin' }); -await app.register(chatRoutes, { prefix: '/api/chats' }); -await app.register(messageRoutes, { prefix: '/api/messages' }); -await app.register(userRoutes, { prefix: '/api/users' }); -await app.register(pushRoutes, { prefix: '/api/push' }); - -// Health check -app.get('/health', async () => ({ ok: true })); - -// WebSocket -setupWebSocket(app); - -// Init -await initDB(); -await app.listen({ port: parseInt(process.env.PORT || '3000'), host: '0.0.0.0' });