Add WebSocket URL to backend config API - frontend now reads from backend .env
All checks were successful
Build Frontend / Build Frontend (push) Successful in 9s

- Added websocket.url to backend config
- Updated /api/config/public endpoint to return WebSocket URL
- Frontend now fetches WS URL from backend config API
- Falls back to environment variable or auto-generated URL
- Add WS_URL to backend .env: wss://api.turbotrades.dev/ws
This commit is contained in:
2026-01-11 02:01:34 +00:00
parent 0bd3f60627
commit ae18e8b530
2 changed files with 21 additions and 3 deletions

View File

@@ -30,12 +30,24 @@ export const useWebSocketStore = defineStore("websocket", () => {
}); });
// Helper functions // Helper functions
const getWebSocketUrl = () => { const getWebSocketUrl = async () => {
// Use environment variable or fallback // Use environment variable or fallback
if (import.meta.env.VITE_WS_URL) { if (import.meta.env.VITE_WS_URL) {
return import.meta.env.VITE_WS_URL; return import.meta.env.VITE_WS_URL;
} }
// Try to fetch from backend config API
try {
const apiUrl = import.meta.env.VITE_API_URL || "http://localhost:3000";
const response = await fetch(`${apiUrl}/api/config/public`);
const data = await response.json();
if (data.success && data.config.websocket?.url) {
return data.config.websocket.url;
}
} catch (error) {
console.warn("Failed to fetch WebSocket URL from config:", error);
}
// In production, use main API domain with /ws path // In production, use main API domain with /ws path
if (import.meta.env.PROD) { if (import.meta.env.PROD) {
const apiUrl = const apiUrl =
@@ -76,7 +88,7 @@ export const useWebSocketStore = defineStore("websocket", () => {
}; };
// Actions // Actions
const connect = () => { const connect = async () => {
if (ws.value?.readyState === WebSocket.OPEN || isConnecting.value) { if (ws.value?.readyState === WebSocket.OPEN || isConnecting.value) {
console.log("WebSocket already connected or connecting"); console.log("WebSocket already connected or connecting");
return; return;
@@ -86,7 +98,7 @@ export const useWebSocketStore = defineStore("websocket", () => {
clearReconnectTimeout(); clearReconnectTimeout();
try { try {
const wsUrl = getWebSocketUrl(); const wsUrl = await getWebSocketUrl();
console.log("Connecting to WebSocket:", wsUrl); console.log("Connecting to WebSocket:", wsUrl);
ws.value = new WebSocket(wsUrl); ws.value = new WebSocket(wsUrl);

View File

@@ -1,4 +1,5 @@
import SiteConfig from "../models/SiteConfig.js"; import SiteConfig from "../models/SiteConfig.js";
import { config as appConfig } from "../config/index.js";
/** /**
* Public configuration routes * Public configuration routes
@@ -22,6 +23,11 @@ export default async function configRoutes(fastify, options) {
scheduledEnd: config.maintenance.scheduledEnd, scheduledEnd: config.maintenance.scheduledEnd,
}, },
// WebSocket URL
websocket: {
url: appConfig.websocket.url,
},
// Features // Features
features: { features: {
twoFactorAuth: config.features.twoFactorAuth, twoFactorAuth: config.features.twoFactorAuth,