Skip to content

Commit 8fdd42c

Browse files
committed
feat: Add server health check at '/api/health'
1 parent 4a3a2e4 commit 8fdd42c

File tree

8 files changed

+92
-0
lines changed

8 files changed

+92
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
import { healthCheck } from '@app/core/resolvers/healthCheck'
3+
4+
/* --- Types ----------------------------------------------------------------------------------- */
5+
6+
type RequestContext<T = Record<string, unknown>> = {
7+
params: T
8+
}
9+
10+
/* --- Handlers -------------------------------------------------------------------------------- */
11+
12+
export const handler = async (req: NextRequest, context: RequestContext) => {
13+
// Input
14+
const searchParams = req.nextUrl.searchParams
15+
const echo = searchParams.get('echo') || null
16+
17+
// Run
18+
const serverHealth = await healthCheck({ echo, req })
19+
20+
// Respond
21+
return NextResponse.json(serverHealth)
22+
}
23+
24+
/* --- Methods --------------------------------------------------------------------------------- */
25+
26+
export const GET = handler
27+
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import type { NextApiRequest } from 'next'
2+
import * as OS from 'os'
3+
4+
/* --- Constants ------------------------------------------------------------------------------- */
5+
6+
const ALIVE_SINCE = new Date()
7+
8+
/* --- Types ----------------------------------------------------------------------------------- */
9+
10+
type HealthCheckArgs = {
11+
echo: string
12+
req: Request | NextApiRequest
13+
}
14+
15+
/** --- healthCheck() -------------------------------------------------------------------------- */
16+
/** -i- Check the health status of the server. Includes relevant urls, server time(zone), versions and more */
17+
export const healthCheck = async (args: HealthCheckArgs) => {
18+
// Input
19+
const { echo, req } = args
20+
21+
// Vars
22+
const now = new Date()
23+
const aliveTime = now.getTime() - ALIVE_SINCE.getTime()
24+
25+
// Urls
26+
const r = req as Request
27+
const rn = req as NextApiRequest
28+
const reqHost = rn?.headers?.host
29+
const reqProtocol = rn?.headers?.['x-forwarded-proto'] ?? 'http'
30+
const requestURL = r?.url ?? `${reqProtocol}://${reqHost}/api/health`
31+
const baseURL = process.env.BACKEND_URL || requestURL?.split('/api/')[0]
32+
const apiURL = baseURL ? `${baseURL}/api` : null
33+
34+
// -- Respond --
35+
36+
return {
37+
// PARAMS
38+
echo,
39+
// STATUS
40+
status: 'OK',
41+
alive: true,
42+
kicking: true,
43+
// TIME & DATES
44+
now: now.toISOString(),
45+
aliveTime,
46+
aliveSince: ALIVE_SINCE.toISOString(),
47+
serverTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
48+
// URLS
49+
requestURL,
50+
baseURL,
51+
apiURL,
52+
port: process.env.PORT ? Number(process.env.PORT) : null,
53+
debugPort: process.debugPort && Number(process.debugPort),
54+
// VERSIONS
55+
nodeVersion: process.versions.node,
56+
v8Version: process.versions.v8,
57+
// SYSTEM
58+
systemArch: OS.arch(),
59+
systemPlatform: OS.platform(),
60+
systemRelease: OS.release(),
61+
systemFreeMemory: OS.freemem(),
62+
systemTotalMemory: OS.totalmem(),
63+
systemLoadAverage: OS.loadavg(),
64+
}
65+
}

0 commit comments

Comments
 (0)