Update CORS settings, enhance API URL detection, and improve template rendering
- Added new CORS origins for local development and specific IP addresses in `settings.py`. - Refactored API URL retrieval logic in `loadSiteDefinition.ts` and `fileAccess.ts` to auto-detect based on the current origin. - Enhanced error handling in API calls and improved logging for better debugging. - Updated `renderTemplate` function to support additional block types and improved rendering logic for various components in `templateEngine.tsx`.
This commit is contained in:
@@ -7,9 +7,39 @@
|
||||
import axios from 'axios';
|
||||
import type { SiteDefinition } from '../types';
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL || 'https://api.igny8.com/api';
|
||||
const SITES_DATA_PATH = import.meta.env.SITES_DATA_PATH || '/sites';
|
||||
|
||||
/**
|
||||
* Get API base URL - auto-detect based on current origin
|
||||
*/
|
||||
function getApiBaseUrl(): string {
|
||||
// First check environment variables
|
||||
const envUrl = import.meta.env.VITE_API_URL;
|
||||
if (envUrl) {
|
||||
return envUrl.endsWith('/api') ? envUrl : `${envUrl}/api`;
|
||||
}
|
||||
|
||||
// Auto-detect based on current origin (browser only)
|
||||
if (typeof window !== 'undefined') {
|
||||
const origin = window.location.origin;
|
||||
|
||||
// If accessing via IP address, use direct backend port
|
||||
if (/^\d+\.\d+\.\d+\.\d+/.test(origin) || origin.includes('localhost') || origin.includes('127.0.0.1')) {
|
||||
// Backend is on port 8011 (external) when accessed via IP
|
||||
if (origin.includes(':8024')) {
|
||||
return origin.replace(':8024', ':8011') + '/api';
|
||||
}
|
||||
// Default: try port 8011
|
||||
return origin.split(':')[0] + ':8011/api';
|
||||
}
|
||||
}
|
||||
|
||||
// Production: use subdomain
|
||||
return 'https://api.igny8.com/api';
|
||||
}
|
||||
|
||||
const API_URL = getApiBaseUrl();
|
||||
|
||||
/**
|
||||
* Load site definition by site ID.
|
||||
* First tries to load from filesystem (deployed sites),
|
||||
@@ -18,13 +48,26 @@ const SITES_DATA_PATH = import.meta.env.SITES_DATA_PATH || '/sites';
|
||||
export async function loadSiteDefinition(siteId: string): Promise<SiteDefinition> {
|
||||
// Try API endpoint for deployed site definition first
|
||||
try {
|
||||
const response = await axios.get(`${API_URL}/v1/publisher/sites/${siteId}/definition/`);
|
||||
const response = await axios.get(`${API_URL}/v1/publisher/sites/${siteId}/definition/`, {
|
||||
timeout: 10000, // 10 second timeout
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
});
|
||||
if (response.data) {
|
||||
return response.data as SiteDefinition;
|
||||
}
|
||||
} catch (error) {
|
||||
// API load failed, try blueprint endpoint as fallback
|
||||
console.warn('Failed to load deployed site definition, trying blueprint:', error);
|
||||
console.error('Failed to load deployed site definition:', error);
|
||||
if (axios.isAxiosError(error)) {
|
||||
console.error('Error details:', {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
response: error.response?.status,
|
||||
url: error.config?.url,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to blueprint API (for non-deployed sites)
|
||||
|
||||
Reference in New Issue
Block a user