set base URL dynamiically
This commit is contained in:
13
server.js
13
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;
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user