mirror of
https://github.com/AmruthPillai/Reactive-Resume.git
synced 2025-11-23 21:21:32 +10:00
refactor(v4.0.0-alpha): beginning of a new era
This commit is contained in:
23
apps/server/src/health/browser.health.ts
Normal file
23
apps/server/src/health/browser.health.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { HealthIndicator, HealthIndicatorResult } from "@nestjs/terminus";
|
||||
import { withTimeout } from "@reactive-resume/utils";
|
||||
|
||||
import { PrinterService } from "../printer/printer.service";
|
||||
|
||||
@Injectable()
|
||||
export class BrowserHealthIndicator extends HealthIndicator {
|
||||
constructor(private readonly printerService: PrinterService) {
|
||||
super();
|
||||
}
|
||||
|
||||
async isHealthy(): Promise<HealthIndicatorResult> {
|
||||
try {
|
||||
const version = await withTimeout(this.printerService.getVersion(), 5000);
|
||||
// const version = await this.printerService.getVersion();
|
||||
|
||||
return this.getStatus("browser", true, { version });
|
||||
} catch (error) {
|
||||
return this.getStatus("browser", false, { message: error.message });
|
||||
}
|
||||
}
|
||||
}
|
||||
20
apps/server/src/health/database.health.ts
Normal file
20
apps/server/src/health/database.health.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { HealthIndicator, HealthIndicatorResult } from "@nestjs/terminus";
|
||||
import { PrismaService } from "nestjs-prisma";
|
||||
|
||||
@Injectable()
|
||||
export class DatabaseHealthIndicator extends HealthIndicator {
|
||||
constructor(private readonly prisma: PrismaService) {
|
||||
super();
|
||||
}
|
||||
|
||||
async isHealthy(): Promise<HealthIndicatorResult> {
|
||||
try {
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
|
||||
return this.getStatus("database", true);
|
||||
} catch (error) {
|
||||
return this.getStatus("database", false, { message: error.message });
|
||||
}
|
||||
}
|
||||
}
|
||||
48
apps/server/src/health/health.controller.ts
Normal file
48
apps/server/src/health/health.controller.ts
Normal file
@ -0,0 +1,48 @@
|
||||
import { CacheInterceptor, CacheKey, CacheTTL } from "@nestjs/cache-manager";
|
||||
import { Controller, Get, NotFoundException, UseInterceptors } from "@nestjs/common";
|
||||
import { HealthCheck, HealthCheckService } from "@nestjs/terminus";
|
||||
import { RedisService } from "@songkeys/nestjs-redis";
|
||||
import { RedisHealthIndicator } from "@songkeys/nestjs-redis-health";
|
||||
|
||||
import { configSchema } from "../config/schema";
|
||||
import { BrowserHealthIndicator } from "./browser.health";
|
||||
import { DatabaseHealthIndicator } from "./database.health";
|
||||
import { StorageHealthIndicator } from "./storage.health";
|
||||
|
||||
@Controller("health")
|
||||
export class HealthController {
|
||||
constructor(
|
||||
private readonly health: HealthCheckService,
|
||||
private readonly database: DatabaseHealthIndicator,
|
||||
private readonly browser: BrowserHealthIndicator,
|
||||
private readonly storage: StorageHealthIndicator,
|
||||
private readonly redisService: RedisService,
|
||||
private readonly redis: RedisHealthIndicator,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
@HealthCheck()
|
||||
@UseInterceptors(CacheInterceptor)
|
||||
@CacheKey("health:check")
|
||||
@CacheTTL(30000) // 30 seconds
|
||||
check() {
|
||||
return this.health.check([
|
||||
() => this.database.isHealthy(),
|
||||
() => this.storage.isHealthy(),
|
||||
() => this.browser.isHealthy(),
|
||||
() => {
|
||||
return this.redis.checkHealth("redis", {
|
||||
type: "redis",
|
||||
timeout: 1000,
|
||||
client: this.redisService.getClient(),
|
||||
});
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
@Get("environment")
|
||||
environment() {
|
||||
if (process.env.NODE_ENV === "production") throw new NotFoundException();
|
||||
return configSchema.parse(process.env);
|
||||
}
|
||||
}
|
||||
17
apps/server/src/health/health.module.ts
Normal file
17
apps/server/src/health/health.module.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TerminusModule } from "@nestjs/terminus";
|
||||
import { RedisHealthModule } from "@songkeys/nestjs-redis-health";
|
||||
|
||||
import { PrinterModule } from "../printer/printer.module";
|
||||
import { StorageModule } from "../storage/storage.module";
|
||||
import { BrowserHealthIndicator } from "./browser.health";
|
||||
import { DatabaseHealthIndicator } from "./database.health";
|
||||
import { HealthController } from "./health.controller";
|
||||
import { StorageHealthIndicator } from "./storage.health";
|
||||
|
||||
@Module({
|
||||
imports: [TerminusModule, PrinterModule, StorageModule, RedisHealthModule],
|
||||
controllers: [HealthController],
|
||||
providers: [DatabaseHealthIndicator, BrowserHealthIndicator, StorageHealthIndicator],
|
||||
})
|
||||
export class HealthModule {}
|
||||
21
apps/server/src/health/storage.health.ts
Normal file
21
apps/server/src/health/storage.health.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { HealthIndicator, HealthIndicatorResult } from "@nestjs/terminus";
|
||||
|
||||
import { StorageService } from "../storage/storage.service";
|
||||
|
||||
@Injectable()
|
||||
export class StorageHealthIndicator extends HealthIndicator {
|
||||
constructor(private readonly storageService: StorageService) {
|
||||
super();
|
||||
}
|
||||
|
||||
async isHealthy(): Promise<HealthIndicatorResult> {
|
||||
try {
|
||||
await this.storageService.bucketExists();
|
||||
|
||||
return this.getStatus("storage", true);
|
||||
} catch (error) {
|
||||
return this.getStatus("storage", false, { message: error.message });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user