/_health endpoint now checks the database and redis connections

This commit is contained in:
Tom Moor
2023-03-18 12:37:34 -04:00
parent dafc4fb609
commit 68640860fb
2 changed files with 18 additions and 2 deletions

View File

@@ -90,8 +90,6 @@ async function start(id: number, disconnect: () => void) {
// Apply default rate limit to all routes
app.use(defaultRateLimiter());
// install health check endpoint for all services
router.get("/_health", (ctx) => (ctx.body = "OK"));
app.use(router.routes());
// loop through requested services at startup

View File

@@ -6,9 +6,11 @@ import send from "koa-send";
import userAgent, { UserAgentContext } from "koa-useragent";
import { languages } from "@shared/i18n";
import { IntegrationType } from "@shared/types";
import { sequelize } from "@server/database/sequelize";
import env from "@server/env";
import { NotFoundError } from "@server/errors";
import { Integration } from "@server/models";
import RedisAdapter from "@server/redis";
import { opensearchResponse } from "@server/utils/opensearch";
import { getTeamFromContext } from "@server/utils/passport";
import { robotsResponse } from "@server/utils/robots";
@@ -116,6 +118,22 @@ router.get("/opensearch.xml", (ctx) => {
ctx.body = opensearchResponse(ctx.request.URL.origin);
});
router.get("/_health", async (ctx) => {
try {
await sequelize.query("SELECT 1");
} catch (err) {
throw new Error("Database connection failed");
}
try {
await RedisAdapter.defaultClient.ping();
} catch (err) {
throw new Error("Redis ping failed");
}
ctx.body = "OK";
});
router.get("/s/:shareId", renderShare);
router.get("/s/:shareId/doc/:documentSlug", renderShare);
router.get("/s/:shareId/*", renderShare);