54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { InjectDataSource } from '@nestjs/typeorm';
|
|
import { DataSource } from 'typeorm';
|
|
|
|
export interface SystemHealthResponse {
|
|
status: 'ok' | 'error';
|
|
timestamp: string;
|
|
environment: string;
|
|
database: {
|
|
host: string;
|
|
port: number;
|
|
database: string;
|
|
user: string;
|
|
status: 'connected' | 'disconnected';
|
|
error?: string;
|
|
};
|
|
}
|
|
|
|
@Injectable()
|
|
export class SystemService {
|
|
constructor(
|
|
private configService: ConfigService,
|
|
@InjectDataSource() private dataSource: DataSource,
|
|
) {}
|
|
|
|
async getHealth(): Promise<SystemHealthResponse> {
|
|
const dbHealth = await this.checkDatabase();
|
|
|
|
return {
|
|
status: dbHealth.status === 'connected' ? 'ok' : 'error',
|
|
timestamp: new Date().toISOString(),
|
|
environment: this.configService.get('NODE_ENV') || 'development',
|
|
database: dbHealth,
|
|
};
|
|
}
|
|
|
|
private async checkDatabase() {
|
|
const config = {
|
|
host: this.configService.get('POSTGRES_HOST') || 'unknown',
|
|
port: parseInt(this.configService.get('POSTGRES_PORT')) || 5432,
|
|
database: this.configService.get('POSTGRES_DB') || 'unknown',
|
|
user: this.configService.get('POSTGRES_USER') || 'unknown',
|
|
};
|
|
|
|
try {
|
|
await this.dataSource.query('SELECT 1');
|
|
return { ...config, status: 'connected' as const };
|
|
} catch (error) {
|
|
return { ...config, status: 'disconnected' as const, error: error.message };
|
|
}
|
|
}
|
|
}
|