feat: add api liveness and readiness checks
This commit is contained in:
2
backend/libs/infrastructure/src/index.ts
Normal file
2
backend/libs/infrastructure/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './postgres/postgres.module';
|
||||
export * from './redis/redis.module';
|
||||
27
backend/libs/infrastructure/src/postgres/postgres.module.ts
Normal file
27
backend/libs/infrastructure/src/postgres/postgres.module.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Inject, Injectable, Module, OnModuleDestroy } from '@nestjs/common';
|
||||
import { Pool } from 'pg';
|
||||
import { APP_ENVIRONMENT, AppEnvironment } from '../../../configuration/src';
|
||||
|
||||
export const POSTGRES_POOL = Symbol('POSTGRES_POOL');
|
||||
|
||||
export const postgresPoolProvider = {
|
||||
provide: POSTGRES_POOL,
|
||||
inject: [APP_ENVIRONMENT],
|
||||
useFactory: (env: AppEnvironment) =>
|
||||
new Pool({ connectionString: env.databaseUrl }),
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
class PostgresLifecycle implements OnModuleDestroy {
|
||||
constructor(@Inject(POSTGRES_POOL) private readonly pool: Pool) {}
|
||||
|
||||
async onModuleDestroy(): Promise<void> {
|
||||
await this.pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
@Module({
|
||||
providers: [postgresPoolProvider, PostgresLifecycle],
|
||||
exports: [postgresPoolProvider],
|
||||
})
|
||||
export class PostgresModule {}
|
||||
27
backend/libs/infrastructure/src/redis/redis.module.ts
Normal file
27
backend/libs/infrastructure/src/redis/redis.module.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Inject, Injectable, Module, OnModuleDestroy } from '@nestjs/common';
|
||||
import Redis from 'ioredis';
|
||||
import { APP_ENVIRONMENT, AppEnvironment } from '../../../configuration/src';
|
||||
|
||||
export const REDIS_CLIENT = Symbol('REDIS_CLIENT');
|
||||
|
||||
export const redisClientProvider = {
|
||||
provide: REDIS_CLIENT,
|
||||
inject: [APP_ENVIRONMENT],
|
||||
useFactory: (env: AppEnvironment) =>
|
||||
new Redis(env.redisUrl, { lazyConnect: false }),
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
class RedisLifecycle implements OnModuleDestroy {
|
||||
constructor(@Inject(REDIS_CLIENT) private readonly client: Redis) {}
|
||||
|
||||
async onModuleDestroy(): Promise<void> {
|
||||
await this.client.quit();
|
||||
}
|
||||
}
|
||||
|
||||
@Module({
|
||||
providers: [redisClientProvider, RedisLifecycle],
|
||||
exports: [redisClientProvider],
|
||||
})
|
||||
export class RedisModule {}
|
||||
Reference in New Issue
Block a user