Replace Python FastAPI + WebSocket servers with a single 2.1MB Rust binary (wifi-densepose-sensing-server) that serves all UI endpoints: - REST: /health/*, /api/v1/info, /api/v1/pose/current, /api/v1/pose/stats, /api/v1/pose/zones/summary, /api/v1/stream/status - WebSocket: /api/v1/stream/pose (pose_data with 17 COCO keypoints), /ws/sensing (raw sensing_update stream on port 8765) - Static: /ui/* with no-cache headers WiFi-derived pose estimation: derive_pose_from_sensing() generates 17 COCO keypoints from CSI/WiFi signal data with motion-driven animation. Data sources: ESP32 CSI via UDP :5005, Windows WiFi via netsh, simulation fallback. Auto-detection probes each in order. UI changes: - Point all endpoints to Rust server on :8080 (was Python :8000) - Fix WebSocket sensing URL to include /ws/sensing path - Remove sensingOnlyMode gating — all tabs init normally - Remove api.service.js sensing-only short-circuit - Fix clearPingInterval bug in websocket.service.js Also removes obsolete k8s/ template manifests. Co-Authored-By: claude-flow <ruv@ruv.net>
150 lines
4.0 KiB
JavaScript
150 lines
4.0 KiB
JavaScript
// API Service for WiFi-DensePose UI
|
|
|
|
import { API_CONFIG, buildApiUrl } from '../config/api.config.js';
|
|
import { backendDetector } from '../utils/backend-detector.js';
|
|
|
|
export class ApiService {
|
|
constructor() {
|
|
this.authToken = null;
|
|
this.requestInterceptors = [];
|
|
this.responseInterceptors = [];
|
|
}
|
|
|
|
// Set authentication token
|
|
setAuthToken(token) {
|
|
this.authToken = token;
|
|
}
|
|
|
|
// Add request interceptor
|
|
addRequestInterceptor(interceptor) {
|
|
this.requestInterceptors.push(interceptor);
|
|
}
|
|
|
|
// Add response interceptor
|
|
addResponseInterceptor(interceptor) {
|
|
this.responseInterceptors.push(interceptor);
|
|
}
|
|
|
|
// Build headers for requests
|
|
getHeaders(customHeaders = {}) {
|
|
const headers = {
|
|
...API_CONFIG.DEFAULT_HEADERS,
|
|
...customHeaders
|
|
};
|
|
|
|
if (this.authToken) {
|
|
headers['Authorization'] = `Bearer ${this.authToken}`;
|
|
}
|
|
|
|
return headers;
|
|
}
|
|
|
|
// Process request through interceptors
|
|
async processRequest(url, options) {
|
|
let processedUrl = url;
|
|
let processedOptions = options;
|
|
|
|
for (const interceptor of this.requestInterceptors) {
|
|
const result = await interceptor(processedUrl, processedOptions);
|
|
processedUrl = result.url || processedUrl;
|
|
processedOptions = result.options || processedOptions;
|
|
}
|
|
|
|
return { url: processedUrl, options: processedOptions };
|
|
}
|
|
|
|
// Process response through interceptors
|
|
async processResponse(response, url) {
|
|
let processedResponse = response;
|
|
|
|
for (const interceptor of this.responseInterceptors) {
|
|
processedResponse = await interceptor(processedResponse, url);
|
|
}
|
|
|
|
return processedResponse;
|
|
}
|
|
|
|
// Generic request method
|
|
async request(url, options = {}) {
|
|
try {
|
|
// Process request through interceptors
|
|
const processed = await this.processRequest(url, options);
|
|
|
|
// Determine the correct base URL (real backend vs mock)
|
|
let finalUrl = processed.url;
|
|
if (processed.url.startsWith(API_CONFIG.BASE_URL)) {
|
|
const baseUrl = await backendDetector.getBaseUrl();
|
|
finalUrl = processed.url.replace(API_CONFIG.BASE_URL, baseUrl);
|
|
}
|
|
|
|
// Make the request
|
|
const response = await fetch(finalUrl, {
|
|
...processed.options,
|
|
headers: this.getHeaders(processed.options.headers)
|
|
});
|
|
|
|
// Process response through interceptors
|
|
const processedResponse = await this.processResponse(response, url);
|
|
|
|
// Handle errors
|
|
if (!processedResponse.ok) {
|
|
const error = await processedResponse.json().catch(() => ({
|
|
message: `HTTP ${processedResponse.status}: ${processedResponse.statusText}`
|
|
}));
|
|
throw new Error(error.message || error.detail || 'Request failed');
|
|
}
|
|
|
|
// Parse JSON response
|
|
const data = await processedResponse.json().catch(() => null);
|
|
return data;
|
|
|
|
} catch (error) {
|
|
// Only log if not a connection refusal (expected when DensePose API is down)
|
|
if (error.message && !error.message.includes('Failed to fetch')) {
|
|
console.error('API Request Error:', error);
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// GET request
|
|
async get(endpoint, params = {}, options = {}) {
|
|
const url = buildApiUrl(endpoint, params);
|
|
return this.request(url, {
|
|
method: 'GET',
|
|
...options
|
|
});
|
|
}
|
|
|
|
// POST request
|
|
async post(endpoint, data = {}, options = {}) {
|
|
const url = buildApiUrl(endpoint);
|
|
return this.request(url, {
|
|
method: 'POST',
|
|
body: JSON.stringify(data),
|
|
...options
|
|
});
|
|
}
|
|
|
|
// PUT request
|
|
async put(endpoint, data = {}, options = {}) {
|
|
const url = buildApiUrl(endpoint);
|
|
return this.request(url, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(data),
|
|
...options
|
|
});
|
|
}
|
|
|
|
// DELETE request
|
|
async delete(endpoint, options = {}) {
|
|
const url = buildApiUrl(endpoint);
|
|
return this.request(url, {
|
|
method: 'DELETE',
|
|
...options
|
|
});
|
|
}
|
|
}
|
|
|
|
// Create singleton instance
|
|
export const apiService = new ApiService(); |