返回提交历史
Modified
src/index.ts
+3
-24
Modified
src/services/configWatcherService.ts
+7
-0
Added
src/services/webService.ts
+65
-0
XFEstudio/XFESpaceNinjaServer
chore: restart web server when ports in config have changed (#2100)
Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/2100 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
d41e4f7f
代码差异
3 个文件
+75
-24
@@ -12,12 +12,10 @@ import { logger } from "@/src/utils/logger";
12
12
logger.info("Starting up...");
13
13
14
14
// Proceed with normal startup: bring up config watcher service, validate config, connect to MongoDB, and finally start listening for HTTP.
15
import http from "http";
16
import https from "https";
17
import fs from "node:fs";
18
import { app } from "./app";
19
15
import mongoose from "mongoose";
20
16
import { JSONStringify } from "json-with-bigint";
17
import { startWebServer } from "./services/webService";
18
21
19
import { validateConfig } from "@/src/services/configWatcherService";
22
20
23
21
// Patch JSON.stringify to work flawlessly with Bigints.
@@ -29,26 +27,7 @@ mongoose
29
27
.connect(config.mongodbUrl)
30
28
.then(() => {
31
29
logger.info("Connected to MongoDB");
32
33
const httpPort = config.httpPort || 80;
34
const httpsPort = config.httpsPort || 443;
35
const options = {
36
key: fs.readFileSync("static/certs/key.pem"),
37
cert: fs.readFileSync("static/certs/cert.pem")
38
};
39
40
// eslint-disable-next-line @typescript-eslint/no-misused-promises
41
http.createServer(app).listen(httpPort, () => {
42
logger.info("HTTP server started on port " + httpPort);
43
// eslint-disable-next-line @typescript-eslint/no-misused-promises
44
https.createServer(options, app).listen(httpsPort, () => {
45
logger.info("HTTPS server started on port " + httpsPort);
46
47
logger.info(
48
"Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort)
49
);
50
});
51
});
30
startWebServer();
52
31
})
53
32
.catch(error => {
54
33
if (error instanceof Error) {
@@ -2,6 +2,7 @@ import fs from "fs";
2
2
import fsPromises from "fs/promises";
3
3
import { logger } from "../utils/logger";
4
4
import { config, configPath, loadConfig } from "./configService";
5
import { getWebPorts, startWebServer, stopWebServer } from "./webService";
5
6
6
7
let amnesia = false;
7
8
fs.watchFile(configPath, () => {
@@ -16,6 +17,12 @@ fs.watchFile(configPath, () => {
16
17
process.exit(1);
17
18
}
18
19
validateConfig();
20
21
const webPorts = getWebPorts();
22
if (config.httpPort != webPorts.http || config.httpsPort != webPorts.https) {
23
logger.info(`Restarting web server to apply port changes.`);
24
void stopWebServer().then(startWebServer);
25
}
19
26
}
20
27
});
21
28
@@ -0,0 +1,65 @@
1
import http from "http";
2
import https from "https";
3
import fs from "node:fs";
4
import { config } from "./configService";
5
import { logger } from "../utils/logger";
6
import { app } from "../app";
7
import { AddressInfo } from "node:net";
8
9
let httpServer: http.Server | undefined;
10
let httpsServer: https.Server | undefined;
11
12
const tlsOptions = {
13
key: fs.readFileSync("static/certs/key.pem"),
14
cert: fs.readFileSync("static/certs/cert.pem")
15
};
16
17
export const startWebServer = (): void => {
18
const httpPort = config.httpPort || 80;
19
const httpsPort = config.httpsPort || 443;
20
21
// eslint-disable-next-line @typescript-eslint/no-misused-promises
22
httpServer = http.createServer(app);
23
httpServer.listen(httpPort, () => {
24
logger.info("HTTP server started on port " + httpPort);
25
// eslint-disable-next-line @typescript-eslint/no-misused-promises
26
httpsServer = https.createServer(tlsOptions, app);
27
httpsServer.listen(httpsPort, () => {
28
logger.info("HTTPS server started on port " + httpsPort);
29
30
logger.info(
31
"Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort)
32
);
33
});
34
});
35
};
36
37
export const getWebPorts = (): Record<"http" | "https", number | undefined> => {
38
return {
39
http: (httpServer?.address() as AddressInfo | undefined)?.port,
40
https: (httpsServer?.address() as AddressInfo | undefined)?.port
41
};
42
};
43
44
export const stopWebServer = async (): Promise<void> => {
45
const promises: Promise<void>[] = [];
46
if (httpServer) {
47
promises.push(
48
new Promise(resolve => {
49
httpServer!.close(() => {
50
resolve();
51
});
52
})
53
);
54
}
55
if (httpsServer) {
56
promises.push(
57
new Promise(resolve => {
58
httpsServer!.close(() => {
59
resolve();
60
});
61
})
62
);
63
}
64
await Promise.all(promises);
65
};