From 2251ddc251e4070b87d191c48b33f6a20e5c484c Mon Sep 17 00:00:00 2001 From: Ajay Date: Tue, 16 Apr 2024 01:13:56 -0400 Subject: [PATCH] Add ready endpoint --- src/app.ts | 3 +++ src/config.ts | 1 + src/routes/getReady.ts | 13 +++++++++++++ src/types/config.model.ts | 1 + 4 files changed, 18 insertions(+) create mode 100644 src/routes/getReady.ts diff --git a/src/app.ts b/src/app.ts index c6af3a4..a8f38a3 100644 --- a/src/app.ts +++ b/src/app.ts @@ -56,6 +56,7 @@ import { hostHeader } from "./middleware/hostHeader"; import { getBrandingStats } from "./routes/getBrandingStats"; import { getTopBrandingUsers } from "./routes/getTopBrandingUsers"; import { getFeatureFlag } from "./routes/getFeatureFlag"; +import { getReady } from "./routes/getReady"; export function createServer(callback: () => void): Server { // Create a service (the app object is just a callback). @@ -205,6 +206,8 @@ function setupRoutes(router: Router, server: Server) { router.get("/api/status/:value", (req, res) => getStatus(req, res, server)); router.get("/api/status", (req, res) => getStatus(req, res, server)); + router.get("/api/ready", (req, res) => getReady(req, res, server)); + router.get("/api/youtubeApiProxy", youtubeApiProxy); // get user category stats router.get("/api/userStats", getUserStats); diff --git a/src/config.ts b/src/config.ts index ea03170..3752323 100644 --- a/src/config.ts +++ b/src/config.ts @@ -193,6 +193,7 @@ addDefaults(config, { minUserIDLength: 30, deArrowPaywall: false, useCacheForSegmentGroups: false, + maxConnections: 100 }); loadFromEnv(config); migrate(config); diff --git a/src/routes/getReady.ts b/src/routes/getReady.ts new file mode 100644 index 0000000..4320db4 --- /dev/null +++ b/src/routes/getReady.ts @@ -0,0 +1,13 @@ +import { Request, Response } from "express"; +import { Server } from "http"; +import { config } from "../config"; + +export async function getReady(req: Request, res: Response, server: Server): Promise { + const connections = await new Promise((resolve) => server.getConnections((_, count) => resolve(count))) as number; + + if (!connections || connections < config.maxConnections) { + return res.sendStatus(200); + } else { + return res.sendStatus(500); + } +} diff --git a/src/types/config.model.ts b/src/types/config.model.ts index 18c98d6..041c6fa 100644 --- a/src/types/config.model.ts +++ b/src/types/config.model.ts @@ -106,6 +106,7 @@ export interface SBSConfig { minUserIDLength: number, deArrowPaywall: boolean, useCacheForSegmentGroups: boolean + maxConnections: number; } export interface WebhookConfig {