-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add server health check at '/api/health'
- Loading branch information
Showing
8 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { NextRequest, NextResponse } from 'next/server' | ||
import { healthCheck } from '@app/core/resolvers/healthCheck' | ||
|
||
/* --- Types ----------------------------------------------------------------------------------- */ | ||
|
||
type RequestContext<T = Record<string, unknown>> = { | ||
params: T | ||
} | ||
|
||
/* --- Handlers -------------------------------------------------------------------------------- */ | ||
|
||
export const handler = async (req: NextRequest, context: RequestContext) => { | ||
// Input | ||
const searchParams = req.nextUrl.searchParams | ||
const echo = searchParams.get('echo') || null | ||
|
||
// Run | ||
const serverHealth = await healthCheck({ echo, req }) | ||
|
||
// Respond | ||
return NextResponse.json(serverHealth) | ||
} | ||
|
||
/* --- Methods --------------------------------------------------------------------------------- */ | ||
|
||
export const GET = handler | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { NextApiRequest } from 'next' | ||
import * as OS from 'os' | ||
|
||
/* --- Constants ------------------------------------------------------------------------------- */ | ||
|
||
const ALIVE_SINCE = new Date() | ||
|
||
/* --- Types ----------------------------------------------------------------------------------- */ | ||
|
||
type HealthCheckArgs = { | ||
echo: string | ||
req: Request | NextApiRequest | ||
} | ||
|
||
/** --- healthCheck() -------------------------------------------------------------------------- */ | ||
/** -i- Check the health status of the server. Includes relevant urls, server time(zone), versions and more */ | ||
export const healthCheck = async (args: HealthCheckArgs) => { | ||
// Input | ||
const { echo, req } = args | ||
|
||
// Vars | ||
const now = new Date() | ||
const aliveTime = now.getTime() - ALIVE_SINCE.getTime() | ||
|
||
// Urls | ||
const r = req as Request | ||
const rn = req as NextApiRequest | ||
const reqHost = rn?.headers?.host | ||
const reqProtocol = rn?.headers?.['x-forwarded-proto'] ?? 'http' | ||
const requestURL = r?.url ?? `${reqProtocol}://${reqHost}/api/health` | ||
const baseURL = process.env.BACKEND_URL || requestURL?.split('/api/')[0] | ||
const apiURL = baseURL ? `${baseURL}/api` : null | ||
|
||
// -- Respond -- | ||
|
||
return { | ||
// PARAMS | ||
echo, | ||
// STATUS | ||
status: 'OK', | ||
alive: true, | ||
kicking: true, | ||
// TIME & DATES | ||
now: now.toISOString(), | ||
aliveTime, | ||
aliveSince: ALIVE_SINCE.toISOString(), | ||
serverTimezone: Intl.DateTimeFormat().resolvedOptions().timeZone, | ||
// URLS | ||
requestURL, | ||
baseURL, | ||
apiURL, | ||
port: process.env.PORT ? Number(process.env.PORT) : null, | ||
debugPort: process.debugPort && Number(process.debugPort), | ||
// VERSIONS | ||
nodeVersion: process.versions.node, | ||
v8Version: process.versions.v8, | ||
// SYSTEM | ||
systemArch: OS.arch(), | ||
systemPlatform: OS.platform(), | ||
systemRelease: OS.release(), | ||
systemFreeMemory: OS.freemem(), | ||
systemTotalMemory: OS.totalmem(), | ||
systemLoadAverage: OS.loadavg(), | ||
} | ||
} |