Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3b72f35

Browse files
committed
fix: UI auto-detects server port from page origin (ruvnet#55)
The UI had hardcoded localhost:8080 for HTTP and localhost:8765 for WebSocket, causing "Backend unavailable" when served from Docker (port 3000) or any non-default port. Changes: - api.config.js: BASE_URL now uses window.location.origin instead of hardcoded localhost:8080 - api.config.js: buildWsUrl() uses window.location.host instead of hardcoded localhost:8080 - sensing.service.js: WebSocket URL derived from page origin instead of hardcoded localhost:8765 - main.rs: Added /ws/sensing route to the HTTP server so WebSocket and REST are reachable on a single port Fixes ruvnet#55 Co-Authored-By: claude-flow <[email protected]>
1 parent a0b5506 commit 3b72f35

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

rust-port/wifi-densepose-rs/crates/wifi-densepose-sensing-server/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,6 +2091,8 @@ async fn main() {
20912091
// Stream endpoints
20922092
.route("/api/v1/stream/status", get(stream_status))
20932093
.route("/api/v1/stream/pose", get(ws_pose_handler))
2094+
// Sensing WebSocket on the HTTP port so the UI can reach it without a second port
2095+
.route("/ws/sensing", get(ws_sensing_handler))
20942096
// Static UI files
20952097
.nest_service("/ui", ServeDir::new(&ui_path))
20962098
.layer(SetResponseHeaderLayer::overriding(

ui/config/api.config.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
// API Configuration for WiFi-DensePose UI
22

3+
// Auto-detect the backend URL from the page origin so the UI works whether
4+
// served from Docker (:3000), local dev (:8080), or any other port.
5+
const _origin = (typeof window !== 'undefined' && window.location && window.location.origin)
6+
? window.location.origin
7+
: 'http://localhost:3000';
8+
39
export const API_CONFIG = {
4-
BASE_URL: 'http://localhost:8080', // Rust sensing server port
10+
BASE_URL: _origin,
511
API_VERSION: '/api/v1',
612
WS_PREFIX: 'ws://',
713
WSS_PREFIX: 'wss://',
8-
14+
915
// Mock server configuration (only for testing)
1016
MOCK_SERVER: {
1117
ENABLED: false, // Set to true only for testing without backend
@@ -114,9 +120,9 @@ export function buildWsUrl(endpoint, params = {}) {
114120
const protocol = (isSecure || !isLocalhost)
115121
? API_CONFIG.WSS_PREFIX
116122
: API_CONFIG.WS_PREFIX;
117-
118-
// Match Rust sensing server port
119-
const host = 'localhost:8080';
123+
124+
// Derive host from the page origin so it works on any port (Docker :3000, dev :8080, etc.)
125+
const host = window.location.host;
120126
let url = `${protocol}${host}${endpoint}`;
121127

122128
// Add query parameters

ui/services/sensing.service.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
* always shows something.
99
*/
1010

11-
const SENSING_WS_URL = 'ws://localhost:8765/ws/sensing';
11+
// Derive WebSocket URL from the page origin so it works on any port
12+
// (Docker :3000, native :8080, etc.)
13+
const _wsProto = (typeof window !== 'undefined' && window.location.protocol === 'https:') ? 'wss:' : 'ws:';
14+
const _wsHost = (typeof window !== 'undefined' && window.location.host) ? window.location.host : 'localhost:3000';
15+
const SENSING_WS_URL = `${_wsProto}//${_wsHost}/ws/sensing`;
1216
const RECONNECT_DELAYS = [1000, 2000, 4000, 8000, 16000];
1317
const MAX_RECONNECT_ATTEMPTS = 10;
1418
const SIMULATION_INTERVAL = 500; // ms

0 commit comments

Comments
 (0)