From d897f40c9211b9d6ae1324123ea8b2feab2300f9 Mon Sep 17 00:00:00 2001 From: Sainan Date: Mon, 30 Dec 2024 04:11:32 +0100 Subject: [PATCH] fix: abort startup if not connected to MongoDB server --- src/index.ts | 42 +++++++++++++++++++++--------------- src/services/mongoService.ts | 20 ----------------- 2 files changed, 25 insertions(+), 37 deletions(-) delete mode 100644 src/services/mongoService.ts diff --git a/src/index.ts b/src/index.ts index a6bfb654..acf55bdb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,30 +7,38 @@ import https from "https"; import fs from "node:fs"; import { app } from "./app"; import { config, validateConfig } from "./services/configService"; -import { connectDatabase } from "@/src/services/mongoService"; import { registerLogFileCreationListener } from "@/src/utils/logger"; +import mongoose from "mongoose"; registerLogFileCreationListener(); validateConfig(); -void (async (): Promise => { - await connectDatabase(); +mongoose + .connect(config.mongodbUrl) + .then(() => { + logger.info("Connected to MongoDB"); - const httpPort = config.httpPort || 80; - const httpsPort = config.httpsPort || 443; - const options = { - key: fs.readFileSync("static/certs/key.pem"), - cert: fs.readFileSync("static/certs/cert.pem") - }; + const httpPort = config.httpPort || 80; + const httpsPort = config.httpsPort || 443; + const options = { + key: fs.readFileSync("static/certs/key.pem"), + cert: fs.readFileSync("static/certs/cert.pem") + }; - http.createServer(app).listen(httpPort, () => { - logger.info("HTTP server started on port " + httpPort); - https.createServer(options, app).listen(httpsPort, () => { - logger.info("HTTPS server started on port " + httpsPort); + http.createServer(app).listen(httpPort, () => { + logger.info("HTTP server started on port " + httpPort); + https.createServer(options, app).listen(httpsPort, () => { + logger.info("HTTPS server started on port " + httpsPort); - logger.info( - "Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort) - ); + logger.info( + "Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort) + ); + }); }); + }) + .catch(error => { + if (error instanceof Error) { + logger.error(`Error connecting to MongoDB server: ${error.message}`); + } + process.exit(1); }); -})(); diff --git a/src/services/mongoService.ts b/src/services/mongoService.ts deleted file mode 100644 index ef6de535..00000000 --- a/src/services/mongoService.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { logger } from "@/src/utils/logger"; -import { config } from "@/src/services/configService"; -import mongoose from "mongoose"; - -const url = config.mongodbUrl; - -if (url === undefined) { - throw new Error("MONGODB_URL not found. Make sure you have a .env file in the root of the project!"); -} - -export const connectDatabase = async (): Promise => { - try { - await mongoose.connect(`${url}`); - logger.info("Connected to MongoDB"); - } catch (error: unknown) { - if (error instanceof Error) { - logger.error(`Error connecting to MongoDB ${error.message}`); - } - } -};