From f638c4709bae6882814fb00da3ebec7f268e4057 Mon Sep 17 00:00:00 2001 From: b3ni15 Date: Sun, 21 Dec 2025 00:00:46 +0100 Subject: [PATCH] Refactor database connection logic; ensure DB is connected before starting the server --- src/index.js | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 2d0e170..85e11c6 100644 --- a/src/index.js +++ b/src/index.js @@ -8,6 +8,7 @@ import { WebSocketServer } from 'ws'; import authRoutes from './routes/auth.js'; import lobbyRoutes from './routes/lobby.js'; import walletRoutes from './routes/wallet.js'; +import pool from './db.js'; import { setupWebSocket } from './ws.js'; dotenv.config(); @@ -31,7 +32,25 @@ const wss = new WebSocketServer({ server }); setupWebSocket(wss); const PORT = Number(process.env.PORT || 4000); -server.listen(PORT, () => { - // eslint-disable-next-line no-console - console.log(`Backend fut: http://localhost:${PORT}`); -}); + +async function ensureDbConnected() { + try { + const connection = await pool.getConnection(); + await connection.ping(); + connection.release(); + } catch (err) { + // eslint-disable-next-line no-console + console.error('DB kapcsolat hiba inditas kozben:', err); + process.exit(1); + } +} + +async function start() { + await ensureDbConnected(); + server.listen(PORT, () => { + // eslint-disable-next-line no-console + console.log(`Backend fut: http://localhost:${PORT}`); + }); +} + +start();