Files
signalk-plugin/plugin/tools/healthcheck.js
2026-03-11 15:25:03 +01:00

51 lines
1.4 KiB
JavaScript

const SERVICES = {
api: 'https://api.mebboat.it/health',
storage: 'https://storage.mebboat.it/health',
auth: 'https://auth.mebboat.it/health',
realtime: 'https://realtime.mebboat.it/health',
};
/**
* Checks the health of a single service.
* @param {string} url - Health endpoint URL
* @returns {Promise<{ok: boolean, status: string}>}
*/
async function checkService(url) {
try {
const response = await fetch(url, {
headers: { Accept: "application/json" }
});
if (!response.ok) {
return { ok: false, status: `HTTP ${response.status}` };
}
const data = await response.json().catch(() => null);
const isOk = data?.status === "ok";
return { ok: isOk, status: isOk ? 'online' : 'offline' };
} catch (err) {
return { ok: false, status: `error: ${err.message}` };
}
}
/**
* Checks all MEB cloud services in parallel.
* @returns {Promise<Object>} Map of service name -> health result
*/
async function checkAllServices() {
const entries = Object.entries(SERVICES);
const results = await Promise.all(
entries.map(async ([name, url]) => {
const result = await checkService(url);
return [name, result];
})
);
return Object.fromEntries(results);
}
module.exports = {
SERVICES,
checkService,
checkAllServices
};