improve: await database connection before listening for HTTP traffic

This commit is contained in:
Sainan 2024-12-23 21:06:38 +01:00
parent d50c6b8c76
commit a3c73459eb
3 changed files with 19 additions and 22 deletions

View File

@ -13,11 +13,6 @@ import { customRouter } from "@/src/routes/custom";
import { dynamicController } from "@/src/routes/dynamic";
import { statsRouter } from "@/src/routes/stats";
import { webuiRouter } from "@/src/routes/webui";
import { connectDatabase } from "@/src/services/mongoService";
import { registerLogFileCreationListener } from "@/src/utils/logger";
void registerLogFileCreationListener();
void connectDatabase();
const app = express();

View File

@ -7,21 +7,25 @@ import https from "https";
import fs from "node:fs";
import { app } from "./app";
import { config } from "./services/configService";
//const morgan = require("morgan");
//const bodyParser = require("body-parser");
import { connectDatabase } from "@/src/services/mongoService";
import { registerLogFileCreationListener } from "@/src/utils/logger";
const options = {
key: fs.readFileSync("static/certs/key.pem"),
cert: fs.readFileSync("static/certs/cert.pem")
};
registerLogFileCreationListener();
const httpPort = config.httpPort || 80;
const httpsPort = config.httpsPort || 443;
void (async (): Promise<void> => {
await connectDatabase();
// const server = http.createServer(app).listen(80);
http.createServer(app).listen(httpPort, () => logger.info("HTTP server started on port " + httpPort));
const server = https.createServer(options, app);
server.listen(httpsPort, () => logger.info("HTTPS server started on port " + httpsPort));
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")
};
// server.keepAliveTimeout = 60 * 1000 + 1000;
// server.headersTimeout = 60 * 1000 + 2000;
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);
});
});
})();

View File

@ -8,7 +8,7 @@ if (url === undefined) {
throw new Error("MONGODB_URL not found. Make sure you have a .env file in the root of the project!");
}
const connectDatabase = async () => {
export const connectDatabase = async (): Promise<void> => {
try {
await mongoose.connect(`${url}`);
logger.info("Connected to MongoDB");
@ -18,5 +18,3 @@ const connectDatabase = async () => {
}
}
};
export { connectDatabase };