|
| 1 | +import { FastifyInstance } from 'fastify' |
| 2 | +import { join } from 'path' |
| 3 | +import { hostname } from 'os' |
| 4 | +import { Adapter } from '../adapter' |
| 5 | +import { buildSettingsList } from '../util/settings' |
| 6 | + |
| 7 | +export interface StatusResponse { |
| 8 | + adapter: { |
| 9 | + name: string |
| 10 | + version: string |
| 11 | + uptimeSeconds: number |
| 12 | + } |
| 13 | + endpoints: { |
| 14 | + name: string |
| 15 | + aliases: string[] |
| 16 | + transports: string[] |
| 17 | + }[] |
| 18 | + defaultEndpoint: string |
| 19 | + configuration: { |
| 20 | + name: string |
| 21 | + value: unknown |
| 22 | + type: string |
| 23 | + description: string |
| 24 | + required: boolean |
| 25 | + default: unknown |
| 26 | + customSetting: boolean |
| 27 | + envDefaultOverride: unknown |
| 28 | + }[] |
| 29 | + runtime: { |
| 30 | + nodeVersion: string |
| 31 | + platform: string |
| 32 | + architecture: string |
| 33 | + hostname: string |
| 34 | + } |
| 35 | + metrics: { |
| 36 | + enabled: boolean |
| 37 | + port?: number |
| 38 | + endpoint?: string |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * This function registers the status endpoint for the adapter. |
| 44 | + * This endpoint provides comprehensive information about the adapter including: |
| 45 | + * - Adapter metadata (name, version, commit) |
| 46 | + * - Configuration (obfuscated sensitive values) |
| 47 | + * - Runtime information |
| 48 | + * - Dependencies status |
| 49 | + * - Endpoints and transports |
| 50 | + * |
| 51 | + * @param app - the fastify instance that has been created |
| 52 | + * @param adapter - the adapter for which to create the status endpoint |
| 53 | + */ |
| 54 | +export default function registerStatusEndpoint(app: FastifyInstance, adapter: Adapter) { |
| 55 | + // Status endpoint that returns comprehensive adapter information |
| 56 | + app.get(join(adapter.config.settings.BASE_URL, '/status'), async () => { |
| 57 | + const metricsEndpoint = adapter.config.settings.METRICS_USE_BASE_URL |
| 58 | + ? join(adapter.config.settings.BASE_URL, 'metrics') |
| 59 | + : '/metrics' |
| 60 | + |
| 61 | + const statusResponse: StatusResponse = { |
| 62 | + adapter: { |
| 63 | + name: adapter.name, |
| 64 | + version: process.env['npm_package_version'] || 'unknown', |
| 65 | + uptimeSeconds: process.uptime(), |
| 66 | + }, |
| 67 | + endpoints: adapter.endpoints.map((endpoint) => ({ |
| 68 | + name: endpoint.name, |
| 69 | + aliases: endpoint.aliases || [], |
| 70 | + transports: endpoint.transportRoutes.routeNames(), |
| 71 | + })), |
| 72 | + defaultEndpoint: adapter.defaultEndpoint || '', |
| 73 | + configuration: buildSettingsList(adapter), |
| 74 | + runtime: { |
| 75 | + nodeVersion: process.version, |
| 76 | + platform: process.platform, |
| 77 | + architecture: process.arch, |
| 78 | + hostname: hostname(), |
| 79 | + }, |
| 80 | + metrics: { |
| 81 | + enabled: adapter.config.settings.METRICS_ENABLED, |
| 82 | + port: adapter.config.settings.METRICS_ENABLED |
| 83 | + ? adapter.config.settings.METRICS_PORT |
| 84 | + : undefined, |
| 85 | + endpoint: adapter.config.settings.METRICS_ENABLED ? metricsEndpoint : undefined, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + return statusResponse |
| 90 | + }) |
| 91 | +} |
0 commit comments