Implement health check service and global exception handling
This commit is contained in:
@@ -1,36 +1,41 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Controller, Get, HttpCode, Res } from '@nestjs/common';
|
||||
import { Response } from 'express';
|
||||
import { HealthService } from './health.service';
|
||||
|
||||
@Controller('health')
|
||||
export class HealthController {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(private readonly healthService: HealthService) {}
|
||||
|
||||
/**
|
||||
* Övergripande hälsostatus för tjänsten
|
||||
* Returnerar 200 om allt är bra, 503 om något är fel
|
||||
*/
|
||||
@Get()
|
||||
getHealth() {
|
||||
return {
|
||||
status: 'ok',
|
||||
service: 'recipe-api',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
async getHealth(@Res() res: Response) {
|
||||
const health = await this.healthService.getOverallHealth();
|
||||
res.status(health.statusCode).json({
|
||||
status: health.status,
|
||||
service: health.service,
|
||||
timestamp: health.timestamp,
|
||||
uptime: health.uptime,
|
||||
checks: health.checks,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Databasspecific hälsokontroll
|
||||
* Returnerar 200 om databasen är tillgänglig, 503 om inte
|
||||
*/
|
||||
@Get('db')
|
||||
async getDatabaseHealth() {
|
||||
try {
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
|
||||
return {
|
||||
status: 'ok',
|
||||
database: 'connected',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 'error',
|
||||
database: 'not reachable',
|
||||
message: error instanceof Error ? error.message : 'unknown error',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
async getDatabaseHealth(@Res() res: Response) {
|
||||
const dbHealth = await this.healthService.getDatabaseHealth();
|
||||
res.status(dbHealth.statusCode).json({
|
||||
status: dbHealth.status,
|
||||
database: dbHealth.database,
|
||||
responseTime: `${dbHealth.responseTime}ms`,
|
||||
timestamp: dbHealth.timestamp,
|
||||
details: dbHealth.details,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HealthController } from './health.controller';
|
||||
import { HealthService } from './health.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [HealthController],
|
||||
providers: [HealthService],
|
||||
})
|
||||
export class HealthModule {}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
|
||||
export interface HealthStatus {
|
||||
status: 'healthy' | 'degraded' | 'unhealthy';
|
||||
service: string;
|
||||
timestamp: string;
|
||||
uptime: number;
|
||||
checks: {
|
||||
database: {
|
||||
status: 'ok' | 'error';
|
||||
responseTime: number;
|
||||
details?: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class HealthService {
|
||||
private readonly startTime = Date.now();
|
||||
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async getOverallHealth(): Promise<{
|
||||
status: 'healthy' | 'degraded' | 'unhealthy';
|
||||
statusCode: number;
|
||||
service: string;
|
||||
timestamp: string;
|
||||
uptime: number;
|
||||
checks: {
|
||||
database: {
|
||||
status: 'ok' | 'error';
|
||||
responseTime: number;
|
||||
details?: string;
|
||||
};
|
||||
};
|
||||
}> {
|
||||
const timestamp = new Date().toISOString();
|
||||
const uptime = Date.now() - this.startTime;
|
||||
|
||||
// Testa databaskopplingen
|
||||
const dbStart = Date.now();
|
||||
let dbStatus: 'ok' | 'error' = 'ok';
|
||||
let dbResponseTime = 0;
|
||||
let dbDetails: string | undefined;
|
||||
|
||||
try {
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
dbResponseTime = Date.now() - dbStart;
|
||||
} catch (error) {
|
||||
dbStatus = 'error';
|
||||
dbResponseTime = Date.now() - dbStart;
|
||||
dbDetails = error instanceof Error ? error.message : 'Unknown database error';
|
||||
}
|
||||
|
||||
// Bestäm övergripande hälsa
|
||||
let overallStatus: 'healthy' | 'degraded' | 'unhealthy' = 'healthy';
|
||||
if (dbStatus === 'error') {
|
||||
overallStatus = 'unhealthy';
|
||||
}
|
||||
|
||||
const statusCode = overallStatus === 'unhealthy' ? 503 : 200;
|
||||
|
||||
return {
|
||||
status: overallStatus,
|
||||
statusCode,
|
||||
service: 'recipe-api',
|
||||
timestamp,
|
||||
uptime,
|
||||
checks: {
|
||||
database: {
|
||||
status: dbStatus,
|
||||
responseTime: dbResponseTime,
|
||||
details: dbDetails,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async getDatabaseHealth(): Promise<{
|
||||
status: 'ok' | 'error';
|
||||
database: string;
|
||||
responseTime: number;
|
||||
timestamp: string;
|
||||
details?: string;
|
||||
statusCode: number;
|
||||
}> {
|
||||
const timestamp = new Date().toISOString();
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
const responseTime = Date.now() - startTime;
|
||||
|
||||
return {
|
||||
status: 'ok',
|
||||
database: 'connected',
|
||||
responseTime,
|
||||
timestamp,
|
||||
statusCode: 200,
|
||||
};
|
||||
} catch (error) {
|
||||
const responseTime = Date.now() - startTime;
|
||||
const details = error instanceof Error ? error.message : 'Unknown database error';
|
||||
|
||||
return {
|
||||
status: 'error',
|
||||
database: 'not reachable',
|
||||
responseTime,
|
||||
timestamp,
|
||||
details,
|
||||
statusCode: 503,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user