UNPKG

@hirosystems/api-toolkit

Version:
46 lines 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildFastifyApiServer = buildFastifyApiServer; exports.buildPrometheusServer = buildPrometheusServer; const cors_1 = require("@fastify/cors"); const fastify_1 = require("fastify"); const fastify_metrics_1 = require("fastify-metrics"); const logger_1 = require("../logger"); const values_1 = require("../helpers/values"); /** * Creates a Fastify server that handles Prometheus metrics and CORS headers automatically. * @returns Fastify instance */ async function buildFastifyApiServer() { const fastify = (0, fastify_1.default)({ trustProxy: true, logger: logger_1.PINO_LOGGER_CONFIG, }).withTypeProvider(); if (values_1.isProdEnv) { await fastify.register(fastify_metrics_1.default, { endpoint: null }); } await fastify.register(cors_1.default); return fastify; } /** * Creates a Fastify server that serves a `/metrics` endpoint with metrics taken from * `FastifyMetrics`. * @param args - Fastify instance metrics decorator * @returns Fastify instance */ async function buildPrometheusServer(args) { const promServer = (0, fastify_1.default)({ trustProxy: true, logger: logger_1.PINO_LOGGER_CONFIG, }); promServer.route({ url: '/metrics', method: 'GET', logLevel: 'info', handler: async (_, reply) => { await reply.type('text/plain').send(await args.metrics.client.register.metrics()); }, }); return promServer; } //# sourceMappingURL=fastify.js.map