forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth.controller.ts
More file actions
56 lines (48 loc) 路 1.48 KB
/
Copy pathhealth.controller.ts
File metadata and controls
56 lines (48 loc) 路 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Controller, Get } from '@nestjs/common';
import { InjectDataSource } from '@nestjs/typeorm';
import { DataSource } from 'typeorm';
interface ServiceStatus {
status: 'ok' | 'error';
message?: string;
}
interface HealthResponse {
status: 'ok' | 'degraded';
timestamp: string;
services: {
database: ServiceStatus;
postgis: ServiceStatus;
};
}
@Controller({ path: 'health', version: '1' })
export class HealthController {
constructor(@InjectDataSource() private readonly dataSource: DataSource) {}
@Get()
async check(): Promise<HealthResponse> {
const database = await this.checkDatabase();
const postgis = await this.checkPostGIS();
const allOk = database.status === 'ok' && postgis.status === 'ok';
return {
status: allOk ? 'ok' : 'degraded',
timestamp: new Date().toISOString(),
services: { database, postgis },
};
}
private async checkDatabase(): Promise<ServiceStatus> {
try {
await this.dataSource.query('SELECT 1');
return { status: 'ok' };
} catch (err) {
return { status: 'error', message: (err as Error).message };
}
}
private async checkPostGIS(): Promise<ServiceStatus> {
try {
const result = await this.dataSource.query<{ version: string }[]>(
`SELECT postgis_lib_version() AS version`,
);
return { status: 'ok', message: `PostGIS ${result[0].version}` };
} catch (err) {
return { status: 'error', message: (err as Error).message };
}
}
}