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

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

View File

@ -4,11 +4,19 @@
const levenshtein = require('fastest-levenshtein'); const levenshtein = require('fastest-levenshtein');
const fs = require("fs");
const path = require("path"); const path = require("path");
const { PrismaClient } = require('@prisma/client'); const { PrismaClient } = require('@prisma/client');
const DayOfWeek = require("@prisma/client").DayOfWeek; const DayOfWeek = require("@prisma/client").DayOfWeek;
const winston = require('winston'); 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({ const logger = winston.createLogger({
level: 'info', // Set the default log level 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 // If neither condition is met, the phone number is invalid
return false; 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'); const filePath = path.join(__dirname, 'baseUrl.txt');
try { try {
@ -66,6 +84,12 @@ exports.getBaseUrl = function (relative = "") {
const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl; const fullUrl = relative ? new URL(relative, baseUrl).toString() : baseUrl;
return fullUrl; return fullUrl;
} else { } else {
if (req) {
const baseUrl = exports.setBaseUrl(req);
return `${baseUrl}/${relative.replace(/^\/|\/$/g, '')}/`;
}
console.log('Base URL file does not exist.'); console.log('Base URL file does not exist.');
return null; return null;
} }
@ -75,10 +99,10 @@ exports.getBaseUrl = function (relative = "") {
} }
// const host = process.env.NEXT_PUBLIC_HOST || '127.0.0.1'; // 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 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 isRelativeEmpty = !relative || relative.trim() === '';
// const formattedRelative = !isRelativeEmpty ? '/' + relative.replace(/^\/|\/$/g, '') : ''; // const formattedRelative = !isRelativeEmpty ? '/' + relative.replace(/^\/|\/$/g, '') : '';
// const url = `${protocol}://${host}${port}${formattedRelative}`; // const url = `${protocol}://${host}${port}${formattedRelative}`;
@ -88,7 +112,8 @@ exports.getBaseUrl = function (relative = "") {
// logger.debug("getBaseURL = ", url); // logger.debug("getBaseURL = ", url);
// return url; // return url;
}; }
let prisma; let prisma;
exports.getPrismaClient = function getPrismaClient() { exports.getPrismaClient = function getPrismaClient() {
@ -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) { exports.getUser = async function (req) {
// Use req if provided (server-side), otherwise call getSession without args (client-side) // Use req if provided (server-side), otherwise call getSession without args (client-side)