diff --git a/src/api/start.ts b/src/api/start.ts index 1fdc1f6ee..ced1a5f8f 100644 --- a/src/api/start.ts +++ b/src/api/start.ts @@ -40,7 +40,7 @@ if (cluster.isPrimary && process.env.NODE_ENV == "production") { cluster.fork(); }); } else { - const port = Number(process.env.PORT) || 3001; + const port = EnvConfig.get().port || 3001; const server = new SpacebarServer({ port }); server.start().catch(console.error); diff --git a/src/bundle/Server.ts b/src/bundle/Server.ts index 1424ce8c4..7a1625612 100644 --- a/src/bundle/Server.ts +++ b/src/bundle/Server.ts @@ -32,7 +32,7 @@ import { Config, EnvConfig, initDatabase } from "@spacebar/util"; const app = express(); const server = http.createServer(); -const port = Number(process.env.PORT) || 3001; +const port = EnvConfig.get().port || 3001; const wrtcWsPort = Number(process.env.WRTC_WS_PORT) || 3004; const production = process.env.NODE_ENV == "development" ? false : true; server.on("request", app); diff --git a/src/cdn/start.ts b/src/cdn/start.ts index 4455806aa..7c758716e 100644 --- a/src/cdn/start.ts +++ b/src/cdn/start.ts @@ -22,12 +22,12 @@ import { config } from "dotenv"; config({ quiet: true }); import { CDNServer } from "./Server"; -const server = new CDNServer({ port: Number(process.env.PORT) || 3003 }); +const server = new CDNServer({ port: EnvConfig.get().port || 3003 }); server - .start() - .then(() => { - console.log("[Server] started on :" + server.options.port); - }) - .catch((e) => console.error("[Server] Error starting: ", e)); + .start() + .then(() => { + console.log("[Server] started on :" + server.options.port); + }) + .catch((e) => console.error("[Server] Error starting: ", e)); module.exports = server; diff --git a/src/gateway/start.ts b/src/gateway/start.ts index 10dfac92d..b2b5b420c 100644 --- a/src/gateway/start.ts +++ b/src/gateway/start.ts @@ -23,12 +23,12 @@ process.on("unhandledRejection", console.error); import { Server } from "./Server"; import { config } from "dotenv"; +import { EnvConfig } from "@spacebar/util*"; config({ quiet: true }); -let port = Number(process.env.PORT); -if (isNaN(port)) port = 3002; +const port = EnvConfig.get().port || 3002; const server = new Server({ - port, + port, }); server.start(); diff --git a/src/util/config/EnvConfig.ts b/src/util/config/EnvConfig.ts index 800bd3322..2816461e1 100644 --- a/src/util/config/EnvConfig.ts +++ b/src/util/config/EnvConfig.ts @@ -52,6 +52,10 @@ export class EnvConfig { return 1; } } + + get port(): number | undefined { + return Number(process.env.PORT); + } } // Deprecations: diff --git a/src/webrtc/start.ts b/src/webrtc/start.ts index 5ad9761a3..72b383ba7 100644 --- a/src/webrtc/start.ts +++ b/src/webrtc/start.ts @@ -22,11 +22,12 @@ process.on("unhandledRejection", console.error); import { config } from "dotenv"; import { Server } from "./Server"; +import { EnvConfig } from "@spacebar/util"; config({ quiet: true }); -const port = Number(process.env.PORT) || 3004; +const port = EnvConfig.get().port || 3004; const server = new Server({ - port, + port, }); server.start();