From f488fa971a2e360959fa28aa7236a16b6efdba37 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Mon, 1 Apr 2024 11:12:00 +0300 Subject: [PATCH] fix and simplify baseurl --- .env | 8 +++----- .env.demo | 9 --------- .env.development | 8 ++++---- .env.production | 8 ++++---- .env.test | 8 ++++---- next.config.js | 2 +- server.js | 14 +++++++------- src/helpers/common.js | 34 +--------------------------------- 8 files changed, 24 insertions(+), 67 deletions(-) delete mode 100644 .env.demo diff --git a/.env b/.env index 827cefb..eec3b83 100644 --- a/.env +++ b/.env @@ -1,9 +1,7 @@ - #NODE_TLS_REJECT_UNAUTHORIZED='0' -# SSL_ENABLED=false -# NEXT_PUBLIC_HOST=localhost -# NEXT_PUBLIC_PORT=3003 -# PUBLIC_URL=http://localhost:3003 +# HOST=localhost +# PORT=3003 +# NEXT_PUBLIC_PUBLIC_URL=http://localhost:3003 # Linux: `openssl rand -hex 32` or go to https://generate-secret.now.sh/32 NEXTAUTH_SECRET=ed8a9681efc414df89dfd03cd188ed58 diff --git a/.env.demo b/.env.demo deleted file mode 100644 index 02a1a51..0000000 --- a/.env.demo +++ /dev/null @@ -1,9 +0,0 @@ -SSL_ENABLED=false -NEXT_PUBLIC_PORT= -NEXT_PUBLIC_HOST=staging.mwhitnessing.com -PUBLIC_URL=https://staging.mwhitnessing.com - -# Linux: `openssl rand -hex 32` or go to https://generate-secret.now.sh/32 -NEXTAUTH_SECRET=1dd8a5457970d1dda50600be28e935ecc4513ff27c49c431849e6746f158d638 -# ? do we need to duplicate this? already defined in the deoployment yml file -DATABASE_URL=mysql://jwpwsofia_demo:dwxhns9p9vp248@mariadb:3306/jwpwsofia_demo \ No newline at end of file diff --git a/.env.development b/.env.development index 8029d98..21fcc42 100644 --- a/.env.development +++ b/.env.development @@ -1,9 +1,9 @@ NODE_TLS_REJECT_UNAUTHORIZED=0 # NODE_EXTRA_CA_CERTS=C:\\Users\\popov\\AppData\\Local\\mkcert -NEXT_PUBLIC_PROTOCOL=https -NEXT_PUBLIC_PORT=3003 -NEXT_PUBLIC_HOST=localhost -PUBLIC_URL=https://localhost:3003 +PROTOCOL=https +PORT=3003 +HOST=localhost +NEXT_PUBLIC_PUBLIC_URL=https://localhost:3003 SSL_KEY=./certificates/localhost-key.pem SSL_CERT=./certificates/localhost.pem diff --git a/.env.production b/.env.production index 3f4db06..8f9c4eb 100644 --- a/.env.production +++ b/.env.production @@ -1,7 +1,7 @@ -NEXT_PUBLIC_PORT= -NEXT_PUBLIC_HOST=sofia.mwhitnessing.com -SSL_ENABLED=false # we're behind a reverse proxy. SSL is handled by the proxy -PUBLIC_URL= https://sofia.mwhitnessing.com +PORT= +HOST=sofia.mwhitnessing.com +PROTOCOL=http # we're behind a reverse proxy. SSL is handled by the proxy +NEXT_PUBLIC_PUBLIC_URL= https://sofia.mwhitnessing.com # Linux: `openssl rand -hex 32` or go to https://generate-secret.now.sh/32 NEXTAUTH_SECRET=1dd8a5457970d1dda50600be28e935ecc4513ff27c49c431849e6746f158d638 diff --git a/.env.test b/.env.test index 78ba247..0fbca9e 100644 --- a/.env.test +++ b/.env.test @@ -1,7 +1,7 @@ -NEXT_PUBLIC_PROTOCOL=http -NEXT_PUBLIC_PORT= -NEXT_PUBLIC_HOST=staging.mwhitnessing.com -PUBLIC_URL=https://staging.mwhitnessing.com +PROTOCOL=http +HOST=staging.mwhitnessing.com +PORT= +NEXT_PUBLIC_PUBLIC_URL=https://staging.mwhitnessing.com # Linux: `openssl rand -hex 32` or go to https://generate-secret.now.sh/32 NEXTAUTH_SECRET=1dd8a5457970d1dda50600be28e935ecc4513ff27c49c431849e6746f158d638 diff --git a/next.config.js b/next.config.js index 865380e..32d4f12 100644 --- a/next.config.js +++ b/next.config.js @@ -12,7 +12,7 @@ module.exports = { pageExtensions: ['ts', 'tsx', 'md', 'mdx'], // Replace `jsx?` with `tsx?` env: { env: process.env.NODE_ENV, - server: process.env.NEXT_PUBLIC_PROTOCOL + '://' + process.env.NEXT_PUBLIC_HOST + ':' + process.env.NEXT_PUBLIC_PORT + '', + server: process.env.NEXT_PUBLIC_PUBLIC_URL }, webpack(config, { isServer }) { diff --git a/server.js b/server.js index 21637f2..c48323b 100644 --- a/server.js +++ b/server.js @@ -35,16 +35,16 @@ require('dotenv').config({ console.log("process.env.NODE_ENV = ", process.env.NODE_ENV); -const PROTOCOL = process.env.NEXT_PUBLIC_PROTOCOL; -const PORT = process.env.NEXT_PUBLIC_PORT || 3000; -const HOST = process.env.NEXT_PUBLIC_HOST; +const PROTOCOL = process.env.PROTOCOL; +const PORT = process.env.PORT || 3000; +const HOST = process.env.HOST; const dev = process.env.NODE_ENV !== "production"; const nextApp = next({ dev }); const nextHandler = nextApp.getRequestHandler(); -console.log("process.env.NEXT_PUBLIC_PROTOCOL = ", process.env.NEXT_PUBLIC_PROTOCOL); -console.log("process.env.PUBLIC_URL = ", process.env.PUBLIC_URL); -console.log("process.env.NEXT_PUBLIC_PORT = ", process.env.NEXT_PUBLIC_PORT); +console.log("process.env.PROTOCOL = ", process.env.PROTOCOL); +console.log("process.env.PUBLIC_URL = ", process.env.NEXT_PUBLIC_PUBLIC_URL); +console.log("process.env.PORT = ", process.env.PORT); console.log("process.env.TELEGRAM_BOT = ", process.env.TELEGRAM_BOT); //require('module-alias/register'); @@ -66,7 +66,7 @@ const prisma = common.getPrismaClient(); const server = express(); //check if ssl is enabled -if (process.env.NEXT_PUBLIC_PROTOCOL === 'https') { +if (process.env.PROTOCOL === 'https') { console.log("SSL_ENABLED = true"); // Redirect from http to https // server.use((req, res, next) => { diff --git a/src/helpers/common.js b/src/helpers/common.js index 288f906..e580a4b 100644 --- a/src/helpers/common.js +++ b/src/helpers/common.js @@ -76,39 +76,7 @@ exports.setBaseUrl = function (req) { }; exports.getBaseUrl = function (relative = "", req = null) { - if (typeof window === 'undefined') { - // Server-side logic - // Read the base URL from env (PUBLIC_URL): - return process.env.PUBLIC_URL + relative; - - // const filePath = path.join(__dirname, 'baseUrl.txt'); - - // try { - // if (fs.existsSync(filePath)) { - // const baseUrl = fs.readFileSync(filePath, 'utf8').trim(); - // const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl; - // return fullUrl; - // } else { - // if (req) { - // // Assuming setBaseUrl is defined somewhere in this file - // const baseUrl = exports.setBaseUrl(req); - // return `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}`; - // } - // console.log('Base URL file does not exist.'); - // return null; - // } - // } catch (error) { - // console.error('Error reading the base URL file:', error); - // return null; - // } - } else { - // Client-side logic - // Fetch the base URL from the server endpoint you've set up - const baseUrl = window.location.origin; - const fullUrl = relative ? `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}` : baseUrl; - //console.log("getBaseUrl()=", fullUrl); - return fullUrl.toString(); - } + return process.env.NEXT_PUBLIC_PUBLIC_URL + relative; };