Skip to content

Commit

Permalink
feat: Add server health check at '/api/health'
Browse files Browse the repository at this point in the history
  • Loading branch information
codinsonn committed Apr 11, 2024
1 parent 4a3a2e4 commit 8fdd42c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions apps/next/app/(main)/api/health/route.ts
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.
65 changes: 65 additions & 0 deletions features/app-core/resolvers/healthCheck.ts
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(),
}
}

0 comments on commit 8fdd42c

Please sign in to comment.