Enhance backend configuration and error handling: Update CORS settings to allow dynamic origins, improve RabbitMQ connection handling in chat services, and adjust API server host configuration. Refactor environment variables for better flexibility and add fallback mechanisms for WebSocket and chat services. Update frontend environment files for consistent API and WebSocket URLs.
This commit is contained in:
61
frontend/src/utils/appConfig.js
Normal file
61
frontend/src/utils/appConfig.js
Normal file
@@ -0,0 +1,61 @@
|
||||
function trimTrailingSlash(value) {
|
||||
return value ? value.replace(/\/$/, '') : value;
|
||||
}
|
||||
|
||||
function getWindowOrigin() {
|
||||
if (typeof window === 'undefined') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return window.location.origin;
|
||||
}
|
||||
|
||||
function toWsOrigin(value) {
|
||||
if (!value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return value
|
||||
.replace(/^http:\/\//i, 'ws://')
|
||||
.replace(/^https:\/\//i, 'wss://');
|
||||
}
|
||||
|
||||
export function getApiBaseUrl() {
|
||||
return trimTrailingSlash(import.meta.env.VITE_API_BASE_URL || getWindowOrigin() || '');
|
||||
}
|
||||
|
||||
export function getSocketIoUrl() {
|
||||
return trimTrailingSlash(import.meta.env.VITE_SOCKET_IO_URL || getApiBaseUrl() || getWindowOrigin() || '');
|
||||
}
|
||||
|
||||
export function getDaemonSocketUrl() {
|
||||
const configured = import.meta.env.VITE_DAEMON_SOCKET;
|
||||
if (configured) {
|
||||
return configured;
|
||||
}
|
||||
|
||||
return toWsOrigin(getWindowOrigin());
|
||||
}
|
||||
|
||||
export function getPublicBaseUrl() {
|
||||
return trimTrailingSlash(import.meta.env.VITE_PUBLIC_BASE_URL || getWindowOrigin() || 'https://www.your-part.de');
|
||||
}
|
||||
|
||||
export function getChatWsUrlFromEnv() {
|
||||
const directUrl = import.meta.env.VITE_CHAT_WS_URL;
|
||||
if (directUrl) {
|
||||
return directUrl.trim();
|
||||
}
|
||||
|
||||
const host = import.meta.env.VITE_CHAT_WS_HOST;
|
||||
const port = import.meta.env.VITE_CHAT_WS_PORT;
|
||||
const protocol = import.meta.env.VITE_CHAT_WS_PROTOCOL || (typeof window !== 'undefined' && window.location.protocol === 'https:' ? 'wss' : 'ws');
|
||||
|
||||
if (host || port) {
|
||||
const resolvedHost = host || (typeof window !== 'undefined' ? window.location.hostname : 'localhost');
|
||||
const resolvedPort = port ? `:${port}` : '';
|
||||
return `${protocol}://${resolvedHost}${resolvedPort}`;
|
||||
}
|
||||
|
||||
return toWsOrigin(getWindowOrigin());
|
||||
}
|
||||
@@ -1,20 +1,10 @@
|
||||
import axios from 'axios';
|
||||
import store from '../store';
|
||||
import { getApiBaseUrl } from './appConfig.js';
|
||||
|
||||
// API-Basis-URL - Apache-Proxy für Produktion, direkte Verbindung für lokale Entwicklung
|
||||
const getApiBaseURL = () => {
|
||||
// Wenn explizite Umgebungsvariable gesetzt ist, diese verwenden
|
||||
if (import.meta.env.VITE_API_BASE_URL) {
|
||||
return import.meta.env.VITE_API_BASE_URL;
|
||||
}
|
||||
|
||||
// Für lokale Entwicklung: direkte Backend-Verbindung
|
||||
if (import.meta.env.DEV || window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
|
||||
return 'http://localhost:3001';
|
||||
}
|
||||
|
||||
// Für Produktion: Root-Pfad, da API-Endpunkte bereits mit /api beginnen
|
||||
return '';
|
||||
return getApiBaseUrl();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
// Centralized config for YourChat protocol mapping and WS endpoint
|
||||
// Override via .env (VITE_* variables)
|
||||
import { getChatWsUrlFromEnv } from './appConfig.js';
|
||||
|
||||
const env = import.meta.env || {};
|
||||
|
||||
export const CHAT_WS_URL = env.VITE_CHAT_WS_URL
|
||||
|| (env.VITE_CHAT_WS_HOST || env.VITE_CHAT_WS_PORT
|
||||
? `ws://${env.VITE_CHAT_WS_HOST || 'localhost'}:${env.VITE_CHAT_WS_PORT || '1235'}`
|
||||
: (typeof window !== 'undefined' && window.location.protocol === 'https:' ? 'wss://' : 'ws://') + window.location.host + '/socket.io/');
|
||||
export const CHAT_WS_URL = getChatWsUrlFromEnv();
|
||||
|
||||
// Event/type keys
|
||||
export const CHAT_EVENT_KEY = env.VITE_CHAT_EVENT_KEY || 'type';
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { getPublicBaseUrl } from './appConfig.js';
|
||||
|
||||
const DEFAULT_BASE_URL = 'https://www.your-part.de';
|
||||
const DEFAULT_SITE_NAME = 'YourPart';
|
||||
const DEFAULT_TITLE = 'YourPart - Community, Chat, Forum, Vokabeltrainer, Falukant und Minispiele';
|
||||
@@ -21,7 +23,7 @@ const MANAGED_META_KEYS = [
|
||||
];
|
||||
|
||||
function getBaseUrl() {
|
||||
return (import.meta.env.VITE_PUBLIC_BASE_URL || DEFAULT_BASE_URL).replace(/\/$/, '');
|
||||
return getPublicBaseUrl().replace(/\/$/, '') || DEFAULT_BASE_URL;
|
||||
}
|
||||
|
||||
function upsertMeta(attr, key, content) {
|
||||
|
||||
Reference in New Issue
Block a user