38 lines
967 B
Plaintext
38 lines
967 B
Plaintext
export const dynamic = 'force-dynamic'
|
|
|
|
export async function GET() {
|
|
const encoder = new TextEncoder()
|
|
|
|
const stream = new ReadableStream({
|
|
async start(controller) {
|
|
const send = (data: object) => {
|
|
controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`))
|
|
}
|
|
|
|
// Poll FastAPI stats every 3s and push to browser
|
|
while (true) {
|
|
try {
|
|
const res = await fetch('http://localhost:8000/api/stats', {
|
|
signal: AbortSignal.timeout(2000),
|
|
})
|
|
if (res.ok) {
|
|
const stats = await res.json()
|
|
send({ type: 'stats', payload: stats })
|
|
}
|
|
} catch {
|
|
send({ type: 'offline' })
|
|
}
|
|
await new Promise((r) => setTimeout(r, 3000))
|
|
}
|
|
},
|
|
})
|
|
|
|
return new Response(stream, {
|
|
headers: {
|
|
'Content-Type': 'text/event-stream',
|
|
'Cache-Control': 'no-cache',
|
|
Connection: 'keep-alive',
|
|
},
|
|
})
|
|
}
|