From 4e2c1e26baa9b8a45c9bbd94bd208ea995124dbf Mon Sep 17 00:00:00 2001
From: Amir <amirhossein.d.z@gmail.com>
Date: Wed, 25 Dec 2024 14:09:06 +0100
Subject: [PATCH] refactor: update environment variables for file URLs to use
 protocol, host, and port

---
 .env                           | 11 +++++++----
 compose.yaml                   |  5 +++--
 frontend/.env.local.example    |  5 +++--
 frontend/next.config.mjs       |  5 +++--
 frontend/src/constants/envs.ts |  4 +---
 5 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/.env b/.env
index 6e509032..f2b86087 100644
--- a/.env
+++ b/.env
@@ -28,9 +28,12 @@ NATS_URL=nats:4222
 
 ## frontend
 INTERNAL_BACKEND_BASE_URL=https://backend.tarhche.com
-NEXT_PUBLIC_FILES_BASE_URL=https://backend.tarhche.com
-NEXT_PUBLIC_EXTERNAL_BACKEND_BASE_URL=https://tarhche.com
+NEXT_PUBLIC_FILES_PROTOCOL=https
+NEXT_PUBLIC_FILES_HOST=backend.tarhche.com
+NEXT_PUBLIC_FILES_PORT=80
 
+## frontend local
 # INTERNAL_BACKEND_BASE_URL=http://app
-# NEXT_PUBLIC_FILES_BASE_URL=http://127.0.0.1:8000
-# NEXT_PUBLIC_EXTERNAL_BACKEND_BASE_URL=http://127.0.0.1:3000
+# NEXT_PUBLIC_FILES_PROTOCOL=http
+# NEXT_PUBLIC_FILES_HOST=127.0.0.1
+# NEXT_PUBLIC_FILES_PORT=8000
diff --git a/compose.yaml b/compose.yaml
index be320dd4..62d60e81 100644
--- a/compose.yaml
+++ b/compose.yaml
@@ -43,9 +43,10 @@ services:
     volumes:
       - ./frontend:/opt/app
     environment:
-      NEXT_PUBLIC_EXTERNAL_BACKEND_BASE_URL: ${NEXT_PUBLIC_EXTERNAL_BACKEND_BASE_URL}
       INTERNAL_BACKEND_BASE_URL: ${INTERNAL_BACKEND_BASE_URL}
-      NEXT_PUBLIC_FILES_BASE_URL: ${NEXT_PUBLIC_FILES_BASE_URL}
+      NEXT_PUBLIC_FILES_PROTOCOL: ${NEXT_PUBLIC_FILES_PROTOCOL}
+      NEXT_PUBLIC_FILES_HOST: ${NEXT_PUBLIC_FILES_HOST}
+      NEXT_PUBLIC_FILES_PORT: ${NEXT_PUBLIC_FILES_PORT}
     command: >
       sh -c "npm install; npm run dev -- --hostname \"0.0.0.0\" --port 3000"
 
diff --git a/frontend/.env.local.example b/frontend/.env.local.example
index 2ccb6b69..9769d98d 100644
--- a/frontend/.env.local.example
+++ b/frontend/.env.local.example
@@ -1,3 +1,4 @@
 INTERNAL_BACKEND_BASE_URL=https://backend.tarhche.com
-NEXT_PUBLIC_FILES_BASE_URL=https://backend.tarhche.com
-NEXT_PUBLIC_EXTERNAL_BACKEND_BASE_URL=https://tarhche.com
+NEXT_PUBLIC_FILES_PROTOCOL=https
+NEXT_PUBLIC_FILES_HOST=backend.tarhche.com
+NEXT_PUBLIC_FILES_PORT=80
diff --git a/frontend/next.config.mjs b/frontend/next.config.mjs
index 00aa818b..98c889e3 100644
--- a/frontend/next.config.mjs
+++ b/frontend/next.config.mjs
@@ -6,8 +6,9 @@ const nextConfig = {
   images: {
     remotePatterns: [
       {
-        protocol: "https",
-        hostname: "backend.tarhche.com",
+        protocol: process.env.NEXT_PUBLIC_FILES_PROTOCOL,
+        hostname: process.env.NEXT_PUBLIC_FILES_HOST,
+        port: process.env.NEXT_PUBLIC_FILES_PORT,
       },
     ],
   },
diff --git a/frontend/src/constants/envs.ts b/frontend/src/constants/envs.ts
index 4e9a130d..8def7fe8 100644
--- a/frontend/src/constants/envs.ts
+++ b/frontend/src/constants/envs.ts
@@ -1,4 +1,2 @@
 export const INTERNAL_BACKEND_URL = process.env.INTERNAL_BACKEND_BASE_URL;
-export const EXTERNAL_BACKEND_URL =
-  process.env.NEXT_PUBLIC_EXTERNAL_BACKEND_BASE_URL;
-export const FILES_PUBLIC_URL = `https://backend.tarhche.com/files`;
+export const FILES_PUBLIC_URL = `${process.env.NEXT_PUBLIC_FILES_PROTOCOL}://${process.env.NEXT_PUBLIC_FILES_HOST}:${process.env.NEXT_PUBLIC_FILES_PORT}/files`;