From f70aa52dd019ee72c7d7c327851fe86106ab1637 Mon Sep 17 00:00:00 2001 From: Quentin Roussel Date: Wed, 27 Mar 2024 21:49:10 +0100 Subject: [PATCH] added env variables to store hsot and ports --- traque-back/index.js | 13 ++++++++++--- traque-back/package-lock.json | 12 ++++++++++++ traque-back/package.json | 1 + traque-front/components/team/actionDrawer.jsx | 2 +- traque-front/context/socketContext.jsx | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/traque-back/index.js b/traque-back/index.js index 57622be..aec86fa 100644 --- a/traque-back/index.js +++ b/traque-back/index.js @@ -1,11 +1,16 @@ import { createServer } from "http"; import { Server } from "socket.io"; import Game from "./game.js"; +import { config } from "dotenv"; +//extract admin password from .env file +config(); +const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD; +const HOST = process.env.HOST; +const PORT = process.env.PORT; const httpServer = createServer(); //Password that socket clients will have to send to be able to send admin commands -//TODO: put this in an environment variable -const ADMIN_PASSWORD = "admin"; + //set cors to allow all origins const io = new Server(httpServer, { @@ -191,4 +196,6 @@ io.of("player").on("connection", (socket) => { }); }); -httpServer.listen(3000); \ No newline at end of file +httpServer.listen(PORT, HOST, () => { + console.log(`Server running`); +}); \ No newline at end of file diff --git a/traque-back/package-lock.json b/traque-back/package-lock.json index a1b9260..1fd6b0b 100644 --- a/traque-back/package-lock.json +++ b/traque-back/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "dotenv": "^16.4.5", "socket.io": "^4.7.5" } }, @@ -94,6 +95,17 @@ } } }, + "node_modules/dotenv": { + "version": "16.4.5", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz", + "integrity": "sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://dotenvx.com" + } + }, "node_modules/engine.io": { "version": "6.5.4", "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.5.4.tgz", diff --git a/traque-back/package.json b/traque-back/package.json index 961ebbe..a36a1a9 100644 --- a/traque-back/package.json +++ b/traque-back/package.json @@ -11,6 +11,7 @@ "license": "ISC", "type": "module", "dependencies": { + "dotenv": "^16.4.5", "socket.io": "^4.7.5" } } diff --git a/traque-front/components/team/actionDrawer.jsx b/traque-front/components/team/actionDrawer.jsx index 14ba843..f0e01fb 100644 --- a/traque-front/components/team/actionDrawer.jsx +++ b/traque-front/components/team/actionDrawer.jsx @@ -8,7 +8,7 @@ export default function ActionDrawer() { const { sendCurrentPosition } = useGame(); return ( -
+
setVisible(!visible)} /> {visible &&
diff --git a/traque-front/context/socketContext.jsx b/traque-front/context/socketContext.jsx index 158500c..f61e488 100644 --- a/traque-front/context/socketContext.jsx +++ b/traque-front/context/socketContext.jsx @@ -3,7 +3,7 @@ import { createContext, useContext, useMemo } from "react"; const { io } = require("socket.io-client"); -const SOCKET_URL = "http://localhost:3000"; +const SOCKET_URL = 'http://' + process.env.NEXT_PUBLIC_SOCKET_HOST + ':' + process.env.NEXT_PUBLIC_SOCKET_PORT; const USER_SOCKET_URL = SOCKET_URL + "/player"; const ADMIN_SOCKET_URL = SOCKET_URL + "/admin";