From 85cd083750b0068aab0112e73630e680dde98f3a Mon Sep 17 00:00:00 2001 From: Anika Raemer Date: Sun, 21 Sep 2025 20:09:25 +0200 Subject: [PATCH] add migrations to start up --- src/index.ts | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/index.ts b/src/index.ts index 43d7c5b..b26b7f8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,22 +13,35 @@ const app = express(); app.use(express.json()); app.use(errorHandler); -const PORT = process.env.PORT ? parseInt(process.env.PORT) : 4000; -const HOST = process.env.HOST || "localhost"; -app.use("/auth", authRoutes); -app.use("/user", userRoutes); -//app.use("/recipe", recipePoint); +async function startServer() { + try { + // 1️⃣ Initialize database + await AppDataSource.initialize(); + console.log("Data Source initialized"); -app.get("*", (req: Request, res: Response) => { - res.status(505).json({ message: "Bad Request" }); -}); + // Optional: run pending migrations + await AppDataSource.runMigrations(); + console.log("Migrations executed"); -AppDataSource.initialize() - .then(async () => { - app.listen(PORT, HOST, () => { - console.log(`Server is running on http://${HOST}:${PORT}`); + // 2️⃣ Setup routes + app.use("/auth", authRoutes); + app.use("/user", userRoutes); + + app.get("*", (req: Request, res: Response) => { + res.status(505).json({ message: "Bad Request" }); }); - console.log("Data Source has been initialized!"); - }) - .catch((error) => console.log(error)); + + // 3️⃣ Start listening + const PORT = parseInt(process.env.PORT || "4000", 10); + const HOST = process.env.HOST || "localhost"; + app.listen(PORT, HOST, () => { + console.log(`Server running on http://${HOST}:${PORT}`); + }); + } catch (err) { + console.error("Error during Data Source initialization:", err); + } +} + +// Call the async start function +startServer(); \ No newline at end of file