From afab7f94a726056bee2c3739d6bc38a4f41eb99d Mon Sep 17 00:00:00 2001 From: wukko Date: Sat, 2 Dec 2023 22:01:58 +0600 Subject: [PATCH] api & web: ports in env are no longer strictly required --- docs/examples/docker-compose.example.yml | 2 -- src/cobalt.js | 10 +++++++--- src/core/api.js | 8 ++++---- src/core/web.js | 4 ++-- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/docs/examples/docker-compose.example.yml b/docs/examples/docker-compose.example.yml index 8a5f9d67..2262933f 100644 --- a/docs/examples/docker-compose.example.yml +++ b/docs/examples/docker-compose.example.yml @@ -17,7 +17,6 @@ services: #- 127.0.0.1:9000:9000 environment: - - apiPort=9000 # replace apiURL with your instance's target url in same format - apiURL=https://co.wuk.sh/ # replace apiName with your instance's distinctive name @@ -48,7 +47,6 @@ services: #- 127.0.0.1:9001:9001 environment: - - webPort=9001 # replace webURL with your instance's target url in same format - webURL=https://cobalt.tools/ # replace apiURL with preferred api instance url diff --git a/src/cobalt.js b/src/cobalt.js index 949cccba..6a148860 100644 --- a/src/cobalt.js +++ b/src/cobalt.js @@ -21,8 +21,8 @@ app.disable('x-powered-by'); await loadLoc(); -const apiMode = process.env.apiURL && process.env.apiPort && !((process.env.webURL && process.env.webPort) || (process.env.selfURL && process.env.port)); -const webMode = process.env.webURL && process.env.webPort && !((process.env.apiURL && process.env.apiPort) || (process.env.selfURL && process.env.port)); +const apiMode = process.env.apiURL && !process.env.webURL; +const webMode = process.env.webURL && !process.env.apiURL; if (apiMode) { const { runAPI } = await import('./core/api.js'); @@ -31,5 +31,9 @@ if (apiMode) { const { runWeb } = await import('./core/web.js'); await runWeb(express, app, gitCommit, gitBranch, __dirname) } else { - console.log(Red(`cobalt wasn't configured yet or configuration is invalid.\n`) + Bright(`please run the setup script to fix this: `) + Green(`npm run setup`)) + console.log( + Red(`cobalt wasn't configured yet or configuration is invalid.\n`) + + Bright(`please run the setup script to fix this: `) + + Green(`npm run setup`) + ) } diff --git a/src/core/api.js b/src/core/api.js index 84464b56..4e78fbb5 100644 --- a/src/core/api.js +++ b/src/core/api.js @@ -139,9 +139,9 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) { version: version, commit: gitCommit, branch: gitBranch, - name: process.env.apiName ? process.env.apiName : "unknown", + name: process.env.apiName || "unknown", url: process.env.apiURL, - cors: process.env.cors && process.env.cors === "0" ? 0 : 1, + cors: process.env?.cors === "0" ? 0 : 1, startTime: `${startTimestamp}` }); default: @@ -167,12 +167,12 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) { res.redirect('/api/json') }); - app.listen(process.env.apiPort, () => { + app.listen(process.env.apiPort || 9000, () => { console.log(`\n` + `${Cyan("cobalt")} API ${Bright(`v.${version}-${gitCommit} (${gitBranch})`)}\n` + `Start time: ${Bright(`${startTime.toUTCString()} (${startTimestamp})`)}\n\n` + `URL: ${Cyan(`${process.env.apiURL}`)}\n` + - `Port: ${process.env.apiPort}\n` + `Port: ${process.env.apiPort || 9000}\n` ) }); } diff --git a/src/core/web.js b/src/core/web.js index c2512c1f..08a6ffed 100644 --- a/src/core/web.js +++ b/src/core/web.js @@ -76,12 +76,12 @@ export async function runWeb(express, app, gitCommit, gitBranch, __dirname) { return res.redirect('/') }); - app.listen(process.env.webPort, () => { + app.listen(process.env.webPort || 9001, () => { console.log(`\n` + `${Cyan("cobalt")} WEB ${Bright(`v.${version}-${gitCommit} (${gitBranch})`)}\n` + `Start time: ${Bright(`${startTime.toUTCString()} (${startTimestamp})`)}\n\n` + `URL: ${Cyan(`${process.env.webURL}`)}\n` + - `Port: ${process.env.webPort}\n` + `Port: ${process.env.webPort || 9001}\n` ) }) }