UNPKG

@apolitical/server

Version:

Node.js module to encapsulate Apolitical's express server setup

53 lines (42 loc) 1.58 kB
'use strict'; module.exports = ({ health, lru, config }) => { const { CACHE_OPTIONS } = config.SERVER; // Setup health checker const healthCheck = new health.HealthChecker(); // Setup cache const cache = new lru(CACHE_OPTIONS); function buildCheckWithCache(key, check) { cache.clear(); return async function () { const checked = cache.get(key); if (!checked) { await check(); cache.set(key, true); } }; } // function registerStartup(startup) { // healthCheck.registerStartupCheck(new health.StartupCheck("Startup Check", startup)); // } function healthEndpoint() { return health.HealthEndpoint(healthCheck); } function registerReadiness(readiness) { const readinessWithCache = buildCheckWithCache('readiness', readiness); healthCheck.registerReadinessCheck(new health.ReadinessCheck('Readiness Check', readinessWithCache)); } function readinessEndpoint() { return health.ReadinessEndpoint(healthCheck); } function registerLiveness(liveness) { const livenessWithCache = buildCheckWithCache('liveness', liveness); healthCheck.registerLivenessCheck(new health.LivenessCheck('Liveness Check', livenessWithCache)); } function livenessEndpoint() { return health.LivenessEndpoint(healthCheck); } function registerShutdown(shutdown) { healthCheck.registerShutdownCheck(new health.ShutdownCheck('Shutdown Check', shutdown)); } return { healthEndpoint, readinessEndpoint, registerReadiness, registerLiveness, livenessEndpoint, registerShutdown }; };