feat(health): /api/health/services (grouped+counts) + owner /check
Adds GET /api/health/services returning registry services grouped by category with merged cached status and per-group healthy counts, and POST /api/health/check (owner-only) that enqueues a health.check pg-boss job. Registers the health_check worker in the jobs index.
This commit is contained in:
@@ -27,6 +27,7 @@ import { router as dashboardRouter } from './routes/dashboard.js';
|
|||||||
import { router as weatherRouter } from './routes/weather.js';
|
import { router as weatherRouter } from './routes/weather.js';
|
||||||
import { router as hostRouter } from './routes/host.js';
|
import { router as hostRouter } from './routes/host.js';
|
||||||
import { router as speedtestRouter } from './routes/speedtest.js';
|
import { router as speedtestRouter } from './routes/speedtest.js';
|
||||||
|
import { router as healthRouter } from './routes/health.js';
|
||||||
|
|
||||||
export function mountApi(app) {
|
export function mountApi(app) {
|
||||||
const api = Router();
|
const api = Router();
|
||||||
@@ -61,6 +62,7 @@ export function mountApi(app) {
|
|||||||
api.use('/weather', weatherRouter);
|
api.use('/weather', weatherRouter);
|
||||||
api.use('/host', hostRouter);
|
api.use('/host', hostRouter);
|
||||||
api.use('/speedtest', speedtestRouter);
|
api.use('/speedtest', speedtestRouter);
|
||||||
|
api.use('/health', healthRouter);
|
||||||
api.use('/:entity_type/:entity_id/tags', tagsByEntityRouter);
|
api.use('/:entity_type/:entity_id/tags', tagsByEntityRouter);
|
||||||
|
|
||||||
api.use((_req, _res, next) => next(new NotFoundError('route not found')));
|
api.use((_req, _res, next) => next(new NotFoundError('route not found')));
|
||||||
|
|||||||
30
lib/api/routes/health.js
Normal file
30
lib/api/routes/health.js
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { Router } from 'express';
|
||||||
|
import { asyncWrap } from '../errors.js';
|
||||||
|
import { requireOwner } from '../cap.js';
|
||||||
|
import { load, grouped, iconSlug } from '../../health/registry.js';
|
||||||
|
import * as statusRepo from '../../db/repos/service_status.js';
|
||||||
|
import { enqueue } from '../../jobs/queue.js';
|
||||||
|
|
||||||
|
export const router = Router();
|
||||||
|
|
||||||
|
router.get('/services', asyncWrap(async (_req, res) => {
|
||||||
|
const statuses = Object.fromEntries((await statusRepo.all()).map(s => [s.service_id, s]));
|
||||||
|
const groups = grouped(load()).map(g => {
|
||||||
|
const services = g.services.map(s => {
|
||||||
|
const st = statuses[s.id];
|
||||||
|
return {
|
||||||
|
id: s.id, name: s.name, host: s.host, url: s.url, icon: iconSlug(s),
|
||||||
|
status: st?.status || 'unknown', latency_ms: st?.latency_ms ?? null,
|
||||||
|
detail: st?.detail || null, checked_at: st?.checked_at || null
|
||||||
|
};
|
||||||
|
});
|
||||||
|
return { category: g.category, healthy: services.filter(s => s.status === 'ok').length,
|
||||||
|
total: services.length, services };
|
||||||
|
});
|
||||||
|
res.json(groups);
|
||||||
|
}));
|
||||||
|
|
||||||
|
router.post('/check', requireOwner, asyncWrap(async (_req, res) => {
|
||||||
|
const id = await enqueue('health.check', {});
|
||||||
|
res.status(202).json({ enqueued: id });
|
||||||
|
}));
|
||||||
@@ -5,8 +5,9 @@ import * as blob from './workers/blob.js';
|
|||||||
import * as embed from './workers/embed.js';
|
import * as embed from './workers/embed.js';
|
||||||
import * as karakeep from './workers/karakeep.js';
|
import * as karakeep from './workers/karakeep.js';
|
||||||
import * as speedtest from './workers/speedtest.js';
|
import * as speedtest from './workers/speedtest.js';
|
||||||
|
import * as healthCheck from './workers/health_check.js';
|
||||||
|
|
||||||
const WORKERS = [echo, url, blob, embed, karakeep, speedtest];
|
const WORKERS = [echo, url, blob, embed, karakeep, speedtest, healthCheck];
|
||||||
|
|
||||||
export async function registerWorkers() {
|
export async function registerWorkers() {
|
||||||
for (const w of WORKERS) {
|
for (const w of WORKERS) {
|
||||||
|
|||||||
8
lib/jobs/workers/health_check.js
Normal file
8
lib/jobs/workers/health_check.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { load } from '../../health/registry.js';
|
||||||
|
import { checkAll } from '../../health/checker.js';
|
||||||
|
import * as statusRepo from '../../db/repos/service_status.js';
|
||||||
|
export const NAME = 'health.check';
|
||||||
|
export async function handler(_job) {
|
||||||
|
const results = await checkAll(load());
|
||||||
|
for (const r of results) await statusRepo.upsert(r);
|
||||||
|
}
|
||||||
22
tests/api/health.test.js
Normal file
22
tests/api/health.test.js
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { describe, it, expect, beforeAll } from 'vitest';
|
||||||
|
import request from 'supertest';
|
||||||
|
import { setup } from './helpers.js';
|
||||||
|
import * as statusRepo from '../../lib/db/repos/service_status.js';
|
||||||
|
|
||||||
|
let app, ownerHeaders;
|
||||||
|
beforeAll(async () => {
|
||||||
|
({ app, ownerHeaders } = await setup());
|
||||||
|
await statusRepo.upsert({ service_id: 'gitea', status: 'ok', latency_ms: 10, detail: '200' });
|
||||||
|
});
|
||||||
|
describe('health api', () => {
|
||||||
|
it('401 without auth', async () => expect((await request(app).get('/api/health/services')).status).toBe(401));
|
||||||
|
it('returns groups with counts + merged cached status', async () => {
|
||||||
|
const res = await request(app).get('/api/health/services').set(ownerHeaders);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
const infra = res.body.find(g => g.category === 'infrastructure');
|
||||||
|
expect(infra).toBeTruthy();
|
||||||
|
expect(infra.healthy).toBeGreaterThanOrEqual(1); // gitea ok
|
||||||
|
const gitea = infra.services.find(s => s.id === 'gitea');
|
||||||
|
expect(gitea.status).toBe('ok');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user