@apolitical/server
Version:
Node.js module to encapsulate Apolitical's express server setup
40 lines (36 loc) • 1.3 kB
JavaScript
;
module.exports = ({ config, healthService }) => {
const { HEALTH, LIVENESS, READINESS } = config.ENDPOINTS.PROBES;
const { API, UI } = config.SERVER.PROBES_OPTIONS;
function buildPrefix({ prefix }) {
let result = '';
if (prefix) {
if (API.ALLOWED_PREFIXES.some((allowedPrefix) => prefix.includes(allowedPrefix))) {
result = `${API.PREFIX_PATH}/${prefix}`;
} else if (UI.ALLOWED_PREFIXES.some((allowedPrefix) => prefix.includes(allowedPrefix))) {
result = `${UI.PREFIX_PATH}/${prefix}`;
} else {
result = prefix;
}
}
return result;
}
return function load(app, { probes }) {
if (probes) {
const { readinessCheck, livenessCheck } = probes;
// Overwrite checks when defined
if (readinessCheck) {
healthService.registerReadiness(readinessCheck);
}
if (livenessCheck) {
healthService.registerLiveness(livenessCheck);
}
// Build base URL
const probesPath = buildPrefix(probes);
// Load probes endpoints
app.get(`${probesPath}${HEALTH}`, healthService.healthEndpoint());
app.get(`${probesPath}${READINESS}`, healthService.readinessEndpoint());
app.get(`${probesPath}${LIVENESS}`, healthService.livenessEndpoint());
}
};
};