Spaces:
Sleeping
Sleeping
File size: 11,840 Bytes
c024705 eeacc46 c024705 eeacc46 c024705 eeacc46 c024705 eeacc46 c024705 818ef98 c024705 818ef98 c024705 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
/**
* Frontend Configuration Management
* Centralized configuration for the AIMHSA chatbot frontend
*/
(() => {
'use strict';
// Default configuration
const DEFAULT_CONFIG = {
// API Configuration
api: {
baseUrl: 'https://prodevroger-ishingiro.hf.space',
timeout: 10000,
retryAttempts: 3,
retryDelay: 1000
},
// UI Configuration
ui: {
theme: 'dark',
language: 'en',
autoRefreshInterval: 30000,
animationDuration: 300
},
// Chat Configuration
chat: {
maxMessageLength: 5000,
typingIndicatorDelay: 1000,
autoScroll: true,
saveConversations: true
},
// Professional Dashboard Configuration
professional: {
defaultPageSize: 25,
autoRefreshBookings: true,
notificationSound: true
},
// Admin Dashboard Configuration
admin: {
defaultPageSize: 50,
enableDataExport: true,
showAdvancedStats: true
}
};
// Environment-specific configurations
const ENVIRONMENT_CONFIGS = {
development: {
api: {
baseUrl: 'https://prodevroger-ishingiro.hf.space'
}
},
production: {
api: {
baseUrl: 'https://prodevroger-ishingiro.hf.space'
}
},
staging: {
api: {
baseUrl: 'https://prodevroger-ishingiro.hf.space'
}
}
};
class ConfigManager {
constructor() {
this.config = { ...DEFAULT_CONFIG };
this.environment = this.detectEnvironment();
this.loadConfiguration();
}
/**
* Detect current environment
*/
detectEnvironment() {
const hostname = window.location.hostname;
const port = window.location.port;
if (hostname === 'localhost' || hostname === '127.0.0.1') {
return 'development';
} else if (hostname.includes('staging')) {
return 'staging';
} else {
return 'production';
}
}
/**
* Load configuration from multiple sources
*/
loadConfiguration() {
try {
// 1. Apply environment-specific config
this.applyEnvironmentConfig();
// 2. Load from localStorage (user preferences)
this.loadFromLocalStorage();
// 3. Load from URL parameters
this.loadFromUrlParams();
// 4. Auto-detect API URL based on current location
this.autoDetectApiUrl();
console.log('π§ Configuration loaded:', this.config);
} catch (error) {
console.error('β Error loading configuration:', error);
// Fallback to default config
this.config = { ...DEFAULT_CONFIG };
}
}
/**
* Apply environment-specific configuration
*/
applyEnvironmentConfig() {
const envConfig = ENVIRONMENT_CONFIGS[this.environment];
if (envConfig) {
this.config = this.deepMerge(this.config, envConfig);
console.log(`π Applied ${this.environment} environment config`);
}
}
/**
* Load configuration from localStorage
*/
loadFromLocalStorage() {
try {
const savedConfig = localStorage.getItem('aimhsa_config');
if (savedConfig) {
const parsedConfig = JSON.parse(savedConfig);
this.config = this.deepMerge(this.config, parsedConfig);
console.log('πΎ Loaded config from localStorage');
}
} catch (error) {
console.warn('β οΈ Failed to load config from localStorage:', error);
}
}
/**
* Load configuration from URL parameters
*/
loadFromUrlParams() {
const urlParams = new URLSearchParams(window.location.search);
// API Base URL override
const apiUrl = urlParams.get('api_url') || urlParams.get('baseUrl');
if (apiUrl) {
this.config.api.baseUrl = apiUrl;
console.log('π API URL overridden from URL params:', apiUrl);
}
// Environment override
const env = urlParams.get('env') || urlParams.get('environment');
if (env && ENVIRONMENT_CONFIGS[env]) {
this.environment = env;
this.applyEnvironmentConfig();
console.log('π Environment overridden from URL params:', env);
}
// Theme override
const theme = urlParams.get('theme');
if (theme) {
this.config.ui.theme = theme;
console.log('π¨ Theme overridden from URL params:', theme);
}
}
/**
* Auto-detect API URL based on current page location
*/
autoDetectApiUrl() {
const currentLocation = window.location;
// Smart API URL detection
if (currentLocation.port === '8000') {
// Development server (likely Python/Django)
this.config.api.baseUrl = `${currentLocation.protocol}//${currentLocation.hostname}:7860`;
} else if (currentLocation.port === '3000') {
// React development server
this.config.api.baseUrl = `${currentLocation.protocol}//${currentLocation.hostname}:7860`;
} else if (currentLocation.port === '7860') {
// Running on API port
this.config.api.baseUrl = currentLocation.origin;
} else if (currentLocation.port === '80' || currentLocation.port === '443' || !currentLocation.port) {
// Production environment
this.config.api.baseUrl = currentLocation.origin;
}
console.log('π Auto-detected API URL:', this.config.api.baseUrl);
}
/**
* Get configuration value
*/
get(path, defaultValue = null) {
return this.getNestedValue(this.config, path, defaultValue);
}
/**
* Set configuration value
*/
set(path, value) {
this.setNestedValue(this.config, path, value);
this.saveToLocalStorage();
}
/**
* Get API base URL
*/
getApiBaseUrl() {
return this.config.api.baseUrl;
}
/**
* Set API base URL
*/
setApiBaseUrl(url) {
this.config.api.baseUrl = url;
this.saveToLocalStorage();
console.log('π API Base URL updated:', url);
}
/**
* Get full API URL for an endpoint
*/
getApiUrl(endpoint = '') {
const baseUrl = this.config.api.baseUrl;
const cleanEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`;
return `${baseUrl}${cleanEndpoint}`;
}
/**
* Save current configuration to localStorage
*/
saveToLocalStorage() {
try {
localStorage.setItem('aimhsa_config', JSON.stringify(this.config));
console.log('πΎ Configuration saved to localStorage');
} catch (error) {
console.warn('β οΈ Failed to save config to localStorage:', error);
}
}
/**
* Reset configuration to defaults
*/
reset() {
this.config = { ...DEFAULT_CONFIG };
localStorage.removeItem('aimhsa_config');
this.loadConfiguration();
console.log('π Configuration reset to defaults');
}
/**
* Deep merge objects
*/
deepMerge(target, source) {
const result = { ...target };
for (const key in source) {
if (source.hasOwnProperty(key)) {
if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) {
result[key] = this.deepMerge(result[key] || {}, source[key]);
} else {
result[key] = source[key];
}
}
}
return result;
}
/**
* Get nested object value by path
*/
getNestedValue(obj, path, defaultValue = null) {
const keys = path.split('.');
let current = obj;
for (const key of keys) {
if (current && current.hasOwnProperty(key)) {
current = current[key];
} else {
return defaultValue;
}
}
return current;
}
/**
* Set nested object value by path
*/
setNestedValue(obj, path, value) {
const keys = path.split('.');
let current = obj;
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
if (!current[key] || typeof current[key] !== 'object') {
current[key] = {};
}
current = current[key];
}
current[keys[keys.length - 1]] = value;
}
/**
* Validate API connection
*/
async validateApiConnection() {
try {
const response = await fetch(`${this.config.api.baseUrl}/health`, {
method: 'GET',
timeout: this.config.api.timeout
});
if (response.ok) {
console.log('β
API connection validated');
return true;
} else {
console.warn('β οΈ API responded but not healthy:', response.status);
return false;
}
} catch (error) {
console.error('β API connection failed:', error);
return false;
}
}
/**
* Get environment info
*/
getEnvironmentInfo() {
return {
environment: this.environment,
hostname: window.location.hostname,
port: window.location.port,
protocol: window.location.protocol,
apiBaseUrl: this.config.api.baseUrl,
userAgent: navigator.userAgent,
timestamp: new Date().toISOString()
};
}
}
// Create global configuration manager instance
const configManager = new ConfigManager();
// Export to global scope
window.AIMHSA = window.AIMHSA || {};
window.AIMHSA.Config = configManager;
// Legacy support
window.getApiBaseUrl = () => configManager.getApiBaseUrl();
window.getApiUrl = (endpoint) => configManager.getApiUrl(endpoint);
console.log('βοΈ AIMHSA Configuration Manager initialized');
console.log('π Environment:', configManager.environment);
console.log('π API Base URL:', configManager.getApiBaseUrl());
})();
|