fix: wrap top-level await in main() for CommonJS compatibility
This commit is contained in:
@@ -5,7 +5,6 @@ import fastifyWebSocket from '@fastify/websocket';
|
|||||||
import fastifyMultipart from '@fastify/multipart';
|
import fastifyMultipart from '@fastify/multipart';
|
||||||
import fastifyStatic from '@fastify/static';
|
import fastifyStatic from '@fastify/static';
|
||||||
import webpush from 'web-push';
|
import webpush from 'web-push';
|
||||||
import path from 'path';
|
|
||||||
import { initDB } from './db.js';
|
import { initDB } from './db.js';
|
||||||
import { setupWebSocket } from './ws.js';
|
import { setupWebSocket } from './ws.js';
|
||||||
import authRoutes from './routes/auth.js';
|
import authRoutes from './routes/auth.js';
|
||||||
@@ -15,57 +14,68 @@ import messageRoutes from './routes/messages.js';
|
|||||||
import userRoutes from './routes/users.js';
|
import userRoutes from './routes/users.js';
|
||||||
import pushRoutes from './routes/push.js';
|
import pushRoutes from './routes/push.js';
|
||||||
|
|
||||||
const app = Fastify({ logger: true });
|
async function main() {
|
||||||
|
const app = Fastify({ logger: true });
|
||||||
|
|
||||||
// VAPID
|
// VAPID
|
||||||
webpush.setVapidDetails(
|
if (process.env.VAPID_PUBLIC_KEY && process.env.VAPID_PRIVATE_KEY) {
|
||||||
|
webpush.setVapidDetails(
|
||||||
`mailto:${process.env.VAPID_EMAIL || 'admin@janichat.ru'}`,
|
`mailto:${process.env.VAPID_EMAIL || 'admin@janichat.ru'}`,
|
||||||
process.env.VAPID_PUBLIC_KEY!,
|
process.env.VAPID_PUBLIC_KEY,
|
||||||
process.env.VAPID_PRIVATE_KEY!
|
process.env.VAPID_PRIVATE_KEY
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Plugins
|
// Plugins
|
||||||
await app.register(fastifyCors, {
|
await app.register(fastifyCors, {
|
||||||
origin: (process.env.ALLOWED_ORIGINS || '').split(','),
|
origin: (process.env.ALLOWED_ORIGINS || '').split(','),
|
||||||
credentials: true,
|
credentials: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
await app.register(fastifyJwt, { secret: process.env.JWT_SECRET! });
|
await app.register(fastifyJwt, { secret: process.env.JWT_SECRET! });
|
||||||
|
|
||||||
await app.register(fastifyWebSocket);
|
await app.register(fastifyWebSocket);
|
||||||
|
|
||||||
await app.register(fastifyMultipart, {
|
await app.register(fastifyMultipart, {
|
||||||
limits: { fileSize: 50 * 1024 * 1024 },
|
limits: { fileSize: 50 * 1024 * 1024 },
|
||||||
});
|
});
|
||||||
|
|
||||||
await app.register(fastifyStatic, {
|
await app.register(fastifyStatic, {
|
||||||
root: '/uploads',
|
root: '/uploads',
|
||||||
prefix: '/uploads/',
|
prefix: '/uploads/',
|
||||||
});
|
});
|
||||||
|
|
||||||
// Auth decorator
|
// Auth decorator
|
||||||
app.decorate('authenticate', async function(request: any, reply: any) {
|
app.decorate('authenticate', async function(request: any, reply: any) {
|
||||||
try {
|
try {
|
||||||
await request.jwtVerify();
|
await request.jwtVerify();
|
||||||
} catch {
|
} catch {
|
||||||
reply.status(401).send({ error: 'Unauthorized' });
|
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' });
|
|
||||||
|
|||||||
Reference in New Issue
Block a user