set base URL dynamiically

This commit is contained in:
Dobromir Popov
2024-02-25 13:51:19 +02:00
parent 9baea7c8b7
commit f4e3d9cc2e
2 changed files with 41 additions and 11 deletions

View File

@ -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;