Skip to content

Commit

Permalink
Add ready endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ajayyy committed Apr 16, 2024
1 parent 07d4dde commit 2251ddc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ addDefaults(config, {
minUserIDLength: 30,
deArrowPaywall: false,
useCacheForSegmentGroups: false,
maxConnections: 100
});
loadFromEnv(config);
migrate(config);
Expand Down
13 changes: 13 additions & 0 deletions src/routes/getReady.ts
Original file line number Diff line number Diff line change
@@ -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<Response> {
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);
}
}
1 change: 1 addition & 0 deletions src/types/config.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export interface SBSConfig {
minUserIDLength: number,
deArrowPaywall: boolean,
useCacheForSegmentGroups: boolean
maxConnections: number;
}

export interface WebhookConfig {
Expand Down

0 comments on commit 2251ddc

Please sign in to comment.