Spaces:
Sleeping
Sleeping
adityaverma977 commited on
Commit ·
0d6660e
1
Parent(s): a64703a
Fix frontend backend URL handling and track frontend lib folder
Browse filesFiles changed (2)
- .gitignore+1-1
- frontend/lib/api.tsAdded+65
13 unmodified lines1415161718192013 unmodified linesdownloads/eggs/.eggs/lib/lib64/parts/sdist/13 unmodified lines1415161718192013 unmodified linesdownloads/eggs/.eggs//lib/lib64/parts/sdist/
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465const BACKEND_URL = (process.env.NEXT_PUBLIC_BACKEND_URL || "http://localhost:8000").replace(/\/$/, "")
function apiUrl(path: string) { return `${BACKEND_URL}${path}`}
export async function wakeBackend() { const response = await fetch(apiUrl("/wake")) return response.json()}
export async function getAvailableModels() { const response = await fetch(apiUrl("/available-models")) return response.json()}
export async function startSimulation(modelNames: string[]) { const response = await fetch(apiUrl("/start-simulation"), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ model_names: modelNames, scenario: "fire", map_width: 1200, map_height: 800, }), }) if (!response.ok) throw new Error("Failed to start simulation") return response.json()}
export async function placeVolcano(simulationId: string, x: number, y: number) { const response = await fetch(apiUrl("/place-fire"), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ simulation_id: simulationId, x, y }), }) if (!response.ok) throw new Error("Failed to place fire") return response.json()}
export function createSimulationSocket( simulationId: string, onMessage: (msg: any) => void, onError: () => void) { const wsBase = BACKEND_URL.replace(/^http:/, "ws:").replace(/^https:/, "wss:") const wsUrl = `${wsBase}/ws/${simulationId}` const ws = new WebSocket(wsUrl) ws.onmessage = (event) => { try { const msg = JSON.parse(event.data) onMessage(msg) } catch (e) { console.error("Failed to parse message:", e) } } ws.onerror = () => onError() ws.onclose = () => onError() return ws}.gitignore+1-1
frontend/lib/api.tsAdded+65