fix: abort startup if not connected to MongoDB server (#665)
This commit is contained in:
parent
16d98636e9
commit
e4613069b3
16
src/index.ts
16
src/index.ts
@ -7,14 +7,16 @@ import https from "https";
|
|||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
import { app } from "./app";
|
import { app } from "./app";
|
||||||
import { config, validateConfig } from "./services/configService";
|
import { config, validateConfig } from "./services/configService";
|
||||||
import { connectDatabase } from "@/src/services/mongoService";
|
|
||||||
import { registerLogFileCreationListener } from "@/src/utils/logger";
|
import { registerLogFileCreationListener } from "@/src/utils/logger";
|
||||||
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
registerLogFileCreationListener();
|
registerLogFileCreationListener();
|
||||||
validateConfig();
|
validateConfig();
|
||||||
|
|
||||||
void (async (): Promise<void> => {
|
mongoose
|
||||||
await connectDatabase();
|
.connect(config.mongodbUrl)
|
||||||
|
.then(() => {
|
||||||
|
logger.info("Connected to MongoDB");
|
||||||
|
|
||||||
const httpPort = config.httpPort || 80;
|
const httpPort = config.httpPort || 80;
|
||||||
const httpsPort = config.httpsPort || 443;
|
const httpsPort = config.httpsPort || 443;
|
||||||
@ -33,4 +35,10 @@ void (async (): Promise<void> => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
})();
|
})
|
||||||
|
.catch(error => {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
logger.error(`Error connecting to MongoDB server: ${error.message}`);
|
||||||
|
}
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
@ -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<void> => {
|
|
||||||
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}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
Loading…
x
Reference in New Issue
Block a user