dynamic baseURL

This commit is contained in:
Dobromir Popov
2024-02-26 22:44:25 +02:00
parent 9de291fc0c
commit bdacb18a07
3 changed files with 33 additions and 13 deletions

2
package-lock.json generated
View File

@ -11333,4 +11333,4 @@
}
}
}
}
}

View File

@ -24,7 +24,7 @@ const axiosServer = async (context) => {
//headers['X-From-Server'] = token; // Note: Using the entire token as a header value might not be intended
return axios.create({
baseURL: common.getBaseUrl(),
baseURL: common.getBaseUrl("", context.req || req),
withCredentials: true,
headers: headers,
});

View File

@ -4,11 +4,19 @@
const levenshtein = require('fastest-levenshtein');
const fs = require("fs");
const path = require("path");
const { PrismaClient } = require('@prisma/client');
const DayOfWeek = require("@prisma/client").DayOfWeek;
const winston = require('winston');
// User and auth functions
// import { getSession } from "next-auth/react";
// import { UserRole } from "@prisma/client";
//convert to es6 import
const { getSession } = require("next-auth/react");
const { UserRole } = require("@prisma/client");
// const { set } = require('date-fns');
const logger = winston.createLogger({
level: 'info', // Set the default log level
@ -54,7 +62,17 @@ exports.isValidPhoneNumber = function (phone) {
// If neither condition is met, the phone number is invalid
return false;
}
exports.getBaseUrl = function (relative = "") {
exports.setBaseUrl = function (req) {
const protocol = req.headers['x-forwarded-proto'] || 'http';
const host = req.headers.host;
const baseUrl = `${protocol}://${host}`;
fs.writeFileSync(path.join(__dirname, 'baseUrl.txt'), baseUrlGlobal, 'utf8');
return baseUrl;
}
exports.getBaseUrl = function (relative = "", req = null) {
const filePath = path.join(__dirname, 'baseUrl.txt');
try {
@ -66,6 +84,12 @@ exports.getBaseUrl = function (relative = "") {
const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl;
return fullUrl;
} else {
if (req) {
const baseUrl = exports.setBaseUrl(req);
return `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}/`;
}
console.log('Base URL file does not exist.');
return null;
}
@ -75,10 +99,10 @@ 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 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 url = `${ protocol }://${host}${port}/${relative.replace(/^\/|\/$/g, '')}/`;
// const isRelativeEmpty = !relative || relative.trim() === '';
// const formattedRelative = !isRelativeEmpty ? '/' + relative.replace(/^\/|\/$/g, '') : '';
// const url = `${protocol}://${host}${port}${formattedRelative}`;
@ -88,7 +112,8 @@ exports.getBaseUrl = function (relative = "") {
// logger.debug("getBaseURL = ", url);
// return url;
};
}
let prisma;
exports.getPrismaClient = function getPrismaClient() {
@ -307,7 +332,7 @@ exports.getTimeFomatted = function (date) {
/*Todo: remove:
toISOString
slice(0, 10)
getISODateFromDateTime > getISODateOnly
*/
@ -628,12 +653,7 @@ exports.copyToClipboard = function (event, text) {
}
// User and auth functions
// import { getSession } from "next-auth/react";
// import { UserRole } from "@prisma/client";
//convert to es6 import
const { getSession } = require("next-auth/react");
const { UserRole } = require("@prisma/client");
exports.getUser = async function (req) {
// Use req if provided (server-side), otherwise call getSession without args (client-side)