From f4e3d9cc2e9d37890f77c28b83cbb29fb3b45522 Mon Sep 17 00:00:00 2001 From: Dobromir Popov Date: Sun, 25 Feb 2024 13:51:19 +0200 Subject: [PATCH] set base URL dynamiically --- server.js | 13 ++++++++++++- src/helpers/common.js | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/server.js b/server.js index 03ef709..ab5a3da 100644 --- a/server.js +++ b/server.js @@ -23,6 +23,9 @@ import('get-port').then(module => { }); process.env.TZ = 'Europe/Sofia'; +// Global variable to store the base URL +let baseUrlGlobal; + const PORT = process.env.NEXT_PUBLIC_PORT || 3000; const HOST = process.env.NEXT_PUBLIC_HOST; @@ -71,6 +74,14 @@ app // Add the middleware to set 'x-forwarded-host' header server.use((req, res, next) => { req.headers['x-forwarded-host'] = req.headers['x-forwarded-host'] || req.headers.host; + // --------------- + if (!baseUrlGloball) { + const protocol = req.headers['x-forwarded-proto'] || 'http'; + const host = req.headers.host; + const baseUrl = `${protocol}://${host}`; + baseUrlGlobal = baseUrl; + fs.writeFileSync(path.join(__dirname, 'baseUrl.txt'), baseUrlGlobal, 'utf8'); + } next(); }); server.use("/favicon.ico", express.static("styles/favicon_io/favicon.ico")); @@ -621,4 +632,4 @@ async function Stat() { Stat(); - +exports.baseUrlGlobal = baseUrlGlobal; diff --git a/src/helpers/common.js b/src/helpers/common.js index 97cfe10..fcfad27 100644 --- a/src/helpers/common.js +++ b/src/helpers/common.js @@ -55,20 +55,39 @@ exports.isValidPhoneNumber = function (phone) { return false; } exports.getBaseUrl = function (relative = "") { - const host = process.env.NEXT_PUBLIC_HOST || '127.0.0.1'; - const port = process.env.NEXT_PUBLIC_PORT ? `:${process.env.NEXT_PUBLIC_PORT}` : ''; - const protocol = process.env.NEXT_PUBLIC_PROTOCOL || "https" + const filePath = path.join(__dirname, 'baseUrl.txt'); - //const url = `${protocol}://${host}${port}/${relative.replace(/^\/|\/$/g, '')}/`; - const isRelativeEmpty = !relative || relative.trim() === ''; - const formattedRelative = !isRelativeEmpty ? '/' + relative.replace(/^\/|\/$/g, '') : ''; - const url = `${protocol}://${host}${port}${formattedRelative}`; + try { + // Check if the base URL file exists + if (fs.existsSync(filePath)) { + // Read the base URL from the file + const baseUrl = fs.readFileSync(filePath, 'utf8').trim(); + // If a relative path is provided, append it to the base URL + const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl; + return fullUrl; + } else { + console.log('Base URL file does not exist.'); + return null; + } + } catch (error) { + console.error('Error reading the base URL file:', error); + return null; + } + + // const host = process.env.NEXT_PUBLIC_HOST || '127.0.0.1'; + // const port = process.env.NEXT_PUBLIC_PORT ? `:${process.env.NEXT_PUBLIC_PORT}` : ''; + // const protocol = process.env.NEXT_PUBLIC_PROTOCOL || "https" + + // //const url = `${protocol}://${host}${port}/${relative.replace(/^\/|\/$/g, '')}/`; + // const isRelativeEmpty = !relative || relative.trim() === ''; + // const formattedRelative = !isRelativeEmpty ? '/' + relative.replace(/^\/|\/$/g, '') : ''; + // const url = `${protocol}://${host}${port}${formattedRelative}`; - logger.debug("NODE_ENV = ", process.env.NODE_ENV, "protocol:", protocol); - logger.debug("getBaseURL = ", url); + // logger.debug("NODE_ENV = ", process.env.NODE_ENV, "protocol:", protocol); + // logger.debug("getBaseURL = ", url); - return url; + // return url; }; let prisma;