Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(routes): add health check route to server (#68)

Corentin THOMASSET1ff8902bd0bf
diff --git a/apps/papra-server/src/modules/app/health-check/e2e/health-check.e2e.test.ts b/apps/papra-server/src/modules/app/health-check/e2e/health-check.e2e.test.ts
new file mode 100644
index 00000000..2c68ea1f
--- /dev/null
+++ b/apps/papra-server/src/modules/app/health-check/e2e/health-check.e2e.test.ts
@@ -0,0 +1,45 @@
+import type { Database } from '../../database/database.types';
+import { describe, expect, test } from 'vitest';
+import { createInMemoryDatabase } from '../../database/database.test-utils';
+import { createServer } from '../../server';
+
+describe('health check routes e2e', () => {
+ describe('health check', () => {
+ describe('the /api/health is a publicly accessible route that provides health information about the server', () => {
+ test('when the database is healthy, the /api/health returns 200', async () => {
+ const { db } = await createInMemoryDatabase();
+ const { app } = createServer({ db });
+
+ const response = await app.request('/api/health');
+
+ expect(response.status).to.eql(200);
+ expect(await response.json()).to.eql({
+ isDatabaseHealthy: true,
+ isEverythingOk: true,
+ status: 'ok',
+ });
+ });
+
+ test('when their is an issue with the database, the /api/health returns a 500', async () => {
+ const db = {
+ run: async () => {
+ throw new Error('Alerte générale !');
+ },
+ } as unknown as Database;
+
+ const { app } = createServer({ db });
+
+ const response = await app.request('/api/health');
+
+ expect(response.status).to.eql(500);
+ expect(
+ await response.json(),
+ ).to.eql({
+ isDatabaseHealthy: false,
+ isEverythingOk: false,
+ status: 'error',
+ });
+ });
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/app/health-check/e2e/ping.e2e.test.ts b/apps/papra-server/src/modules/app/health-check/e2e/ping.e2e.test.ts
new file mode 100644
index 00000000..1b9ceb79
--- /dev/null
+++ b/apps/papra-server/src/modules/app/health-check/e2e/ping.e2e.test.ts
@@ -0,0 +1,17 @@
+import { describe, expect, test } from 'vitest';
+import { createInMemoryDatabase } from '../../database/database.test-utils';
+import { createServer } from '../../server';
+
+describe('ping routes e2e', () => {
+ test('the /api/ping is a publicly accessible route that always returns a 200 with a status ok', async () => {
+ const { db } = await createInMemoryDatabase();
+ const { app } = createServer({ db });
+
+ const response = await app.request('/api/ping');
+
+ expect(response.status).to.eql(200);
+ expect(await response.json()).to.eql({
+ status: 'ok',
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/app/health-check/health-check.repository.test.ts b/apps/papra-server/src/modules/app/health-check/health-check.repository.test.ts
new file mode 100644
index 00000000..45dadf20
--- /dev/null
+++ b/apps/papra-server/src/modules/app/health-check/health-check.repository.test.ts
@@ -0,0 +1,21 @@
+import type { Database } from '../database/database.types';
+import { describe, expect, test } from 'vitest';
+import { createInMemoryDatabase } from '../database/database.test-utils';
+import { isDatabaseHealthy } from './health-check.repository';
+
+const faultyDatabase = {
+ run: async () => {
+ throw new Error('Alerte générale !');
+ },
+} as unknown as Database;
+
+describe('health-check repository', () => {
+ describe('isDatabaseHealthy', () => {
+ test('when the database is healthy, it returns true', async () => {
+ const { db } = await createInMemoryDatabase();
+
+ expect(await isDatabaseHealthy({ db })).toBe(true);
+ expect(await isDatabaseHealthy({ db: faultyDatabase })).toBe(false);
+ });
+ });
+});
diff --git a/apps/papra-server/src/modules/app/health-check/health-check.repository.ts b/apps/papra-server/src/modules/app/health-check/health-check.repository.ts
new file mode 100644
index 00000000..d804d467
--- /dev/null
+++ b/apps/papra-server/src/modules/app/health-check/health-check.repository.ts
@@ -0,0 +1,9 @@
+import type { Database } from '../database/database.types';
+import { safely } from '@corentinth/chisels';
+import { sql } from 'drizzle-orm';
+
+export async function isDatabaseHealthy({ db }: { db: Database }) {
+ const [result, error] = await safely(db.run(sql`SELECT 1;`));
+
+ return error === null && result.rows.length > 0 && result.rows[0]['1'] === 1;
+}
diff --git a/apps/papra-server/src/modules/app/health-check/health-check.routes.ts b/apps/papra-server/src/modules/app/health-check/health-check.routes.ts
new file mode 100644
index 00000000..fbf8d72a
--- /dev/null
+++ b/apps/papra-server/src/modules/app/health-check/health-check.routes.ts
@@ -0,0 +1,33 @@
+import type { ServerInstance } from '../server.types';
+import { getDb } from '../database/database.models';
+import { isDatabaseHealthy } from './health-check.repository';
+
+export function registerHealthCheckRoutes({ app }: { app: ServerInstance }) {
+ setupPingRoute({ app });
+ setupHealthCheckRoute({ app });
+}
+
+function setupPingRoute({ app }: { app: ServerInstance }) {
+ app.get('/api/ping', context => context.json({ status: 'ok' }));
+}
+
+function setupHealthCheckRoute({ app }: { app: ServerInstance }) {
+ app.get('/api/health', async (context) => {
+ const { db } = getDb({ context });
+
+ const isHealthy = await isDatabaseHealthy({ db });
+
+ const isEverythingOk = isHealthy;
+ const status = isEverythingOk ? 'ok' : 'error';
+ const statusCode = isEverythingOk ? 200 : 500;
+
+ return context.json(
+ {
+ isDatabaseHealthy: isHealthy,
+ isEverythingOk,
+ status,
+ },
+ statusCode,
+ );
+ });
+}
diff --git a/apps/papra-server/src/modules/app/server.routes.ts b/apps/papra-server/src/modules/app/server.routes.ts
index 2625e79b..928c8d38 100644
--- a/apps/papra-server/src/modules/app/server.routes.ts
+++ b/apps/papra-server/src/modules/app/server.routes.ts
@@ -5,6 +5,7 @@ import { registerUsersPrivateRoutes } from '../users/users.routes';
import { registerAuthPrivateRoutes, registerAuthPublicRoutes } from './auth/auth.routes';
import { jwtValidationMiddleware } from './auth/middlewares/auth.middleware';
import { impersonationMiddleware } from './auth/middlewares/impersonation.middleware';
+import { registerHealthCheckRoutes } from './health-check/health-check.routes';
export function registerRoutes({ app }: { app: ServerInstance }) {
registerPublicRoutes({ app });
@@ -12,8 +13,7 @@ export function registerRoutes({ app }: { app: ServerInstance }) {
}
function registerPublicRoutes({ app }: { app: ServerInstance }) {
- app.get('/api/ping', context => context.json({ status: 'ok' }));
-
+ registerHealthCheckRoutes({ app });
registerAuthPublicRoutes({ app });
}