Sfoglia il codice sorgente

feat: set the PORT with an environment variable

Closes #213
Jonas Gloning 2 anni fa
parent
commit
68a3398f54
2 ha cambiato i file con 8 aggiunte e 3 eliminazioni
  1. 2 2
      Dockerfile
  2. 6 1
      bin/peerjs.ts

+ 2 - 2
Dockerfile

@@ -13,6 +13,6 @@ WORKDIR /peer-server
 COPY package.json package-lock.json ./
 RUN npm clean-install --omit=dev
 COPY --from=build /peer-server/dist/bin/peerjs.js ./
-EXPOSE 9000
+ENV PORT 9000
+EXPOSE ${PORT}
 ENTRYPOINT ["node", "peerjs.js"]
-CMD [ "--port", "9000" ]

+ 6 - 1
bin/peerjs.ts

@@ -11,6 +11,8 @@ import type { AddressInfo } from "node:net";
 
 const y = yargs(hideBin(process.argv));
 
+const portEnvIsSet = !!process.env["PORT"]
+
 const opts =  y
   .usage("Usage: $0")
   .wrap(Math.min(optimistUsageLength, y.terminalWidth()))
@@ -56,7 +58,7 @@ const opts =  y
     },
     port: {
       type: "number",
-      demandOption: true,
+      demandOption: !portEnvIsSet,
       alias: "p",
       describe: "port",
     },
@@ -80,6 +82,9 @@ const opts =  y
   })
   .boolean("allow_discovery").parseSync();
 
+if(!opts.port){
+    opts.port= parseInt(process.env["PORT"] as string)
+}
 process.on("uncaughtException", function (e) {
   console.error("Error: " + e);
 });