fix: use headscale health route for healthz

This commit is contained in:
Aarnav Tale
2025-01-10 10:46:50 +05:30
parent e33504016b
commit dfd03e77bb
+20 -14
View File
@@ -1,26 +1,32 @@
import { loadContext } from '~/utils/config/headplane'; import { loadContext } from '~/utils/config/headplane';
import { HeadscaleError, pull } from '~/utils/headscale'; import { HeadscaleError, pull } from '~/utils/headscale';
import { data } from 'react-router';
import log from '~/utils/log'; import log from '~/utils/log';
export async function loader() { export async function loader() {
const context = await loadContext(); const context = await loadContext();
const prefix = context.headscaleUrl;
const health = new URL('health', prefix);
log.debug('APIC', 'GET %s', health.toString());
let healthy = false
try { try {
// Doesn't matter, we just need a 401 const res = await fetch(health.toString(), {
await pull('v1/', 'wrongkey'); headers: {
} catch (e) { 'Accept': 'application/json',
if (!(e instanceof HeadscaleError)) { },
log.debug('Healthz', 'Headscale is not reachable'); });
return data(
{ if (res.status === 200) {
status: 'NOT OK', healthy = true;
error: e.message,
},
{ status: 500 },
);
} }
} catch (e) {
log.debug('APIC', 'GET %s failed with error %s', health.toString(), e);
} }
return data({ status: 'OK' }); return new Response(JSON.stringify({ status: healthy ? 'OK' : 'ERROR' }), {
status: healthy ? 200 : 500,
headers: {
'Content-Type': 'application/json',
},
});
} }