返回提交历史
Modified
src/index.ts
+25
-17
Deleted
src/services/mongoService.ts
+0
-20
XFEstudio/XFESpaceNinjaServer
fix: abort startup if not connected to MongoDB server (#665)
e4613069
代码差异
2 个文件
+25
-37
@@ -7,30 +7,38 @@ import https from "https";
7
7
import fs from "node:fs";
8
8
import { app } from "./app";
9
9
import { config, validateConfig } from "./services/configService";
10
import { connectDatabase } from "@/src/services/mongoService";
11
10
import { registerLogFileCreationListener } from "@/src/utils/logger";
11
import mongoose from "mongoose";
12
12
13
13
registerLogFileCreationListener();
14
14
validateConfig();
15
15
16
void (async (): Promise<void> => {
17
await connectDatabase();
16
mongoose
17
.connect(config.mongodbUrl)
18
.then(() => {
19
logger.info("Connected to MongoDB");
18
20
19
const httpPort = config.httpPort || 80;
20
const httpsPort = config.httpsPort || 443;
21
const options = {
22
key: fs.readFileSync("static/certs/key.pem"),
23
cert: fs.readFileSync("static/certs/cert.pem")
24
};
21
const httpPort = config.httpPort || 80;
22
const httpsPort = config.httpsPort || 443;
23
const options = {
24
key: fs.readFileSync("static/certs/key.pem"),
25
cert: fs.readFileSync("static/certs/cert.pem")
26
};
25
27
26
http.createServer(app).listen(httpPort, () => {
27
logger.info("HTTP server started on port " + httpPort);
28
https.createServer(options, app).listen(httpsPort, () => {
29
logger.info("HTTPS server started on port " + httpsPort);
28
http.createServer(app).listen(httpPort, () => {
29
logger.info("HTTP server started on port " + httpPort);
30
https.createServer(options, app).listen(httpsPort, () => {
31
logger.info("HTTPS server started on port " + httpsPort);
30
32
31
logger.info(
32
"Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort)
33
);
33
logger.info(
34
"Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort)
35
);
36
});
34
37
});
38
})
39
.catch(error => {
40
if (error instanceof Error) {
41
logger.error(`Error connecting to MongoDB server: ${error.message}`);
42
}
43
process.exit(1);
35
44
});
36
})();
@@ -1,20 +0,0 @@
1
import { logger } from "@/src/utils/logger";
2
import { config } from "@/src/services/configService";
3
import mongoose from "mongoose";
4
5
const url = config.mongodbUrl;
6
7
if (url === undefined) {
8
throw new Error("MONGODB_URL not found. Make sure you have a .env file in the root of the project!");
9
}
10
11
export const connectDatabase = async (): Promise<void> => {
12
try {
13
await mongoose.connect(`${url}`);
14
logger.info("Connected to MongoDB");
15
} catch (error: unknown) {
16
if (error instanceof Error) {
17
logger.error(`Error connecting to MongoDB ${error.message}`);
18
}
19
}
20
};