forked from owid/owid-grapher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublicApiRouter.ts
24 lines (21 loc) · 869 Bytes
/
publicApiRouter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { FunctionalRouter } from "./FunctionalRouter.js"
import { Request, Response } from "./authentication.js"
import * as db from "../db/db.js"
export const publicApiRouter = new FunctionalRouter()
function rejectAfterDelay(ms: number) {
return new Promise((resolve, reject) => setTimeout(reject, ms))
}
publicApiRouter.router.get("/health", async (req: Request, res: Response) => {
try {
const sqlPromise = db.knexRaw(
db.knexInstance() as db.KnexReadonlyTransaction,
`SELECT id FROM charts LIMIT 1`
)
const timeoutPromise = rejectAfterDelay(1500) // Wait 1.5 seconds at most
await Promise.race([sqlPromise, timeoutPromise])
res.status(200).end("OK")
} catch (e) {
res.status(500).end("Error querying the database")
console.error("Error at health endpoint", e)
}
})