Skip to content

Commit

Permalink
feat(api): Implement healthcheck HTTP API endpoint.
Browse files Browse the repository at this point in the history
Fixes #838.
  • Loading branch information
make-github-pseudonymous-again committed Apr 3, 2024
1 parent 63cd2ee commit 1fde951
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
19 changes: 19 additions & 0 deletions server/api/healthcheck/index.app-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// eslint-disable-next-line import/no-unassigned-import
import 'regenerator-runtime/runtime.js';
import {assert} from 'chai';
import request from 'supertest';

import {server} from '../../../imports/_test/fixtures';

import absoluteURL from '../../../imports/app/absoluteURL';

const app = absoluteURL('api/healthcheck');

server(__filename, () => {
it('works', async () => {
const response = await request(app).get(`/`);

assert.equal(response.status, 200);
assert.equal(response.text, 'OK');
});
});
25 changes: 25 additions & 0 deletions server/api/healthcheck/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import process from 'process';

import express from 'express';

import isProduction from '../../../imports/app/isProduction';
import isReady from '../../../imports/app/isReady';

import {pipe} from '../rateLimit';

import rateLimiter from './rateLimiter';

const routes = express();

routes.set('trust proxy', process.env.HTTP_FORWARDED_COUNT);

if (isProduction()) {
routes.use(pipe(rateLimiter));
}

routes.get(`/`, async (_req, res, _next) => {
await isReady();
res.status(200).send('OK');
});

export default routes;
11 changes: 11 additions & 0 deletions server/api/healthcheck/rateLimiter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {RateLimiterMemory} from 'rate-limiter-flexible';

const rateLimiter = new RateLimiterMemory({
// NOTE key prefix is not necessary since each RateLimiterMemory is backed
// by its own storage.
keyPrefix: '',
points: 15, // NOTE 6 points
duration: 60, // NOTE Per minute
});

export default rateLimiter;
4 changes: 3 additions & 1 deletion server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import scheduleAllMigrations from '../imports/migrations/scheduleAll';
import './publication/_register/enabled';

import ics from './api/ics/index';
import healthcheck from './api/healthcheck/index';

// DECLARE ALL ENABLED ICS ENDPOINTS
// DECLARE ALL ENABLED HTTP ENDPOINTS
WebApp.connectHandlers.use('/api/ics', ics);
WebApp.connectHandlers.use('/api/healthcheck', healthcheck);

if (isTest()) {
// @ts-expect-error Missing from type definitions.
Expand Down

0 comments on commit 1fde951

Please sign in to comment.