Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

status.ts

32 lines1.1 KB

1import { createFileRoute } from '@tanstack/react-router'
2import { env } from 'cloudflare:workers'
3​
4import { getDb } from '#/db/index.ts'
5import { halGet } from '#/server/hal.ts'
6import { getServiceStatus } from '#/server/status.ts'
7import { parseWaitSeconds, waitForStatusChange } from '#/server/wait.ts'
8​
9export const Route = createFileRoute('/v1/status')({
10 server: {
11 handlers: {
12 GET: async ({ request }) => {
13 // ?wait=Ns long-poll (task 11.3): hold until a poll/sync lands.
14 const waitSeconds = parseWaitSeconds(new URL(request.url))
15 // A long-poll is waiting for a write that just landed, so it stays
16 // on the primary. A plain read can use a replica.
17 const db = getDb(env, { replica: waitSeconds === 0 })
18 if (waitSeconds > 0) {
19 await waitForStatusChange(db, waitSeconds)
20 }
21 return Response.json({
22 ...(await getServiceStatus(db)),
23 _links: {
24 self: halGet('/v1/status'),
25 index: halGet('/v1'),
26 providers: halGet('/v1/providers'),
27 },
28 })
29 },
30 },
31 },
32})