Skip to content

Commit

Permalink
add health endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
shunjizhan committed Nov 28, 2023
1 parent 6a4aab3 commit 1da2e43
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/__tests__/misc.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { expectError, noRoute, testTimeout, version } from './testUtils';
import { expectError, health, noRoute, testTimeout, version } from './testUtils';

describe.concurrent('/miscellaneous', () => {
describe('when no route for the request', () => {
Expand Down Expand Up @@ -32,4 +32,14 @@ describe.concurrent('/miscellaneous', () => {
expect(endTime - startTime).to.be.gte(1000);
});
});

describe('/health', () => {
it('works', async () => {
const res = await health({});

expect(Number(res.data.relayerBalAcala)).to.be.gt(0);
expect(Number(res.data.relayerBalKarura)).to.be.gt(0);
expect(res.data.isHealthy).to.be.true;
});
});
});
4 changes: 4 additions & 0 deletions src/__tests__/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ export const testTimeout = process.env.COVERAGE
? _supertestPost(RELAYER_API.TEST_TIMEOUT)
: _axiosPost(RELAYER_URL.TEST_TIMEOUT);

export const health = process.env.COVERAGE
? _supertestGet(RELAYER_API.HEALTH)
: _axiosGet(RELAYER_URL.HEALTH);

export const shouldRouteHoma = process.env.COVERAGE
? _supertestGet(RELAYER_API.GET_HOMA_ROUTER_ADDR)
: _axiosGet(RELAYER_URL.GET_HOMA_ROUTER_ADDR);
Expand Down
2 changes: 2 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export const RELAYER_API = {
NO_ROUTE: '/noRoute',
VERSION: '/version',
TEST_TIMEOUT: '/testTimeout',
HEALTH: '/health',
} as const;

export const RELAYER_URL = {
Expand All @@ -79,6 +80,7 @@ export const RELAYER_URL = {
NO_ROUTE: `${RELAYER_BASE_URL}${RELAYER_API.NO_ROUTE}`,
VERSION: `${RELAYER_BASE_URL}${RELAYER_API.VERSION}`,
TEST_TIMEOUT: `${RELAYER_BASE_URL}${RELAYER_API.TEST_TIMEOUT}`,
HEALTH: `${RELAYER_BASE_URL}${RELAYER_API.HEALTH}`,
} as const;

/* ---------------
Expand Down
13 changes: 9 additions & 4 deletions src/middlewares/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { NextFunction, Request, Response } from 'express';
import { Schema } from 'yup';

import { NoRouteError } from './error';
import { healthCheck } from '../api/health';
import { logger } from '../utils';
import {
shouldRouteHoma,
relayAndRoute,
relayAndRouteBatch,
routeHoma,
routeWormhole,
routeXcm,
shouldRouteHoma,
shouldRouteWormhole,
shouldRouteXcm,
} from '../api/route';
import { logger } from '../utils';
import {
relayAndRouteSchema,
routeHomaSchema,
Expand All @@ -21,7 +22,7 @@ import {
} from '../utils/validate';

interface RouterConfig {
schema: Schema;
schema?: Schema;
handler: (data: any) => Promise<any>;
}

Expand All @@ -43,6 +44,10 @@ const ROUTER_CONFIGS: {
schema: routeHomaSchema,
handler: shouldRouteHoma,
},
'/health': {
schema: undefined,
handler: healthCheck,
},
},

POST: {
Expand Down Expand Up @@ -83,7 +88,7 @@ const handleRoute = async (req: Request, res: Response) => {
throw new NoRouteError(`${reqPath} not supported`);
}

await config.schema.validate(args, { abortEarly: false });
await config.schema?.validate(args, { abortEarly: false });
const data = await config.handler(args);

logger.info({ data }, `✨ ${reqPath}`);
Expand Down

0 comments on commit 1da2e43

Please sign in to comment.