Update server-service.ts

This commit is contained in:
NotII
2025-03-10 18:31:05 +00:00
parent bc6280efda
commit ae9030a4b0

View File

@@ -5,9 +5,18 @@ import { redirect } from 'next/navigation';
* Gets the base URL for server API requests with proper fallbacks * Gets the base URL for server API requests with proper fallbacks
*/ */
function getBaseUrl() { function getBaseUrl() {
// Check if we're running in Tor mode
const torMode = process.env.NEXT_PUBLIC_TOR_MODE === 'true';
// First check for the specific server API URL environment variable // First check for the specific server API URL environment variable
if (process.env.SERVER_API_URL) { if (process.env.SERVER_API_URL) {
console.log(`Using SERVER_API_URL: ${process.env.SERVER_API_URL}`); console.log(`Using SERVER_API_URL: ${process.env.SERVER_API_URL}`);
// If we're in Tor mode and the SERVER_API_URL is set to a relative path, use it directly
if (torMode && process.env.SERVER_API_URL.startsWith('/')) {
return process.env.SERVER_API_URL;
}
return process.env.SERVER_API_URL; return process.env.SERVER_API_URL;
} }
@@ -20,6 +29,11 @@ function getBaseUrl() {
// Check if we're running in a container/production environment // Check if we're running in a container/production environment
const inContainer = process.env.NODE_ENV === 'production'; const inContainer = process.env.NODE_ENV === 'production';
// If in Tor mode, prefer relative URLs
if (torMode && apiUrl && apiUrl.startsWith('/')) {
return apiUrl;
}
// We need to get the host from somewhere to construct the URL // We need to get the host from somewhere to construct the URL
// In production, we can rely on the VERCEL_URL or similar // In production, we can rely on the VERCEL_URL or similar
if (process.env.VERCEL_URL) { if (process.env.VERCEL_URL) {
@@ -37,13 +51,22 @@ function getBaseUrl() {
const port = process.env.INTERNAL_API_PORT || '3000'; const port = process.env.INTERNAL_API_PORT || '3000';
const protocol = (process.env.USE_HTTPS === 'false') ? 'http' : 'https'; const protocol = (process.env.USE_HTTPS === 'false') ? 'http' : 'https';
// If in Tor mode, prefer relative URLs
if (torMode) {
return apiUrl.startsWith('/') ? apiUrl : `/${apiUrl}`;
}
// Otherwise, it's likely a relative path like /api // Otherwise, it's likely a relative path like /api
// Use localhost with the correct port for container environments // Use localhost with the correct port for container environments
return `${protocol}://localhost:${port}${apiUrl.startsWith('/') ? apiUrl : `/${apiUrl}`}`; return `${protocol}://localhost:${port}${apiUrl.startsWith('/') ? apiUrl : `/${apiUrl}`}`;
} }
// Last resort fallback // Last resort fallback - if in Tor mode, use a relative URL
// Use http for container environment, https for external services if (torMode) {
return '/api';
}
// For regular container environments
const protocol = (process.env.USE_HTTPS === 'false') ? 'http' : 'https'; const protocol = (process.env.USE_HTTPS === 'false') ? 'http' : 'https';
const port = process.env.INTERNAL_API_PORT || '3000'; const port = process.env.INTERNAL_API_PORT || '3000';
return `${protocol}://localhost:${port}/api`; return `${protocol}://localhost:${port}/api`;
@@ -63,7 +86,39 @@ export async function fetchServer<T = unknown>(
try { try {
const baseUrl = getBaseUrl(); const baseUrl = getBaseUrl();
const torMode = process.env.NEXT_PUBLIC_TOR_MODE === 'true';
// Special handling for Tor mode
if (torMode) {
// For Tor, we need to be extra careful with URL construction
// Remove leading slash from endpoint if present
const normalizedEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;
// If baseUrl is a relative path (starts with /)
if (baseUrl.startsWith('/')) {
// Combine paths carefully to create a relative URL
const url = `${baseUrl}${baseUrl.endsWith('/') ? '' : '/'}${normalizedEndpoint}`;
console.log(`Tor mode: Using relative URL for server fetch: ${url}`);
const res = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${authToken}`,
...options.headers,
},
cache: 'no-store',
});
if (res.status === 401) redirect('/login');
if (!res.ok) throw new Error(`Request failed: ${res.statusText}`);
return res.json();
}
}
// Regular URL construction for non-Tor environments
// Ensure endpoint doesn't start with a slash if baseUrl ends with one // Ensure endpoint doesn't start with a slash if baseUrl ends with one
const normalizedEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint; const normalizedEndpoint = endpoint.startsWith('/') ? endpoint.substring(1) : endpoint;