adityaverma977 commited on
Commit
0d6660e
·
1 Parent(s): a64703a

Fix frontend backend URL handling and track frontend lib folder

Browse files
Files changed (2) hide show
  1. .gitignore +1 -1
  2. frontend/lib/api.ts +65 -0
.gitignore CHANGED
@@ -14,7 +14,7 @@ dist/
14
  downloads/
15
  eggs/
16
  .eggs/
17
- lib/
18
  lib64/
19
  parts/
20
  sdist/
 
14
  downloads/
15
  eggs/
16
  .eggs/
17
+ /lib/
18
  lib64/
19
  parts/
20
  sdist/
frontend/lib/api.ts ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const BACKEND_URL = (process.env.NEXT_PUBLIC_BACKEND_URL || "http://localhost:8000").replace(/\/$/, "")
2
+
3
+ function apiUrl(path: string) {
4
+ return `${BACKEND_URL}${path}`
5
+ }
6
+
7
+ export async function wakeBackend() {
8
+ const response = await fetch(apiUrl("/wake"))
9
+ return response.json()
10
+ }
11
+
12
+ export async function getAvailableModels() {
13
+ const response = await fetch(apiUrl("/available-models"))
14
+ return response.json()
15
+ }
16
+
17
+ export async function startSimulation(modelNames: string[]) {
18
+ const response = await fetch(apiUrl("/start-simulation"), {
19
+ method: "POST",
20
+ headers: { "Content-Type": "application/json" },
21
+ body: JSON.stringify({
22
+ model_names: modelNames,
23
+ scenario: "fire",
24
+ map_width: 1200,
25
+ map_height: 800,
26
+ }),
27
+ })
28
+ if (!response.ok) throw new Error("Failed to start simulation")
29
+ return response.json()
30
+ }
31
+
32
+ export async function placeVolcano(simulationId: string, x: number, y: number) {
33
+ const response = await fetch(apiUrl("/place-fire"), {
34
+ method: "POST",
35
+ headers: { "Content-Type": "application/json" },
36
+ body: JSON.stringify({ simulation_id: simulationId, x, y }),
37
+ })
38
+ if (!response.ok) throw new Error("Failed to place fire")
39
+ return response.json()
40
+ }
41
+
42
+ export function createSimulationSocket(
43
+ simulationId: string,
44
+ onMessage: (msg: any) => void,
45
+ onError: () => void
46
+ ) {
47
+ const wsBase = BACKEND_URL.replace(/^http:/, "ws:").replace(/^https:/, "wss:")
48
+ const wsUrl = `${wsBase}/ws/${simulationId}`
49
+
50
+ const ws = new WebSocket(wsUrl)
51
+
52
+ ws.onmessage = (event) => {
53
+ try {
54
+ const msg = JSON.parse(event.data)
55
+ onMessage(msg)
56
+ } catch (e) {
57
+ console.error("Failed to parse message:", e)
58
+ }
59
+ }
60
+
61
+ ws.onerror = () => onError()
62
+ ws.onclose = () => onError()
63
+
64
+ return ws
65
+ }