From 5150990d6f9839c63959137e3cb906db40704620 Mon Sep 17 00:00:00 2001 From: batche Date: Tue, 3 Jan 2023 13:47:37 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Changement=20de=20TOUTES=20les=20occurrence?= =?UTF-8?q?s=20d'un=20host=20ou=20d'un=20port=20=C3=A9crits=20en=20dur=20d?= =?UTF-8?q?ans=20le=20projet.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- make_env.sh | 102 ++++++++++++++---- srcs/docker-compose.yml | 2 + .../src/auth/42/authentication.controller.ts | 6 +- srcs/requirements/nginx/conf/default.conf | 12 +-- .../api_front/src/pages/game/client/audio.ts | 11 +- .../api_front/src/pages/game/client/ws.ts | 2 +- .../api_front/src/routes/primaryRoutes.js | 4 +- 7 files changed, 98 insertions(+), 41 deletions(-) mode change 100644 => 100755 make_env.sh diff --git a/make_env.sh b/make_env.sh old mode 100644 new mode 100755 index 7e424f3b..3cb5e9e9 --- a/make_env.sh +++ b/make_env.sh @@ -2,6 +2,11 @@ ENV_FILE_DOCKER=./srcs/.env ENV_FILE_SVELTE=./srcs/requirements/svelte/api_front/.env +NGINX_CONF_FILE=./srcs/requirements/nginx/conf/default.conf +BOLD_RED="\033[1;31m" +BOLD_GREEN="\033[1;32m" +BOLD_BLUE="\033[1;34m" +RESET="\033[0m" # Function to generate passwords # @@ -13,6 +18,20 @@ ENV_FILE_SVELTE=./srcs/requirements/svelte/api_front/.env echo $(openssl rand -base64 32 | tr "/" "_" ); } + function make_env_for_svelte + { + echo -e "${BOLD_BLUE}Creating a new environment for svelte${RESET}" + grep "^WEBSITE_" "$ENV_FILE_DOCKER" > "$ENV_FILE_SVELTE" + } + + function update_nginx_conf + { + echo -e "${BOLD_BLUE}Updating the nginx conf${RESET}" + echo -e "${BOLD_RED}WARNING : If for some reason you've changed the port, you MUST change it inside the nginx conf file. It's not supposed to change.${RESET}" + HOST=$(grep "^WEBSITE_HOST" $ENV_FILE_DOCKER | cut -d "=" -f 2) + sed -i "s/server_name.*/server_name $HOST;/g" "$NGINX_CONF_FILE" + } + function make_env_for_docker_and_svelte { echo "Creating a new environment for docker" @@ -44,35 +63,78 @@ ENV_FILE_SVELTE=./srcs/requirements/svelte/api_front/.env echo "FORTYTWO_CLIENT_ID=$CLIENT_ID" >> "$ENV_FILE_DOCKER" read -p "Enter the client secret of the 42 api : " CLIENT_SECRET echo "FORTYTWO_CLIENT_SECRET=$CLIENT_SECRET" >> "$ENV_FILE_DOCKER" - FT_CALLBACK="http://\$WEBSITE_HOST:\$WEBSITE_PORT/api/v2/auth/redirect" + FT_CALLBACK="http://$PROJECT_HOST:8080/api/v2/auth/redirect" echo "FORTYTWO_CALLBACK_URL=$FT_CALLBACK" >> "$ENV_FILE_DOCKER" # Other configs echo "COOKIE_SECRET=$(generate_password)" >> "$ENV_FILE_DOCKER" echo "PORT=3000" >> "$ENV_FILE_DOCKER" echo "TWO_FACTOR_AUTHENTICATION_APP_NAME=Transcendance" >> "$ENV_FILE_DOCKER" echo "TICKET_FOR_PLAYING_GAME_SECRET=$(generate_password)" >> "$ENV_FILE_DOCKER" - grep "WEBSITE_" > "$ENV_FILE_SVELTE" + make_env_for_svelte + update_nginx_conf + echo -e "${BOLD_GREEN}Environment created.${RESET}" + echo -e "${BOLD_GREEN}The script will exit${RESET}" + exit 0 } + function change_host_and_port_api + { + if [ -f "$ENV_FILE_DOCKER" ] + then + echo -e "${BOLD_BLUE}Changing the host and the port of the api${RESET}" + read -p "Enter the name of the host like \"localhost\" : " PROJECT_HOST + sed -i "s/WEBSITE_HOST=.*/WEBSITE_HOST=$PROJECT_HOST/g" "$ENV_FILE_DOCKER" + read -p "Enter the port of the api : " PROJECT_PORT + sed -i "s/WEBSITE_PORT=.*/WEBSITE_PORT=$PROJECT_PORT/g" "$ENV_FILE_DOCKER" + make_env_for_svelte + update_nginx_conf + else + echo -e "${BOLD_RED}No environment file found. We will regenerate the entire env files.${RESET}" + fi + } + + function change_api_fields_for_42_auth_api + { + if [ -f "$ENV_FILE_DOCKER" ] + then + echo -e "${BOLD_BLUE}Changing the secret for the 42 api${RESET}" + read -p "Enter the client id of the 42 api : " CLIENT_ID + sed -i "s/FORTYTWO_CLIENT_ID=.*/FORTYTWO_CLIENT_ID=$CLIENT_ID/g" "$ENV_FILE_DOCKER" + read -p "Enter the client secret of the 42 api : " CLIENT_SECRET + sed -i "s/FORTYTWO_CLIENT_SECRET=.*/FORTYTWO_CLIENT_SECRET=$CLIENT_SECRET/g" "$ENV_FILE_DOCKER" + echo -e "${BOLD_GREEN}The fields concerning the 42 api have been changed.${RESET}" + else + echo -e "${BOLD_RED}No environment file found. We will regenerate the entire env files.${RESET}" + make_env_for_docker_and_svelte + fi + } + + function choose_options_and_process { - if [ -f "$ENV_FILE_DOCKER" ]; then - echo "The file $ENV_FILE_DOCKER already exists. Do you want to overwrite it ? (y/n)" - OVERWRITE="" - # Ask to overwrite the .env files - while [ "$OVERWRITE" != "y" ] && [ "$OVERWRITE" != "n" ]; do - read -p "Enter your choice : " OVERWRITE - done - if [ "$OVERWRITE" = "y" ]; then - rm "$ENV_FILE_DOCKER" && rm "$ENV_FILE_SVELTE" - docker rmi -f postgres - make_env_for_docker_and_svelte - else - if [ ! -f "$ENV_FILE_SVELTE" ]; then - grep "WEBSITE_" > "$ENV_FILE_SVELTE" - fi - echo "The file $ENV_FILE_DOCKER will not be overwritten. The script will exit." - exit 0 - fi + if [ ! -f "$ENV_FILE_DOCKER" ]; then + make_env_for_docker_and_svelte + elif [ ! -f "$ENV_FILE_SVELTE" && -f "$ENV_FILE_DOCKER" ]; then + make_env_for_svelte + fi + echo -e "${BOLD_RED}An environment already exists.${RESET}" + echo -e "${BOLD_GREEN}What do you want to do ?${RESET}" + echo -e "${BOLD_GREEN}1. Regenerate entire environment${RESET}" + echo -e "${BOLD_GREEN}2. Only change the fields about the HOSTNAME and the PORT of the API${RESET}" + echo -e "${BOLD_GREEN}3. Only change the fields concerning the 42 API (id and secret)${RESET}" + echo -e "${BOLD_GREEN}4. Exit${RESET}" + CHOICE="" + while [ "$CHOICE" != "1" ] && [ "$CHOICE" != "2" ] && [ "$CHOICE" != "3" ] && [ "$CHOICE" != "4" ]; do + read -p "Enter your choice : " CHOICE + done + if [ "$CHOICE" = "1" ]; then + make_env_for_docker_and_svelte + elif [ "$CHOICE" = "2" ]; then + change_host_and_port_api + elif [ "$CHOICE" = "3" ]; then + change_api_fields_for_42_auth_api + else + echo -e "${BOLD_GREEN}The script will exit.${RESET}" + exit 0 fi } # Create a new environment for docker diff --git a/srcs/docker-compose.yml b/srcs/docker-compose.yml index 9c641941..0c27791f 100644 --- a/srcs/docker-compose.yml +++ b/srcs/docker-compose.yml @@ -38,6 +38,8 @@ services: dockerfile: Dockerfile environment: NODE_ENV: "${NODE_ENV}" + WEBSITE_HOST: "${WEBSITE_HOST}" + WEBSITE_PORT: "${WEBSITE_PORT}" restart: unless-stopped ports: - "8042:8042" diff --git a/srcs/requirements/nestjs/api_back/src/auth/42/authentication.controller.ts b/srcs/requirements/nestjs/api_back/src/auth/42/authentication.controller.ts index 6746e911..44de4932 100644 --- a/srcs/requirements/nestjs/api_back/src/auth/42/authentication.controller.ts +++ b/srcs/requirements/nestjs/api_back/src/auth/42/authentication.controller.ts @@ -37,10 +37,10 @@ export class AuthenticationController { const user : User = request.user if (user.isEnabledTwoFactorAuth === false || user.isTwoFactorAuthenticated === true){ console.log('ON VA VERS PROFILE'); - return response.status(200).redirect('http://transcendance:8080/#/profile'); + return response.status(200).redirect('http://' + process.env.WEBSITE_HOST + ':' + process.env.WEBSITE_PORT + '/#/profile'); } console.log('ON VA VERS 2FA') - return response.status(200).redirect('http://transcendance:8080/#/2fa'); + return response.status(200).redirect('http://' + process.env.WEBSITE_HOST + ':' + process.env.WEBSITE_PORT + '/#/2fa'); } /** @@ -83,6 +83,6 @@ export class AuthenticationController { throw new UnauthorizedException('Wrong Code.'); await this.userService.authenticateUserWith2FA(request.user.id); console.log('ON REDIRIGE'); - return response.status(200).redirect('http://transcendance:8080/#/profile'); + return response.status(200).redirect('http://' + process.env.WEBSITE_HOST + ':' + process.env.WEBSITE_PORT + '/#/profile'); } } diff --git a/srcs/requirements/nginx/conf/default.conf b/srcs/requirements/nginx/conf/default.conf index 92c95f34..02ffec11 100644 --- a/srcs/requirements/nginx/conf/default.conf +++ b/srcs/requirements/nginx/conf/default.conf @@ -1,8 +1,7 @@ server { - - listen 8080 default_server; - listen [::]:8080 default_server; - server_name transcendance; + listen 8080; + listen [::]:8080; + server_name localhost; location /api/v2 { proxy_set_header Host $host; @@ -17,7 +16,7 @@ server { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "upgrade"; + proxy_set_header Connection "Upgrade"; proxy_pass http://backend_dev:5000/chat; } @@ -43,10 +42,9 @@ server { } server { - listen 35729 default_server; listen [::]:35729 default_server; - server_name transcendance; + server_name localhost; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; diff --git a/srcs/requirements/svelte/api_front/src/pages/game/client/audio.ts b/srcs/requirements/svelte/api_front/src/pages/game/client/audio.ts index eedc08a6..a3754446 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/client/audio.ts +++ b/srcs/requirements/svelte/api_front/src/pages/game/client/audio.ts @@ -3,10 +3,10 @@ import * as c from "./constants.js" // export const soundPongArr: HTMLAudioElement[] = []; export const soundPongArr: HTMLAudioElement[] = [ - new Audio("http://transcendance:8080/sound/pong/"+1+".ogg"), - new Audio("http://transcendance:8080/sound/pong/"+2+".ogg") + new Audio("http://" + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + "/sound/pong/"+1+".ogg"), + new Audio("http://" + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + "/sound/pong/"+2+".ogg") ]; -export const soundRoblox = new Audio("http://transcendance:8080/sound/roblox-oof.ogg"); +export const soundRoblox = new Audio("http://" + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + "/sound/roblox-oof.ogg"); export function initAudio(sound: string) { @@ -18,11 +18,6 @@ export function initAudio(sound: string) muteFlag = true; } -/* for (let i = 0; i <= 32; i++) { - soundPongArr.push(new Audio("http://transcendance:8080/sound/pong/"+i+".ogg")); - soundPongArr[i].volume = c.soundPongVolume; - soundPongArr[i].muted = muteFlag; - } */ soundPongArr.forEach((value) => { value.volume = c.soundRobloxVolume; value.muted = muteFlag; diff --git a/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts b/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts index fdd867f4..95222dc7 100644 --- a/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts +++ b/srcs/requirements/svelte/api_front/src/pages/game/client/ws.ts @@ -35,7 +35,7 @@ class ClientInfoSpectator { playerRightNextPos: VectorInteger; } -const wsUrl = "ws://transcendance:8080/pong"; +const wsUrl = "ws://" + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + "/pong"; export let socket: WebSocket; /* TODO: A way to still use "const" not "let" ? */ export const clientInfo = new ClientInfo(); export const clientInfoSpectator = new ClientInfoSpectator(); // WIP, could refactor this diff --git a/srcs/requirements/svelte/api_front/src/routes/primaryRoutes.js b/srcs/requirements/svelte/api_front/src/routes/primaryRoutes.js index 99e3dcd2..6a8f01c0 100644 --- a/srcs/requirements/svelte/api_front/src/routes/primaryRoutes.js +++ b/srcs/requirements/svelte/api_front/src/routes/primaryRoutes.js @@ -21,7 +21,7 @@ export const primaryRoutes = { component: ProfilePage, conditions: [ async(detail) => { - const user = await fetch('http://transcendance:8080/api/v2/user') + const user = await fetch('http://' + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + '/api/v2/user') .then((resp) => resp.json()) console.log('in /profile what is in user') @@ -38,7 +38,7 @@ export const primaryRoutes = { component: ProfilePage, conditions: [ async(detail) => { - const user = await fetch('http://transcendance:8080/api/v2/user') + const user = await fetch('http://' + process.env.WEBSITE_HOST + ":" + process.env.WEBSITE_PORT + '/api/v2/user') .then((resp) => resp.json()) console.log('in /profile/* what is in user') From 3844c4df53c3d59393872bef3abaaaaa1554b452 Mon Sep 17 00:00:00 2001 From: batche Date: Tue, 3 Jan 2023 15:18:36 +0100 Subject: [PATCH 2/3] yolo --- make_env.sh | 6 ++++-- srcs/docker-compose.yml | 1 + srcs/requirements/nestjs/api_back/src/main.ts | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/make_env.sh b/make_env.sh index 3cb5e9e9..d6d5b6b9 100755 --- a/make_env.sh +++ b/make_env.sh @@ -34,7 +34,9 @@ RESET="\033[0m" function make_env_for_docker_and_svelte { - echo "Creating a new environment for docker" + docker rm -f postgresql + docker volume rm -f srcs_data_nest_postgresql + echo -e "${BOLD_BLUE}Creating a new environment for docker${RESET}" NODE_ENV="" # Ask if dev or prod environment while [ "$NODE_ENV" != "1" ] && [ "$NODE_ENV" != "2" ]; do @@ -58,7 +60,7 @@ RESET="\033[0m" echo "REDIS_PORT=6379" >> "$ENV_FILE_DOCKER" echo "REDIS_PASSWORD=$(generate_password)" >> "$ENV_FILE_DOCKER" # Connection to 42 - echo "In the next steps, we'll need to enter the client secret and client id of the 42 api" + echo -e "${BOLD_BLUE}In the next steps, we'll need to enter the client secret and client id of the 42 api${RESET}" read -p "Enter the client id of the 42 api : " CLIENT_ID echo "FORTYTWO_CLIENT_ID=$CLIENT_ID" >> "$ENV_FILE_DOCKER" read -p "Enter the client secret of the 42 api : " CLIENT_SECRET diff --git a/srcs/docker-compose.yml b/srcs/docker-compose.yml index 0c27791f..3d6fa9c2 100644 --- a/srcs/docker-compose.yml +++ b/srcs/docker-compose.yml @@ -22,6 +22,7 @@ services: COOKIE_SECRET: "${COOKIE_SECRET}" TWO_FACTOR_AUTHENTICATION_APP_NAME : "${TWO_FACTOR_AUTHENTICATION_APP_NAME}" TICKET_FOR_PLAYING_GAME_SECRET : "${TICKET_FOR_PLAYING_GAME_SECRET}" + PORT: "${PORT}" volumes: - ./requirements/nestjs/api_back/src:/usr/app/src - ./requirements/nestjs/api_back/test:/usr/app/test/ diff --git a/srcs/requirements/nestjs/api_back/src/main.ts b/srcs/requirements/nestjs/api_back/src/main.ts index 78cb852c..d638aabf 100644 --- a/srcs/requirements/nestjs/api_back/src/main.ts +++ b/srcs/requirements/nestjs/api_back/src/main.ts @@ -8,7 +8,7 @@ import * as connectRedis from 'connect-redis'; async function bootstrap() { const app = await NestFactory.create(AppModule, { cors: true }); - const port = process.env.PORT || 3001; + const port = process.env.PORT || 3000; const client = redis.createClient( { socket: { host: process.env.REDIS_HOST, port: parseInt(process.env.REDIS_PORT) }, From 9bfb1a6ca3bb902d8177eef73f6196dc05fed33f Mon Sep 17 00:00:00 2001 From: batche Date: Tue, 3 Jan 2023 15:20:46 +0100 Subject: [PATCH 3/3] yolo bis --- srcs/requirements/nestjs/api_back/src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/srcs/requirements/nestjs/api_back/src/main.ts b/srcs/requirements/nestjs/api_back/src/main.ts index d638aabf..e9ea8426 100644 --- a/srcs/requirements/nestjs/api_back/src/main.ts +++ b/srcs/requirements/nestjs/api_back/src/main.ts @@ -50,6 +50,6 @@ async function bootstrap() { ); app.use(passport.initialize()); app.use(passport.session()); - await app.listen(port, () => { console.log(`Listening on port ${port}`); }); + await app.listen(port, () => { console.log(`Listening on port ${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}}`); }); } bootstrap();