Files
wifi-densepose/ui/config/api.config.js
ruv 3b72f35306 fix: UI auto-detects server port from page origin (#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 #55

Co-Authored-By: claude-flow <ruv@ruv.net>
2026-03-01 02:09:23 -05:00

135 lines
3.7 KiB
JavaScript

// API Configuration for WiFi-DensePose UI
// Auto-detect the backend URL from the page origin so the UI works whether
// served from Docker (:3000), local dev (:8080), or any other port.
const _origin = (typeof window !== 'undefined' && window.location && window.location.origin)
? window.location.origin
: 'http://localhost:3000';
export const API_CONFIG = {
BASE_URL: _origin,
API_VERSION: '/api/v1',
WS_PREFIX: 'ws://',
WSS_PREFIX: 'wss://',
// Mock server configuration (only for testing)
MOCK_SERVER: {
ENABLED: false, // Set to true only for testing without backend
AUTO_DETECT: false, // Disabled — sensing tab uses its own WebSocket on :8765
},
// API Endpoints
ENDPOINTS: {
// Root & Info
ROOT: '/',
INFO: '/api/v1/info',
STATUS: '/api/v1/status',
METRICS: '/api/v1/metrics',
// Health
HEALTH: {
SYSTEM: '/health/health',
READY: '/health/ready',
LIVE: '/health/live',
METRICS: '/health/metrics',
VERSION: '/health/version'
},
// Pose
POSE: {
CURRENT: '/api/v1/pose/current',
ANALYZE: '/api/v1/pose/analyze',
ZONE_OCCUPANCY: '/api/v1/pose/zones/{zone_id}/occupancy',
ZONES_SUMMARY: '/api/v1/pose/zones/summary',
HISTORICAL: '/api/v1/pose/historical',
ACTIVITIES: '/api/v1/pose/activities',
CALIBRATE: '/api/v1/pose/calibrate',
CALIBRATION_STATUS: '/api/v1/pose/calibration/status',
STATS: '/api/v1/pose/stats'
},
// Streaming
STREAM: {
STATUS: '/api/v1/stream/status',
START: '/api/v1/stream/start',
STOP: '/api/v1/stream/stop',
CLIENTS: '/api/v1/stream/clients',
DISCONNECT_CLIENT: '/api/v1/stream/clients/{client_id}',
BROADCAST: '/api/v1/stream/broadcast',
METRICS: '/api/v1/stream/metrics',
// WebSocket endpoints
WS_POSE: '/api/v1/stream/pose',
WS_EVENTS: '/api/v1/stream/events'
},
// Development (only in dev mode)
DEV: {
CONFIG: '/api/v1/dev/config',
RESET: '/api/v1/dev/reset'
}
},
// Default request options
DEFAULT_HEADERS: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
// Rate limiting
RATE_LIMITS: {
REQUESTS_PER_MINUTE: 60,
BURST_LIMIT: 10
},
// WebSocket configuration
WS_CONFIG: {
RECONNECT_DELAY: 5000,
MAX_RECONNECT_ATTEMPTS: 5,
PING_INTERVAL: 30000,
MESSAGE_TIMEOUT: 10000
}
};
// Helper function to build API URLs
export function buildApiUrl(endpoint, params = {}) {
let url = `${API_CONFIG.BASE_URL}${endpoint}`;
// Replace path parameters
Object.keys(params).forEach(key => {
if (url.includes(`{${key}}`)) {
url = url.replace(`{${key}}`, params[key]);
delete params[key];
}
});
// Add query parameters
const queryParams = new URLSearchParams(params);
if (queryParams.toString()) {
url += `?${queryParams.toString()}`;
}
return url;
}
// Helper function to build WebSocket URLs
export function buildWsUrl(endpoint, params = {}) {
// Use secure WebSocket (wss://) when serving over HTTPS or on non-localhost
// Use ws:// only for localhost development
const isLocalhost = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1';
const isSecure = window.location.protocol === 'https:';
const protocol = (isSecure || !isLocalhost)
? API_CONFIG.WSS_PREFIX
: API_CONFIG.WS_PREFIX;
// Derive host from the page origin so it works on any port (Docker :3000, dev :8080, etc.)
const host = window.location.host;
let url = `${protocol}${host}${endpoint}`;
// Add query parameters
const queryParams = new URLSearchParams(params);
if (queryParams.toString()) {
url += `?${queryParams.toString()}`;
}
return url;
}