From 2e69173d1378bec8296ce556499f71990ee92375 Mon Sep 17 00:00:00 2001 From: Jonas Scholz Date: Mon, 18 Nov 2024 01:27:07 +0100 Subject: [PATCH] feat: basic health-checks --- packages/api/src/app.ts | 2 ++ packages/api/src/controllers/Health.ts | 10 +++++++++ packages/dashboard/src/pages/api/health.ts | 24 ++++++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 packages/api/src/controllers/Health.ts create mode 100644 packages/dashboard/src/pages/api/health.ts diff --git a/packages/api/src/app.ts b/packages/api/src/app.ts index e28e86e..8377e74 100644 --- a/packages/api/src/app.ts +++ b/packages/api/src/app.ts @@ -22,6 +22,7 @@ import { Webhooks } from "./controllers/Webhooks"; import { V1 } from "./controllers/v1"; import { prisma } from "./database/prisma"; import { HttpException } from "./exceptions"; +import { Health } from "./controllers/Health"; const server = new (class extends Server { public constructor() { @@ -69,6 +70,7 @@ const server = new (class extends Server { new Identities(), new Tasks(), new V1(), + new Health(), ]); this.app.use("*", () => { diff --git a/packages/api/src/controllers/Health.ts b/packages/api/src/controllers/Health.ts new file mode 100644 index 0000000..fd420d8 --- /dev/null +++ b/packages/api/src/controllers/Health.ts @@ -0,0 +1,10 @@ +import { Controller, Get } from "@overnightjs/core"; +import type { Request, Response } from "express"; + +@Controller("health") +export class Health { + @Get("") + public async health(req: Request, res: Response) { + return res.json({ success: true }); + } +} diff --git a/packages/dashboard/src/pages/api/health.ts b/packages/dashboard/src/pages/api/health.ts new file mode 100644 index 0000000..ca297e3 --- /dev/null +++ b/packages/dashboard/src/pages/api/health.ts @@ -0,0 +1,24 @@ +import type { NextApiRequest, NextApiResponse } from 'next' + +import { network } from './../../lib/network' + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse, +) { + try { + const timeoutPromise = new Promise((_, reject) => { + setTimeout(() => { + reject(new Error('Timeout')); + }, 2000); + }); + + const healthPromise = network.fetch("GET", "/health"); + + await Promise.race([healthPromise, timeoutPromise]); + + return res.status(200).json({ message: 'OK' }); + } catch (error) { + return res.status(500).json({ message: 'Internal Server Error' }); + } +}