Poll-only live monitor, drop the SSE path
Browse files at this commit7963aad5ee8de3397162448ef85b8fd7edca9f97Parent: 1edb4f3- feat
- poll-only live monitor, drop the SSE path
6 changed files +8 -131
- nginx.conf +0 -36
- nginx.conf.test.ts +0 -33
- src/api/__tests__/exams/exam-live.test.ts +0 -11
- src/api/exams/getExamLiveStreamUrl.ts +0 -3
- src/api/exams/index.ts +0 -1
- src/pages/live-monitor-page.tsx +8 -47
nginx.confdeleted+0 -36
| @@ -1,36 +0,0 @@ | |||
| 1 | -server { | ||
| 2 | - listen 5173; | ||
| 3 | - root /usr/share/nginx/html; | ||
| 4 | - index index.html; | ||
| 5 | - | ||
| 6 | - # Proxy the whole /api/* namespace, not a hand-maintained allowlist: an | ||
| 7 | - # enumerated list silently 404s (falls through to the SPA -> HTML -> the | ||
| 8 | - # client's JSON.parse fails) every time the backend or frontend gains a | ||
| 9 | - # resource the list forgot. homework, messages, and questions were all | ||
| 10 | - # already broken this way. | ||
| 11 | - location /api/ { | ||
| 12 | - rewrite ^/api(/.*)$ $1 break; | ||
| 13 | - proxy_pass http://hezarfen-backend:8080; | ||
| 14 | - proxy_http_version 1.1; | ||
| 15 | - proxy_set_header Host $http_host; | ||
| 16 | - proxy_set_header X-Real-IP $remote_addr; | ||
| 17 | - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
| 18 | - proxy_set_header X-Forwarded-Proto $scheme; | ||
| 19 | - proxy_set_header Upgrade $http_upgrade; | ||
| 20 | - proxy_set_header Connection "upgrade"; | ||
| 21 | - } | ||
| 22 | - | ||
| 23 | - location /assets/ { | ||
| 24 | - try_files $uri =404; | ||
| 25 | - add_header Cache-Control "public, max-age=31536000, immutable" always; | ||
| 26 | - } | ||
| 27 | - | ||
| 28 | - location = /index.html { | ||
| 29 | - add_header Cache-Control "no-store" always; | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - location / { | ||
| 33 | - try_files $uri /index.html; | ||
| 34 | - add_header Cache-Control "no-store" always; | ||
| 35 | - } | ||
| 36 | -} | ||
nginx.conf.test.tsdeleted+0 -33
| @@ -1,33 +0,0 @@ | |||
| 1 | -import { expect, test } from "bun:test"; | ||
| 2 | -import { readFileSync } from "node:fs"; | ||
| 3 | - | ||
| 4 | -// Mirror of backend route mounts (hezarfen_backend src/lib.rs .nest calls + /health, /time). | ||
| 5 | -// Backend adds a prefix → add it here AND in nginx.conf's proxy location. | ||
| 6 | -const BACKEND_PREFIXES = [ | ||
| 7 | - "auth", | ||
| 8 | - "users", | ||
| 9 | - "notes", | ||
| 10 | - "events", | ||
| 11 | - "courses", | ||
| 12 | - "sessions", | ||
| 13 | - "exams", | ||
| 14 | - "marks", | ||
| 15 | - "work", | ||
| 16 | - "pomodoro", | ||
| 17 | - "attendance", | ||
| 18 | - "settings", | ||
| 19 | - "subjects", | ||
| 20 | - "terms", | ||
| 21 | - "health", | ||
| 22 | - "time", | ||
| 23 | -]; | ||
| 24 | - | ||
| 25 | -test("nginx proxy whitelist covers every backend route prefix", () => { | ||
| 26 | - const conf = readFileSync(new URL("./nginx.conf", import.meta.url), "utf8"); | ||
| 27 | - const match = conf.match(/\^\/api\/\(([^)]+)\)/); | ||
| 28 | - expect(match).not.toBeNull(); | ||
| 29 | - const listed = match![1].split("|"); | ||
| 30 | - for (const prefix of BACKEND_PREFIXES) { | ||
| 31 | - expect(listed).toContain(prefix); | ||
| 32 | - } | ||
| 33 | -}); | ||
src/api/__tests__/exams/exam-live.test.tsmodified+0 -11
| @@ -1,6 +1,5 @@ | |||
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; | 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { getExamLive } from "../../exams"; | 2 | import { getExamLive } from "../../exams"; |
| 3 | -import { getExamLiveStreamUrl } from "../../exams"; | ||
| 4 | import { lastFetchCall, mockFetchSuccess } from "../helpers/mock-fetch"; | 3 | import { lastFetchCall, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 5 | 4 | ||
| 6 | describe("exams API - live", () => { | 5 | describe("exams API - live", () => { |
| @@ -19,14 +18,4 @@ describe("exams API - live", () => { | |||
| 19 | expect(url).toBe("/api/exams/ex1/live"); | 18 | expect(url).toBe("/api/exams/ex1/live"); |
| 20 | expect(init?.method).toBe("GET"); | 19 | expect(init?.method).toBe("GET"); |
| 21 | }); | 20 | }); |
| 22 | - | ||
| 23 | - it("getExamLiveStreamUrl returns correct URL", () => { | ||
| 24 | - const url = getExamLiveStreamUrl("ex1"); | ||
| 25 | - expect(url).toBe("/api/exams/ex1/live/stream"); | ||
| 26 | - }); | ||
| 27 | - | ||
| 28 | - it("getExamLiveStreamUrl encodes examId correctly", () => { | ||
| 29 | - const url = getExamLiveStreamUrl("ex1/abc?xyz"); | ||
| 30 | - expect(url).toBe("/api/exams/ex1%2Fabc%3Fxyz/live/stream"); | ||
| 31 | - }); | ||
| 32 | }); | 21 | }); |
src/api/exams/getExamLiveStreamUrl.tsdeleted+0 -3
| @@ -1,3 +0,0 @@ | |||
| 1 | -export function getExamLiveStreamUrl(examId: string): string { | ||
| 2 | - return `/api/exams/${encodeURIComponent(examId)}/live/stream`; | ||
| 3 | -} | ||
src/api/exams/index.tsmodified+0 -1
| @@ -18,7 +18,6 @@ export * from "./deleteExamResultByUserId"; | |||
| 18 | export * from "./getExamStatistics"; | 18 | export * from "./getExamStatistics"; |
| 19 | export * from "./getStudentAnswers"; | 19 | export * from "./getStudentAnswers"; |
| 20 | export * from "./getExamLive"; | 20 | export * from "./getExamLive"; |
| 21 | -export * from "./getExamLiveStreamUrl"; | ||
| 22 | export * from "./getExamAttempt"; | 21 | export * from "./getExamAttempt"; |
| 23 | export * from "./postExamAttempt"; | 22 | export * from "./postExamAttempt"; |
| 24 | export * from "./postExamAttemptFinish"; | 23 | export * from "./postExamAttemptFinish"; |
src/pages/live-monitor-page.tsxmodified+8 -47
| @@ -2,7 +2,6 @@ import { Show, Suspense, createEffect, createMemo, createResource, createSignal, | |||
| 2 | import { Link, useLocation, useParams } from "@tanstack/solid-router"; | 2 | import { Link, useLocation, useParams } from "@tanstack/solid-router"; |
| 3 | import type { ColumnDef } from "@tanstack/solid-table"; | 3 | import type { ColumnDef } from "@tanstack/solid-table"; |
| 4 | import { getExamLive } from "@/api/exams"; | 4 | import { getExamLive } from "@/api/exams"; |
| 5 | -import { getExamLiveStreamUrl } from "@/api/exams"; | ||
| 6 | import { getExamById } from "@/api/exams"; | 5 | import { getExamById } from "@/api/exams"; |
| 7 | import { formatApiError } from "@/api/client"; | 6 | import { formatApiError } from "@/api/client"; |
| 8 | import type { LiveMonitor, LiveRosterEntry } from "@/api/client"; | 7 | import type { LiveMonitor, LiveRosterEntry } from "@/api/client"; |
| @@ -142,15 +141,6 @@ function LiveMonitorContent() { | |||
| 142 | } | 141 | } |
| 143 | }; | 142 | }; |
| 144 | 143 | ||
| 145 | - const applyStreamEvent = (event: MessageEvent) => { | ||
| 146 | - try { | ||
| 147 | - setSnapshot(JSON.parse(event.data) as LiveMonitor); | ||
| 148 | - setError(""); | ||
| 149 | - } catch (err) { | ||
| 150 | - console.error("[live-monitor] stream parse error:", err); | ||
| 151 | - } | ||
| 152 | - }; | ||
| 153 | - | ||
| 154 | createEffect(() => { | 144 | createEffect(() => { |
| 155 | const eid = id(); | 145 | const eid = id(); |
| 156 | const e = exam(); | 146 | const e = exam(); |
| @@ -158,45 +148,16 @@ function LiveMonitorContent() { | |||
| 158 | void fetchSnapshot(); | 148 | void fetchSnapshot(); |
| 159 | if (e.ends_at != null && e.ends_at < untrack(now)) return; | 149 | if (e.ends_at != null && e.ends_at < untrack(now)) return; |
| 160 | 150 | ||
| 161 | - let interval: ReturnType<typeof setInterval> | null = null; | 151 | + const interval = setInterval(() => { |
| 162 | - const startPolling = () => { | 152 | + if (e.ends_at != null && e.ends_at < untrack(now)) { |
| 163 | - if (interval) return; | ||
| 164 | - interval = setInterval(() => { | ||
| 165 | - if (e.ends_at != null && e.ends_at < untrack(now)) { | ||
| 166 | - if (interval) clearInterval(interval); | ||
| 167 | - interval = null; | ||
| 168 | - void fetchSnapshot(); | ||
| 169 | - return; | ||
| 170 | - } | ||
| 171 | - void fetchSnapshot(); | ||
| 172 | - }, 2000); | ||
| 173 | - }; | ||
| 174 | - | ||
| 175 | - // ponytail: keep polling even with SSE; some live proxies accept EventSource | ||
| 176 | - // but never flush events. 2s GET is cheaper than stale teacher screen. | ||
| 177 | - startPolling(); | ||
| 178 | - | ||
| 179 | - if (!("EventSource" in window)) { | ||
| 180 | - onCleanup(() => { | ||
| 181 | - if (interval) clearInterval(interval); | ||
| 182 | - }); | ||
| 183 | - return; | ||
| 184 | - } | ||
| 185 | - | ||
| 186 | - const source = new EventSource(getExamLiveStreamUrl(eid), { withCredentials: true }); | ||
| 187 | - source.addEventListener("snapshot", applyStreamEvent); | ||
| 188 | - source.onmessage = applyStreamEvent; | ||
| 189 | - source.onerror = () => { | ||
| 190 | - source.close(); | ||
| 191 | - startPolling(); | ||
| 192 | - }; | ||
| 193 | - | ||
| 194 | - onCleanup(() => { | ||
| 195 | - source.close(); | ||
| 196 | - if (interval) { | ||
| 197 | clearInterval(interval); | 153 | clearInterval(interval); |
| 154 | + void fetchSnapshot(); | ||
| 155 | + return; | ||
| 198 | } | 156 | } |
| 199 | - }); | 157 | + void fetchSnapshot(); |
| 158 | + }, 2000); | ||
| 159 | + | ||
| 160 | + onCleanup(() => clearInterval(interval)); | ||
| 200 | }); | 161 | }); |
| 201 | 162 | ||
| 202 | const liveRows = createMemo<LiveRosterRow[]>(() => { | 163 | const liveRows = createMemo<LiveRosterRow[]>(() => { |