Skip to content

Commit

Permalink
Add test and update timeout for endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hansaskov committed Dec 30, 2024
1 parent ad183bc commit bbfe603
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
14 changes: 14 additions & 0 deletions services/backend/src/health/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { beforeAll, describe, expect, it } from "bun:test";
import { treaty } from "@elysiajs/eden";
import Elysia from "elysia";
import { healthApi } from "./api";

describe("health", () => {
const app = new Elysia().use(healthApi);
const api = treaty(app);

it("should return 200 for /health", async () => {
const response = await api.health.get();
expect(response.status).toBe(200);
});
});
17 changes: 16 additions & 1 deletion services/backend/src/health/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@ import { healthQueries } from "./query";

export const healthApi = new Elysia().get("/health", async () => {
try {
const start = Date.now();

// Check database connection health
await healthQueries.health();

return { status: "ok" };
const duration = (Date.now() - start) / 1000;
const isLong = duration > 1.0;

if (isLong) {
return error("Gateway Timeout", {
status: "timed out",
duration,
});
}

return {
status: "ok",
duration,
};
} catch (e) {
// Log error for monitoring
console.error("Health check failed:", e);
Expand Down
12 changes: 6 additions & 6 deletions services/backend/src/health/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { Table } from "$db/collections";
import { db } from "$db/postgres";

export const healthQueries = {
health: async () =>
db
.select()
.from(Table.organizations)
.limit(1)
.then((v) => v.length >= 0),
health: async () =>
db
.select()
.from(Table.organizations)
.limit(1)
.then((v) => v.length >= 0),
};

0 comments on commit bbfe603

Please sign in to comment.