| 29 | } |
| 30 | |
| 31 | function setupHttp( |
| 32 | koaApp: Koa, |
| 33 | ssl: boolean, |
| 34 | sslPemPath: string, |
| 35 | sslKeyPath: string, |
| 36 | port: number, |
| 37 | host?: string |
| 38 | ) { |
| 39 | let httpServer: http.Server | https.Server; |
| 40 | |
| 41 | if (ssl) { |
| 42 | const options = { |
| 43 | cert: fs.readFileSync(path.join(sslPemPath)), |
| 44 | key: fs.readFileSync(path.join(sslKeyPath)) |
| 45 | }; |
| 46 | httpServer = https.createServer(options, koaApp.callback()); |
| 47 | } else { |
| 48 | httpServer = http.createServer(koaApp.callback()); |
| 49 | } |
| 50 | |
| 51 | httpServer.on("error", (err) => { |
| 52 | logger.error($t("TXT_CODE_app.httpSetupError")); |
| 53 | logger.error(err); |
| 54 | process.exit(1); |
| 55 | }); |
| 56 | |
| 57 | httpServer.listen(port, host); |
| 58 | logger.info("=================================="); |
| 59 | logger.info($t("TXT_CODE_app.panelStarted")); |
| 60 | logger.info($t("TXT_CODE_app.reference")); |
| 61 | let appHost = $t("TXT_CODE_app.host", { port }); |
| 62 | if (ssl) appHost = appHost.replace("http", "https"); |
| 63 | logger.info(appHost); |
| 64 | logger.info($t("TXT_CODE_app.portTip", { port })); |
| 65 | logger.info($t("TXT_CODE_app.exitTip", { port })); |
| 66 | logger.info("=================================="); |
| 67 | |
| 68 | if (os.platform() == "win32" && hasParams("--open")) { |
| 69 | open(ssl ? `https://localhost:${port}/` : `http://localhost:${port}/`); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | async function processExit() { |
| 74 | try { |