make all environment variables consistent & list them in docs (#380)

This commit is contained in:
wukko 2024-03-05 20:19:01 +06:00 committed by GitHub
commit db6a75529d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 94 additions and 47 deletions

View file

@ -13,17 +13,17 @@ services:
ports: ports:
- 9000:9000/tcp - 9000:9000/tcp
# if you're using a reverse proxy, uncomment the next line: # if you're using a reverse proxy, uncomment the next line and remove the one above (9000:9000/tcp):
#- 127.0.0.1:9000:9000 #- 127.0.0.1:9000:9000
environment: environment:
# replace apiURL with your instance's target url in same format # replace https://co.wuk.sh/ with your instance's target url in same format
- apiURL=https://co.wuk.sh/ - API_URL=https://co.wuk.sh/
# replace apiName with your instance's distinctive name # replace eu-nl with your instance's distinctive name
- apiName=eu-nl - API_NAME=eu-nl
# if you want to use cookies when fetching data from services, uncomment the next line # if you want to use cookies when fetching data from services, uncomment the next line
#- cookiePath=/cookies.json #- COOKIE_PATH=/cookies.json
# see cookies_example.json for example file. # see cookies.example.json for example file.
labels: labels:
- com.centurylinklabs.watchtower.scope=cobalt - com.centurylinklabs.watchtower.scope=cobalt
@ -43,14 +43,14 @@ services:
ports: ports:
- 9001:9001/tcp - 9001:9001/tcp
# if you're using a reverse proxy, uncomment the next line: # if you're using a reverse proxy, uncomment the next line and remove the one above (9001:9001/tcp):
#- 127.0.0.1:9001:9001 #- 127.0.0.1:9001:9001
environment: environment:
# replace webURL with your instance's target url in same format # replace https://cobalt.tools/ with your instance's target url in same format
- webURL=https://cobalt.tools/ - WEB_URL=https://cobalt.tools/
# replace apiURL with preferred api instance url # replace https://co.wuk.sh/ with preferred api instance url
- apiURL=https://co.wuk.sh/ - API_URL=https://co.wuk.sh/
labels: labels:
- com.centurylinklabs.watchtower.scope=cobalt - com.centurylinklabs.watchtower.scope=cobalt

View file

@ -47,3 +47,25 @@ setup script installs all needed `npm` dependencies, but you have to install `no
sudo apt install nscd sudo apt install nscd
sudo service nscd start sudo service nscd start
``` ```
## list of all environment variables
### variables for api
| variable name | default | example | description |
|:----------------------|:----------|:------------------------|:------------|
| `API_PORT` | `9000` | `9000` | changes port from which api server is accessible. |
| `API_URL` | | `https://co.wuk.sh/` | changes url from which api server is accessible. <br> ***REQUIRED TO RUN API***. |
| `API_NAME` | `unknown` | `ams-1` | api server name that is shown in `/api/serverInfo`. |
| `CORS_WILDCARD` | `1` | `0` | toggles cross-origin resource sharing. <br> `0`: disabled. `1`: enabled. |
| `CORS_URL` | not used | `https://cobalt.tools/` | cross-origin resource sharing url. api will be available only from this url if `CORS_WILDCARD` is set to `0`. |
| `COOKIE_PATH` | not used | `/cookies.json` | path for cookie file relative to main folder. |
| `PROCESSING_PRIORITY` | not used | `10` | changes `nice` value* for ffmpeg subprocess. available only on unix systems. |
\* the higher the nice value, the lower the priority. [read more here](https://en.wikipedia.org/wiki/Nice_(Unix)).
### variables for web
| variable name | default | example | description |
|:--------------- |:--------|:------------------------|:--------------------------------------------------------------------------------------|
| `WEB_PORT` | `9001` | `9001` | changes port from which frontend server is accessible. |
| `WEB_URL` | | `https://cobalt.tools/` | changes url from which frontend server is accessible. <br> ***REQUIRED TO RUN WEB***. |
| `SHOW_SPONSORS` | `0` | `1` | toggles sponsor list in about popup. <br> `0`: disabled. `1`: enabled. |
| `IS_BETA` | `0` | `1` | toggles beta tag next to cobalt logo. <br> `0`: disabled. `1`: enabled. |

View file

@ -1,4 +1,5 @@
import "dotenv/config"; import "dotenv/config";
import "./modules/sub/alias-envs.js";
import express from "express"; import express from "express";
@ -21,8 +22,8 @@ app.disable('x-powered-by');
await loadLoc(); await loadLoc();
const apiMode = process.env.apiURL && !process.env.webURL; const apiMode = process.env.API_URL && !process.env.WEB_URL;
const webMode = process.env.webURL && process.env.apiURL; const webMode = process.env.WEB_URL && process.env.API_URL;
if (apiMode) { if (apiMode) {
const { runAPI } = await import('./core/api.js'); const { runAPI } = await import('./core/api.js');

View file

@ -14,7 +14,7 @@ import { sha256 } from "../modules/sub/crypto.js";
import { verifyStream } from "../modules/stream/manage.js"; import { verifyStream } from "../modules/stream/manage.js";
export function runAPI(express, app, gitCommit, gitBranch, __dirname) { export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
const corsConfig = process.env.cors === '0' ? { const corsConfig = process.env.CORS_WILDCARD === '0' ? {
origin: process.env.CORS_URL, origin: process.env.CORS_URL,
optionsSuccessStatus: 200 optionsSuccessStatus: 200
} : {}; } : {};
@ -141,9 +141,9 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
version: version, version: version,
commit: gitCommit, commit: gitCommit,
branch: gitBranch, branch: gitBranch,
name: process.env.apiName || "unknown", name: process.env.API_NAME || "unknown",
url: process.env.apiURL, url: process.env.API_URL,
cors: process.env?.cors === "0" ? 0 : 1, cors: process.env?.CORS_WILDCARD === "0" ? 0 : 1,
startTime: `${startTimestamp}` startTime: `${startTimestamp}`
}); });
default: default:
@ -169,12 +169,12 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) {
res.redirect('/api/json') res.redirect('/api/json')
}); });
app.listen(process.env.apiPort || 9000, () => { app.listen(process.env.API_PORT || 9000, () => {
console.log(`\n` + console.log(`\n` +
`${Cyan("cobalt")} API ${Bright(`v.${version}-${gitCommit} (${gitBranch})`)}\n` + `${Cyan("cobalt")} API ${Bright(`v.${version}-${gitCommit} (${gitBranch})`)}\n` +
`Start time: ${Bright(`${startTime.toUTCString()} (${startTimestamp})`)}\n\n` + `Start time: ${Bright(`${startTime.toUTCString()} (${startTimestamp})`)}\n\n` +
`URL: ${Cyan(`${process.env.apiURL}`)}\n` + `URL: ${Cyan(`${process.env.API_URL}`)}\n` +
`Port: ${process.env.apiPort || 9000}\n` `Port: ${process.env.API_PORT || 9000}\n`
) )
}); });
} }

View file

@ -76,12 +76,12 @@ export async function runWeb(express, app, gitCommit, gitBranch, __dirname) {
return res.redirect('/') return res.redirect('/')
}); });
app.listen(process.env.webPort || 9001, () => { app.listen(process.env.WEB_PORT || 9001, () => {
console.log(`\n` + console.log(`\n` +
`${Cyan("cobalt")} WEB ${Bright(`v.${version}-${gitCommit} (${gitBranch})`)}\n` + `${Cyan("cobalt")} WEB ${Bright(`v.${version}-${gitCommit} (${gitBranch})`)}\n` +
`Start time: ${Bright(`${startTime.toUTCString()} (${startTimestamp})`)}\n\n` + `Start time: ${Bright(`${startTime.toUTCString()} (${startTimestamp})`)}\n\n` +
`URL: ${Cyan(`${process.env.webURL}`)}\n` + `URL: ${Cyan(`${process.env.WEB_URL}`)}\n` +
`Port: ${process.env.webPort || 9001}\n` `Port: ${process.env.WEB_PORT || 9001}\n`
) )
}) })
} }

View file

@ -264,5 +264,5 @@ export function sponsoredList() {
} }
export function betaTag() { export function betaTag() {
return process.env.isBeta ? '<span class="logo-sub">β</span>' : '' return process.env.IS_BETA ? '<span class="logo-sub">β</span>' : ''
} }

View file

@ -48,10 +48,10 @@ export default function(obj) {
<title>${t("AppTitleCobalt")}</title> <title>${t("AppTitleCobalt")}</title>
<meta property="og:url" content="${process.env.webURL}"> <meta property="og:url" content="${process.env.WEB_URL}">
<meta property="og:title" content="${t("AppTitleCobalt")}"> <meta property="og:title" content="${t("AppTitleCobalt")}">
<meta property="og:description" content="${t('EmbedBriefDescription')}"> <meta property="og:description" content="${t('EmbedBriefDescription')}">
<meta property="og:image" content="${process.env.webURL}icons/generic.png"> <meta property="og:image" content="${process.env.WEB_URL}icons/generic.png">
<meta name="title" content="${t("AppTitleCobalt")}"> <meta name="title" content="${t("AppTitleCobalt")}">
<meta name="description" content="${t('AboutSummary')}"> <meta name="description" content="${t('AboutSummary')}">
<meta name="theme-color" content="#000000"> <meta name="theme-color" content="#000000">
@ -165,7 +165,7 @@ export default function(obj) {
body: t("FairUse") body: t("FairUse")
}]) }])
}, },
...(process.env.showSponsors ? ...(process.env.SHOW_SPONSORS ?
[{ [{
text: t("SponsoredBy"), text: t("SponsoredBy"),
classes: ["sponsored-by-text"], classes: ["sponsored-by-text"],
@ -627,7 +627,7 @@ export default function(obj) {
</footer> </footer>
</div> </div>
<script> <script>
let defaultApiUrl = '${process.env.apiURL ? process.env.apiURL : ''}'; let defaultApiUrl = '${process.env.API_URL || ''}';
const loc = ${webLoc(t, const loc = ${webLoc(t,
[ [
'ErrorNoInternet', 'ErrorNoInternet',

View file

@ -3,7 +3,7 @@ import { readFile, writeFile } from 'fs/promises';
import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser'; import { parse as parseSetCookie, splitCookiesString } from 'set-cookie-parser';
const WRITE_INTERVAL = 60000, const WRITE_INTERVAL = 60000,
cookiePath = process.env.cookiePath, cookiePath = process.env.COOKIE_PATH,
COUNTER = Symbol('counter'); COUNTER = Symbol('counter');
let cookies = {}, dirty = false, intervalId; let cookies = {}, dirty = false, intervalId;

View file

@ -39,27 +39,27 @@ function setup() {
console.log(Bright("\nCool! What's the domain this API instance will be running on? (localhost)\nExample: co.wuk.sh")); console.log(Bright("\nCool! What's the domain this API instance will be running on? (localhost)\nExample: co.wuk.sh"));
rl.question(q, apiURL => { rl.question(q, apiURL => {
ob['apiURL'] = `http://localhost:9000/`; ob['API_URL'] = `http://localhost:9000/`;
ob['apiPort'] = 9000; ob['API_PORT'] = 9000;
if (apiURL && apiURL !== "localhost") ob['apiURL'] = `https://${apiURL.toLowerCase()}/`; if (apiURL && apiURL !== "localhost") ob['API_URL'] = `https://${apiURL.toLowerCase()}/`;
console.log(Bright("\nGreat! Now, what port will it be running on? (9000)")); console.log(Bright("\nGreat! Now, what port will it be running on? (9000)"));
rl.question(q, apiPort => { rl.question(q, apiPort => {
if (apiPort) ob['apiPort'] = apiPort; if (apiPort) ob['API_PORT'] = apiPort;
if (apiPort && (apiURL === "localhost" || !apiURL)) ob['apiURL'] = `http://localhost:${apiPort}/`; if (apiPort && (apiURL === "localhost" || !apiURL)) ob['API_URL'] = `http://localhost:${apiPort}/`;
console.log(Bright("\nWhat will your instance's name be? Usually it's something like eu-nl aka region-country. (local)")); console.log(Bright("\nWhat will your instance's name be? Usually it's something like eu-nl aka region-country. (local)"));
rl.question(q, apiName => { rl.question(q, apiName => {
ob['apiName'] = apiName.toLowerCase(); ob['API_URL'] = apiName.toLowerCase();
if (!apiName || apiName === "local") ob['apiName'] = "local"; if (!apiName || apiName === "local") ob['API_URL'] = "local";
console.log(Bright("\nOne last thing: would you like to enable CORS? It allows other websites and extensions to use your instance's API.\ny/n (n)")); console.log(Bright("\nOne last thing: would you like to enable CORS? It allows other websites and extensions to use your instance's API.\ny/n (n)"));
rl.question(q, apiCors => { rl.question(q, apiCors => {
let answCors = apiCors.toLowerCase().trim(); let answCors = apiCors.toLowerCase().trim();
if (answCors !== "y" && answCors !== "yes") ob['cors'] = '0' if (answCors !== "y" && answCors !== "yes") ob['CORS_WILDCARD'] = '0'
final() final()
}) })
}) })
@ -71,25 +71,25 @@ function setup() {
console.log(Bright("\nAwesome! What's the domain this web app instance will be running on? (localhost)\nExample: cobalt.tools")); console.log(Bright("\nAwesome! What's the domain this web app instance will be running on? (localhost)\nExample: cobalt.tools"));
rl.question(q, webURL => { rl.question(q, webURL => {
ob['webURL'] = `http://localhost:9001/`; ob['WEB_URL'] = `http://localhost:9001/`;
ob['webPort'] = 9001; ob['WEB_PORT'] = 9001;
if (webURL && webURL !== "localhost") ob['webURL'] = `https://${webURL.toLowerCase()}/`; if (webURL && webURL !== "localhost") ob['WEB_URL'] = `https://${webURL.toLowerCase()}/`;
console.log( console.log(
Bright("\nGreat! Now, what port will it be running on? (9001)") Bright("\nGreat! Now, what port will it be running on? (9001)")
) )
rl.question(q, webPort => { rl.question(q, webPort => {
if (webPort) ob['webPort'] = webPort; if (webPort) ob['WEB_PORT'] = webPort;
if (webPort && (webURL === "localhost" || !webURL)) ob['webURL'] = `http://localhost:${webPort}/`; if (webPort && (webURL === "localhost" || !webURL)) ob['WEB_URL'] = `http://localhost:${webPort}/`;
console.log( console.log(
Bright("\nOne last thing: what default API domain should be used? (co.wuk.sh)\nIf it's hosted locally, make sure to include the port:") + Cyan(" localhost:9000") Bright("\nOne last thing: what default API domain should be used? (co.wuk.sh)\nIf it's hosted locally, make sure to include the port:") + Cyan(" localhost:9000")
); );
rl.question(q, apiURL => { rl.question(q, apiURL => {
ob['apiURL'] = `https://${apiURL.toLowerCase()}/`; ob['API_URL'] = `https://${apiURL.toLowerCase()}/`;
if (apiURL.includes(':')) ob['apiURL'] = `http://${apiURL.toLowerCase()}/`; if (apiURL.includes(':')) ob['API_URL'] = `http://${apiURL.toLowerCase()}/`;
if (!apiURL) ob['apiURL'] = "https://co.wuk.sh/"; if (!apiURL) ob['API_URL'] = "https://co.wuk.sh/";
final() final()
}) })
}); });

View file

@ -43,7 +43,7 @@ export function createStream(obj) {
exp = streamInfo.exp; exp = streamInfo.exp;
ghmac = streamInfo.hmac; ghmac = streamInfo.hmac;
} }
return `${process.env.apiURL}api/stream?t=${streamID}&e=${exp}&h=${ghmac}`; return `${process.env.API_URL}api/stream?t=${streamID}&e=${exp}&h=${ghmac}`;
} }
export function verifyStream(id, hmac, exp) { export function verifyStream(id, hmac, exp) {

View file

@ -0,0 +1,23 @@
import { Red } from "./consoleText.js";
const mapping = {
apiPort: 'API_PORT',
apiURL: 'API_URL',
apiName: 'API_NAME',
cors: 'CORS_WILDCARD',
cookiePath: 'COOKIE_PATH',
webPort: 'WEB_PORT',
webUrl: 'WEB_URL',
showSponsors: 'SHOW_SPONSORS',
isBeta: 'IS_BETA'
}
for (const [ oldEnv, newEnv ] of Object.entries(mapping)) {
if (process.env[oldEnv] && !process.env[newEnv]) {
process.env[newEnv] = process.env[oldEnv];
console.error(`${Red('[!]')} ${oldEnv} is deprecated and will be removed in a future version.`);
console.error(` You should use ${newEnv} instead.`);
console.error();
delete process.env[oldEnv];
}
}

View file

@ -1,4 +1,5 @@
import "dotenv/config"; import "dotenv/config";
import "../modules/sub/alias-envs.js";
import { getJSON } from "../modules/api.js"; import { getJSON } from "../modules/api.js";
import { services } from "../modules/config.js"; import { services } from "../modules/config.js";