refactor(v4.0.0-alpha): beginning of a new era

This commit is contained in:
Amruth Pillai
2023-11-05 12:31:42 +01:00
parent 0ba6a444e2
commit 22933bd412
505 changed files with 81829 additions and 0 deletions

View 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 });
}
}
}

View 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 });
}
}
}

View 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);
}
}

View 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 {}

View 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 });
}
}
}