Skip to content

Commit

Permalink
feat(anni): add health check
Browse files Browse the repository at this point in the history
  • Loading branch information
tamslo committed Jul 10, 2024
1 parent 9b6e392 commit 07a5b61
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
15 changes: 15 additions & 0 deletions anni/src/pages/api/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NextApiHandler } from 'next';

import { getAppDataOrThrowUnavailableError } from './v1/data';
import { handleApiMethods } from '../../common/api-helpers';

const api: NextApiHandler = async (req, res) =>
await handleApiMethods(req, res, {
GET: async () => {
// Test if getting app data works, otherwise error is thrown
await getAppDataOrThrowUnavailableError();
return { successStatus: 200 };
},
});

export default api;
16 changes: 12 additions & 4 deletions anni/src/pages/api/v1/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ import { ApiError } from 'next/dist/server/api-utils';

import { handleApiMethods } from '../../../common/api-helpers';
import dbConnect from '../../../database/helpers/connect';
import AppData from '../../../database/models/AppData';
import AppData, { IAppData_DB } from '../../../database/models/AppData';
import { IVersionedDoc } from '../../../database/versioning/schema';

export async function getAppDataOrThrowUnavailableError(): Promise<
IVersionedDoc<IAppData_DB>
> {
await dbConnect();
const data = await AppData!.getCurrent();
if (!data) throw new ApiError(503, 'Data not available');
return data;
}

const api: NextApiHandler = async (req, res) =>
await handleApiMethods(req, res, {
GET: async () => {
await dbConnect();
const data = await AppData!.getCurrent();
if (!data) throw new ApiError(503, 'Data not available');
const data = await getAppDataOrThrowUnavailableError();
return { successStatus: 200, data };
},
});
Expand Down

0 comments on commit 07a5b61

Please sign in to comment.