This commit is contained in:
Iliyan Angelov
2025-11-25 02:06:38 +02:00
parent 2f6dca736a
commit 82024016cd
37 changed files with 1800 additions and 1478 deletions

View File

@@ -6,17 +6,62 @@
* In Production: Uses Next.js rewrites/nginx proxy at /api (internal network only)
*/
// Production: Use relative URLs (nginx proxy)
// Development: Use full backend URL
// Docker: Use backend service name or port 1086
// Production: Use relative URLs (nginx proxy) for client-side
// For server-side (SSR), use internal backend URL or public domain
const isProduction = process.env.NODE_ENV === 'production';
const isDocker = process.env.DOCKER_ENV === 'true';
export const API_BASE_URL = isDocker
? (process.env.NEXT_PUBLIC_API_URL || 'http://backend:1086')
: isProduction
? '' // Use relative URLs in production (proxied by nginx)
: (process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000');
// Detect if we're on the server (Node.js) or client (browser)
const isServer = typeof window === 'undefined';
// For server-side rendering, we need an absolute URL
// During build time, use internal backend URL directly (faster, no SSL issues)
// At runtime, use public domain (goes through nginx which adds API key header)
const getServerApiUrl = () => {
if (isProduction) {
// Check if we're in build context (no access to window, and NEXT_PHASE might be set)
// During build, use internal backend URL directly
// At runtime (SSR), use public domain through nginx
const isBuildTime = process.env.NEXT_PHASE === 'phase-production-build' ||
!process.env.NEXT_RUNTIME;
if (isBuildTime) {
// Build time: use internal backend URL directly
return process.env.INTERNAL_API_URL || 'http://127.0.0.1:1086';
} else {
// Runtime SSR: use public domain - nginx will proxy and add API key header
return process.env.NEXT_PUBLIC_SITE_URL || 'https://gnxsoft.com';
}
}
return process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:1086';
};
// For client-side, use relative URLs in production (proxied by nginx)
// For server-side, use absolute URLs
export const API_BASE_URL = isServer
? getServerApiUrl() // Server-side: absolute URL
: (isProduction
? '' // Client-side production: relative URLs (proxied by nginx)
: (process.env.NEXT_PUBLIC_API_URL || 'http://127.0.0.1:1086')); // Development: direct backend
// Internal API key for server-side requests (must match backend INTERNAL_API_KEY)
// This is only used when calling backend directly (build time or internal requests)
export const INTERNAL_API_KEY = process.env.INTERNAL_API_KEY || '9hZtPwyScigoBAl59Uvcz_9VztSRC6Zt_6L1B2xTM2M';
// Helper to get headers for API requests
// Adds API key header when calling internal backend directly
export const getApiHeaders = (): Record<string, string> => {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};
// If we're calling the internal backend directly (not through nginx),
// add the API key header
if (isServer && API_BASE_URL.includes('127.0.0.1:1086')) {
headers['X-Internal-API-Key'] = INTERNAL_API_KEY;
}
return headers;
};
export const API_CONFIG = {
// Django API Base URL