2025-05-24 01:48:25 -07:00
|
|
|
import http from "http";
|
|
|
|
import https from "https";
|
|
|
|
import fs from "node:fs";
|
|
|
|
import { config } from "./configService";
|
|
|
|
import { logger } from "../utils/logger";
|
|
|
|
import { app } from "../app";
|
|
|
|
import { AddressInfo } from "node:net";
|
2025-06-20 14:00:55 -07:00
|
|
|
import ws from "ws";
|
2025-05-24 01:48:25 -07:00
|
|
|
|
|
|
|
let httpServer: http.Server | undefined;
|
|
|
|
let httpsServer: https.Server | undefined;
|
2025-06-20 14:00:55 -07:00
|
|
|
let wsServer: ws.Server | undefined;
|
|
|
|
let wssServer: ws.Server | undefined;
|
2025-05-24 01:48:25 -07:00
|
|
|
|
|
|
|
const tlsOptions = {
|
|
|
|
key: fs.readFileSync("static/certs/key.pem"),
|
|
|
|
cert: fs.readFileSync("static/certs/cert.pem")
|
|
|
|
};
|
|
|
|
|
|
|
|
export const startWebServer = (): void => {
|
|
|
|
const httpPort = config.httpPort || 80;
|
|
|
|
const httpsPort = config.httpsPort || 443;
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
|
|
httpServer = http.createServer(app);
|
|
|
|
httpServer.listen(httpPort, () => {
|
2025-06-20 14:00:55 -07:00
|
|
|
wsServer = new ws.Server({ server: httpServer });
|
|
|
|
//wsServer.on("connection", wsOnConnect);
|
|
|
|
|
2025-05-24 01:48:25 -07:00
|
|
|
logger.info("HTTP server started on port " + httpPort);
|
2025-06-20 14:00:55 -07:00
|
|
|
|
2025-05-24 01:48:25 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
|
|
httpsServer = https.createServer(tlsOptions, app);
|
|
|
|
httpsServer.listen(httpsPort, () => {
|
2025-06-20 14:00:55 -07:00
|
|
|
wssServer = new ws.Server({ server: httpsServer });
|
|
|
|
//wssServer.on("connection", wsOnConnect);
|
|
|
|
|
2025-05-24 01:48:25 -07:00
|
|
|
logger.info("HTTPS server started on port " + httpsPort);
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getWebPorts = (): Record<"http" | "https", number | undefined> => {
|
|
|
|
return {
|
|
|
|
http: (httpServer?.address() as AddressInfo | undefined)?.port,
|
|
|
|
https: (httpsServer?.address() as AddressInfo | undefined)?.port
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const stopWebServer = async (): Promise<void> => {
|
|
|
|
const promises: Promise<void>[] = [];
|
|
|
|
if (httpServer) {
|
|
|
|
promises.push(
|
|
|
|
new Promise(resolve => {
|
|
|
|
httpServer!.close(() => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (httpsServer) {
|
|
|
|
promises.push(
|
|
|
|
new Promise(resolve => {
|
|
|
|
httpsServer!.close(() => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2025-06-20 14:00:55 -07:00
|
|
|
if (wsServer) {
|
|
|
|
promises.push(
|
|
|
|
new Promise(resolve => {
|
|
|
|
wsServer!.close(() => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (wssServer) {
|
|
|
|
promises.push(
|
|
|
|
new Promise(resolve => {
|
|
|
|
wssServer!.close(() => {
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2025-05-24 01:48:25 -07:00
|
|
|
await Promise.all(promises);
|
|
|
|
};
|
2025-06-20 14:00:55 -07:00
|
|
|
|
|
|
|
/*const wsOnConnect = (ws: ws, _req: http.IncomingMessage): void => {
|
|
|
|
ws.on("message", console.log);
|
|
|
|
};*/
|
|
|
|
|
|
|
|
export const sendWsBroadcast = <T>(data: T): void => {
|
|
|
|
const msg = JSON.stringify(data);
|
|
|
|
if (wsServer) {
|
|
|
|
for (const client of wsServer.clients) {
|
|
|
|
client.send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wssServer) {
|
|
|
|
for (const client of wssServer.clients) {
|
|
|
|
client.send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|