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
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:
@@ -30,12 +30,24 @@ export const useWebSocketStore = defineStore("websocket", () => {
|
||||
});
|
||||
|
||||
// Helper functions
|
||||
const getWebSocketUrl = () => {
|
||||
const getWebSocketUrl = async () => {
|
||||
// Use environment variable or fallback
|
||||
if (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
|
||||
if (import.meta.env.PROD) {
|
||||
const apiUrl =
|
||||
@@ -76,7 +88,7 @@ export const useWebSocketStore = defineStore("websocket", () => {
|
||||
};
|
||||
|
||||
// Actions
|
||||
const connect = () => {
|
||||
const connect = async () => {
|
||||
if (ws.value?.readyState === WebSocket.OPEN || isConnecting.value) {
|
||||
console.log("WebSocket already connected or connecting");
|
||||
return;
|
||||
@@ -86,7 +98,7 @@ export const useWebSocketStore = defineStore("websocket", () => {
|
||||
clearReconnectTimeout();
|
||||
|
||||
try {
|
||||
const wsUrl = getWebSocketUrl();
|
||||
const wsUrl = await getWebSocketUrl();
|
||||
console.log("Connecting to WebSocket:", wsUrl);
|
||||
|
||||
ws.value = new WebSocket(wsUrl);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import SiteConfig from "../models/SiteConfig.js";
|
||||
import { config as appConfig } from "../config/index.js";
|
||||
|
||||
/**
|
||||
* Public configuration routes
|
||||
@@ -22,6 +23,11 @@ export default async function configRoutes(fastify, options) {
|
||||
scheduledEnd: config.maintenance.scheduledEnd,
|
||||
},
|
||||
|
||||
// WebSocket URL
|
||||
websocket: {
|
||||
url: appConfig.websocket.url,
|
||||
},
|
||||
|
||||
// Features
|
||||
features: {
|
||||
twoFactorAuth: config.features.twoFactorAuth,
|
||||
|
||||
Reference in New Issue
Block a user