Skip to content
Snippets Groups Projects
Verified Commit efa9b87f authored by Konstantin Tsabolov's avatar Konstantin Tsabolov
Browse files

chore(credential-manager): make use of shared health modules and response format interceptor

parent 224971cf
No related branches found
No related tags found
1 merge request!17Schemas and credential definitions. Refs #9
...@@ -24,15 +24,13 @@ ...@@ -24,15 +24,13 @@
"test:e2e": "jest --config ./test/jest.config.js" "test:e2e": "jest --config ./test/jest.config.js"
}, },
"dependencies": { "dependencies": {
"@nestjs/axios": "^3.0.1",
"@nestjs/common": "^10.2.10", "@nestjs/common": "^10.2.10",
"@nestjs/config": "^3.1.1", "@nestjs/config": "^3.1.1",
"@nestjs/core": "^10.2.10", "@nestjs/core": "^10.2.10",
"@nestjs/microservices": "^10.2.10", "@nestjs/microservices": "^10.2.10",
"@nestjs/platform-express": "^10.2.8", "@nestjs/platform-express": "^10.2.8",
"@nestjs/swagger": "^7.1.16", "@nestjs/swagger": "^7.1.16",
"@nestjs/terminus": "^10.1.1", "@ocm/shared": "workspace:*",
"axios": "^1.6.2",
"class-transformer": "^0.5.1", "class-transformer": "^0.5.1",
"class-validator": "^0.14.0", "class-validator": "^0.14.0",
"express": "^4.17.3", "express": "^4.17.3",
......
import type { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { Application } from '../application.js';
describe('Application', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture = await Test.createTestingModule({
imports: [Application],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
});
it('should be defined', () => {
expect(app).toBeDefined();
});
});
import type { ConfigType } from '@nestjs/config';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
import { RouterModule } from '@nestjs/core';
import { HealthModule } from '@ocm/shared';
import { httpConfig } from './config/http.config.js'; import { httpConfig } from './config/http.config.js';
import { natsConfig } from './config/nats.config.js'; import { natsConfig } from './config/nats.config.js';
import { ssiConfig } from './config/ssi.config.js'; import { ssiConfig } from './config/ssi.config.js';
import { validationSchema } from './config/validation.js'; import { validationSchema } from './config/validation.js';
import { HealthModule } from './health/health.module.js';
@Module({ @Module({
imports: [ imports: [
...@@ -20,7 +23,23 @@ import { HealthModule } from './health/health.module.js'; ...@@ -20,7 +23,23 @@ import { HealthModule } from './health/health.module.js';
abortEarly: true, abortEarly: true,
}, },
}), }),
HealthModule,
HealthModule.registerAsync({
inject: [natsConfig.KEY],
useFactory: (config: ConfigType<typeof natsConfig>) => {
const options: Parameters<typeof HealthModule.register>[0] = {};
if (config.monitoringUrl) {
options.nats = {
monitoringUrl: config.monitoringUrl as string,
};
}
return options;
},
}),
RouterModule.register([{ module: HealthModule, path: '/health' }]),
], ],
}) })
export default class AppModule {} export class Application {}
import type { HealthIndicatorFunction } from '@nestjs/terminus';
import { Controller, Get } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import {
HealthCheck,
HealthCheckService,
HttpHealthIndicator,
} from '@nestjs/terminus';
@Controller('health')
export class HealthController {
public constructor(
private readonly config: ConfigService,
private readonly health: HealthCheckService,
private readonly http: HttpHealthIndicator,
) {}
@Get()
@HealthCheck()
public check() {
const healthIndicators: HealthIndicatorFunction[] = [];
const natsMonitoringUrl = this.config.get('nats.monitoringUrl');
if (typeof natsMonitoringUrl === 'string') {
healthIndicators.push(() =>
this.http.pingCheck('nats', natsMonitoringUrl),
);
} else {
healthIndicators.push(() => ({ nats: { status: 'down' } }));
}
return this.health.check(healthIndicators);
}
}
import type { ConfigType } from '@nestjs/config';
import { HttpModule } from '@nestjs/axios';
import { Module } from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { TerminusModule } from '@nestjs/terminus';
import { SERVICE_NAME } from '../common/constants.js';
import { natsConfig } from '../config/nats.config.js';
import { HealthController } from './health.controller.js';
@Module({
imports: [
TerminusModule,
HttpModule,
ClientsModule.registerAsync({
clients: [
{
name: SERVICE_NAME,
inject: [natsConfig.KEY],
useFactory: (config: ConfigType<typeof natsConfig>) => ({
transport: Transport.NATS,
options: {
servers: [config.url as string],
},
}),
},
],
}),
],
controllers: [HealthController],
})
export class HealthModule {}
...@@ -6,9 +6,9 @@ import { NestFactory } from '@nestjs/core'; ...@@ -6,9 +6,9 @@ import { NestFactory } from '@nestjs/core';
import { Transport } from '@nestjs/microservices'; import { Transport } from '@nestjs/microservices';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import AppModule from './app.module.js'; import { Application } from './application.js';
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(Application);
const configService = app.get(ConfigService); const configService = app.get(ConfigService);
app.enableCors(); app.enableCors();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment