From 0474b79d4ca853ad63ced40ea6484837ce6d69d9 Mon Sep 17 00:00:00 2001 From: Konstantin Tsabolov <konstantin.tsabolov@spherity.com> Date: Thu, 15 Feb 2024 23:28:38 +0100 Subject: [PATCH] chore: simplify services initialization --- apps/connection-manager/src/application.ts | 39 ++++++++-------- apps/connection-manager/src/main.ts | 54 ++++++++++------------ apps/credential-manager/.env.example | 6 +-- apps/credential-manager/package.json | 3 +- apps/credential-manager/src/application.ts | 36 ++++++++------- apps/credential-manager/src/main.ts | 47 ++++++++----------- apps/did-manager/.env.example | 6 +++ apps/did-manager/package.json | 1 + apps/did-manager/src/application.ts | 39 ++++++++-------- apps/did-manager/src/main.ts | 47 ++++++++----------- apps/proof-manager/.env.example | 6 +-- apps/proof-manager/package.json | 7 +-- apps/proof-manager/src/application.ts | 39 ++++++++-------- apps/proof-manager/src/main.ts | 47 ++++++++----------- apps/schema-manager/.env.example | 6 +-- apps/schema-manager/package.json | 3 +- apps/schema-manager/src/application.ts | 39 ++++++++-------- apps/schema-manager/src/main.ts | 47 ++++++++----------- 18 files changed, 224 insertions(+), 248 deletions(-) create mode 100644 apps/did-manager/.env.example diff --git a/apps/connection-manager/src/application.ts b/apps/connection-manager/src/application.ts index 3ea98e3..e8cc292 100644 --- a/apps/connection-manager/src/application.ts +++ b/apps/connection-manager/src/application.ts @@ -1,10 +1,10 @@ +import type { OnApplicationBootstrap } from '@nestjs/common'; import type { ConfigType } from '@nestjs/config'; -import type { ClientProvider } from '@nestjs/microservices'; -import { Module } from '@nestjs/common'; +import { Inject, Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { RouterModule } from '@nestjs/core'; -import { ClientsModule, Transport } from '@nestjs/microservices'; +import { ClientProxy, ClientsModule, Transport } from '@nestjs/microservices'; import { HealthModule } from '@ocm/shared'; import { NATS_CLIENT } from './common/constants.js'; @@ -34,21 +34,14 @@ import { InvitationsModule } from './invitations/invitations.module.js'; { name: NATS_CLIENT, inject: [natsConfig.KEY], - useFactory: (config: ConfigType<typeof natsConfig>) => { - const provider: Required<ClientProvider> = { - transport: Transport.NATS, - options: { - servers: config.url as string, - }, - }; - - if ('user' in config && 'password' in config) { - provider.options.user = config.user as string; - provider.options.pass = config.password as string; - } - - return provider; - }, + useFactory: (config: ConfigType<typeof natsConfig>) => ({ + transport: Transport.NATS, + options: { + servers: [config.url], + user: config.user, + pass: config.password, + }, + }), }, ], }), @@ -78,4 +71,12 @@ import { InvitationsModule } from './invitations/invitations.module.js'; ]), ], }) -export class Application {} +export class Application implements OnApplicationBootstrap { + public constructor( + @Inject(NATS_CLIENT) private readonly client: ClientProxy, + ) {} + + public async onApplicationBootstrap(): Promise<void> { + await this.client.connect(); + } +} diff --git a/apps/connection-manager/src/main.ts b/apps/connection-manager/src/main.ts index 24529e4..a09508e 100644 --- a/apps/connection-manager/src/main.ts +++ b/apps/connection-manager/src/main.ts @@ -1,37 +1,22 @@ /* c8 ignore start */ import type { ConfigType } from '@nestjs/config'; -import type { MicroserviceOptions, NatsOptions } from '@nestjs/microservices'; -import { VersioningType } from '@nestjs/common'; -import { ConfigService } from '@nestjs/config'; +import { Logger, VersioningType } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; -import { Transport } from '@nestjs/microservices'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; +import helmet from 'helmet'; +import { createRequire } from 'module'; +import { resolve } from 'node:path'; import { Application } from './application.js'; -import { natsConfig } from './config/nats.config.js'; +import { httpConfig } from './config/http.config.js'; -const app = await NestFactory.create(Application); -const configService = app.get(ConfigService); -app.enableCors(); - -const { url, user, password } = app.get(natsConfig.KEY) as ConfigType< - typeof natsConfig ->; +const pkgPath = resolve('package.json'); +const pkg = createRequire(import.meta.url)(pkgPath); -const microserviceOptions: Required<NatsOptions> = { - transport: Transport.NATS, - options: { - servers: [url], - }, -}; - -if (user && password) { - microserviceOptions.options.user = user; - microserviceOptions.options.pass = password; -} +const app = await NestFactory.create(Application); -app.connectMicroservice<MicroserviceOptions>(microserviceOptions); +app.use(helmet()); app.enableVersioning({ defaultVersion: ['1'], @@ -39,15 +24,24 @@ app.enableVersioning({ }); const swaggerConfig = new DocumentBuilder() - .setTitle('Gaia-X Connection Manager API') - .setDescription('API documentation for GAIA-X Connection Manager') - .setVersion('1.0') + .setTitle(pkg.description) + .setVersion(pkg.version) .build(); const document = SwaggerModule.createDocument(app, swaggerConfig); -SwaggerModule.setup('/swagger', app, document); -await app.startAllMicroservices(); +SwaggerModule.setup('/', app, document, { + swaggerOptions: { + docExpansion: 'none', + tryItOutEnabled: true, + }, +}); + +const { hostname, port } = app.get(httpConfig.KEY) as ConfigType< + typeof httpConfig +>; +await app.listen(port, hostname); + +Logger.log(`Application is running on: ${await app.getUrl()}`); -await app.listen(configService.get('http.port') as number); /* c8 ignore stop */ diff --git a/apps/credential-manager/.env.example b/apps/credential-manager/.env.example index 81e338c..46fb0f1 100644 --- a/apps/credential-manager/.env.example +++ b/apps/credential-manager/.env.example @@ -1,6 +1,6 @@ -HTTP_HOST=0.0.0.0 -HTTP_PORT=3003 +HTTP_HOSTNAME=0.0.0.0 +HTTP_PORT=4003 NATS_URL=nats://localhost:4222 NATS_USER=nats_user -NATS_PASSWORD= +NATS_PASSWORD=nats_password NATS_MONITORING_URL=http://localhost:8222 diff --git a/apps/credential-manager/package.json b/apps/credential-manager/package.json index f245d88..7dfea6d 100644 --- a/apps/credential-manager/package.json +++ b/apps/credential-manager/package.json @@ -1,7 +1,7 @@ { "name": "@ocm/credential-manager", "version": "1.0.0", - "description": "", + "description": "Gaia-X OCM Credential Manager", "author": "Konstantin Tsabolov <konstantin.tsabolov@spherity.com>", "contributors": [ "Konstantin Tsabolov <konstantin.tsabolov@spherity.com>" @@ -27,6 +27,7 @@ "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "express": "^4.17.3", + "helmet": "^7.1.0", "joi": "^17.11.0", "nats": "^2.18.0", "reflect-metadata": "^0.1.13", diff --git a/apps/credential-manager/src/application.ts b/apps/credential-manager/src/application.ts index 63c7fd4..e32627c 100644 --- a/apps/credential-manager/src/application.ts +++ b/apps/credential-manager/src/application.ts @@ -1,10 +1,10 @@ +import type { OnApplicationBootstrap } from '@nestjs/common'; import type { ConfigType } from '@nestjs/config'; -import type { ClientProvider } from '@nestjs/microservices'; -import { Module } from '@nestjs/common'; +import { Inject, Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { RouterModule } from '@nestjs/core'; -import { ClientsModule, Transport } from '@nestjs/microservices'; +import { ClientProxy, ClientsModule, Transport } from '@nestjs/microservices'; import { HealthModule } from '@ocm/shared'; import { NATS_CLIENT } from './common/constants.js'; @@ -35,18 +35,14 @@ import { CredentialsModule } from './credentials/credentials.module.js'; { name: NATS_CLIENT, inject: [natsConfig.KEY], - useFactory: (config: ConfigType<typeof natsConfig>) => { - const provider: Required<ClientProvider> = { - transport: Transport.NATS, - options: { - servers: [config.url], - user: config.user as string, - pass: config.password as string, - }, - }; - - return provider; - }, + useFactory: (config: ConfigType<typeof natsConfig>) => ({ + transport: Transport.NATS, + options: { + servers: [config.url], + user: config.user as string, + pass: config.password as string, + }, + }), }, ], }), @@ -78,4 +74,12 @@ import { CredentialsModule } from './credentials/credentials.module.js'; ]), ], }) -export class Application {} +export class Application implements OnApplicationBootstrap { + public constructor( + @Inject(NATS_CLIENT) private readonly client: ClientProxy, + ) {} + + public async onApplicationBootstrap(): Promise<void> { + await this.client.connect(); + } +} diff --git a/apps/credential-manager/src/main.ts b/apps/credential-manager/src/main.ts index f2780c9..e926eb7 100644 --- a/apps/credential-manager/src/main.ts +++ b/apps/credential-manager/src/main.ts @@ -1,36 +1,22 @@ /* c8 ignore start */ import type { ConfigType } from '@nestjs/config'; -import type { MicroserviceOptions, NatsOptions } from '@nestjs/microservices'; import { Logger, VersioningType } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; -import { Transport } from '@nestjs/microservices'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; +import helmet from 'helmet'; +import { createRequire } from 'module'; +import { resolve } from 'node:path'; import { Application } from './application.js'; import { httpConfig } from './config/http.config.js'; -import { natsConfig } from './config/nats.config.js'; -const app = await NestFactory.create(Application); -app.enableCors(); - -const { url, user, password } = app.get(natsConfig.KEY) as ConfigType< - typeof natsConfig ->; - -const microserviceOptions: Required<NatsOptions> = { - transport: Transport.NATS, - options: { - servers: [url], - }, -}; +const pkgPath = resolve('package.json'); +const pkg = createRequire(import.meta.url)(pkgPath); -if (user && password) { - microserviceOptions.options.user = user; - microserviceOptions.options.pass = password; -} +const app = await NestFactory.create(Application); -app.connectMicroservice<MicroserviceOptions>(microserviceOptions); +app.use(helmet()); app.enableVersioning({ defaultVersion: ['1'], @@ -38,18 +24,23 @@ app.enableVersioning({ }); const swaggerConfig = new DocumentBuilder() - .setTitle('Gaia-X OCM Credential Manager API') - .setDescription('API documentation for Gaia-X OCM Credential Manager') - .setVersion('1.0') + .setTitle(pkg.description) + .setVersion(pkg.version) .build(); const document = SwaggerModule.createDocument(app, swaggerConfig); -SwaggerModule.setup('/swagger', app, document); -await app.startAllMicroservices(); +SwaggerModule.setup('/', app, document, { + swaggerOptions: { + docExpansion: 'none', + tryItOutEnabled: true, + }, +}); -const { host, port } = app.get(httpConfig.KEY) as ConfigType<typeof httpConfig>; -await app.listen(port as number, host as string); +const { hostname, port } = app.get(httpConfig.KEY) as ConfigType< + typeof httpConfig +>; +await app.listen(port, hostname); Logger.log(`Application is running on: ${await app.getUrl()}`); /* c8 ignore stop */ diff --git a/apps/did-manager/.env.example b/apps/did-manager/.env.example new file mode 100644 index 0000000..dfef64c --- /dev/null +++ b/apps/did-manager/.env.example @@ -0,0 +1,6 @@ +HTTP_HOSTNAME=0.0.0.0 +HTTP_PORT=4006 +NATS_URL=nats://localhost:4222 +NATS_USER=nats_user +NATS_PASSWORD=nats_password +NATS_MONITORING_URL=http://localhost:8222 diff --git a/apps/did-manager/package.json b/apps/did-manager/package.json index 669db0a..c20134a 100644 --- a/apps/did-manager/package.json +++ b/apps/did-manager/package.json @@ -27,6 +27,7 @@ "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "express": "^4.17.3", + "helmet": "^7.1.0", "joi": "^17.11.0", "nats": "^2.18.0", "reflect-metadata": "^0.1.13", diff --git a/apps/did-manager/src/application.ts b/apps/did-manager/src/application.ts index 8e4cea8..81185d0 100644 --- a/apps/did-manager/src/application.ts +++ b/apps/did-manager/src/application.ts @@ -1,10 +1,10 @@ +import type { OnApplicationBootstrap } from '@nestjs/common'; import type { ConfigType } from '@nestjs/config'; -import type { ClientProvider } from '@nestjs/microservices'; -import { Module } from '@nestjs/common'; +import { Inject, Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { RouterModule } from '@nestjs/core'; -import { ClientsModule, Transport } from '@nestjs/microservices'; +import { ClientProxy, ClientsModule, Transport } from '@nestjs/microservices'; import { HealthModule } from '@ocm/shared'; import { NATS_CLIENT } from './common/constants.js'; @@ -33,21 +33,14 @@ import { DIDsModule } from './dids/dids.module.js'; { name: NATS_CLIENT, inject: [natsConfig.KEY], - useFactory: (config: ConfigType<typeof natsConfig>) => { - const provider: Required<ClientProvider> = { - transport: Transport.NATS, - options: { - servers: config.url as string, - }, - }; - - if ('user' in config && 'password' in config) { - provider.options.user = config.user as string; - provider.options.pass = config.password as string; - } - - return provider; - }, + useFactory: (config: ConfigType<typeof natsConfig>) => ({ + transport: Transport.NATS, + options: { + servers: [config.url], + user: config.user as string, + pass: config.password as string, + }, + }), }, ], }), @@ -75,4 +68,12 @@ import { DIDsModule } from './dids/dids.module.js'; ]), ], }) -export class Application {} +export class Application implements OnApplicationBootstrap { + public constructor( + @Inject(NATS_CLIENT) private readonly client: ClientProxy, + ) {} + + public async onApplicationBootstrap(): Promise<void> { + await this.client.connect(); + } +} diff --git a/apps/did-manager/src/main.ts b/apps/did-manager/src/main.ts index fa521d4..e926eb7 100644 --- a/apps/did-manager/src/main.ts +++ b/apps/did-manager/src/main.ts @@ -1,36 +1,22 @@ /* c8 ignore start */ import type { ConfigType } from '@nestjs/config'; -import type { MicroserviceOptions, NatsOptions } from '@nestjs/microservices'; import { Logger, VersioningType } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; -import { Transport } from '@nestjs/microservices'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; +import helmet from 'helmet'; +import { createRequire } from 'module'; +import { resolve } from 'node:path'; import { Application } from './application.js'; import { httpConfig } from './config/http.config.js'; -import { natsConfig } from './config/nats.config.js'; -const app = await NestFactory.create(Application); -app.enableCors(); - -const { url, user, password } = app.get(natsConfig.KEY) as ConfigType< - typeof natsConfig ->; - -const microserviceOptions: Required<NatsOptions> = { - transport: Transport.NATS, - options: { - servers: [url], - }, -}; +const pkgPath = resolve('package.json'); +const pkg = createRequire(import.meta.url)(pkgPath); -if (user && password) { - microserviceOptions.options.user = user; - microserviceOptions.options.pass = password; -} +const app = await NestFactory.create(Application); -app.connectMicroservice<MicroserviceOptions>(microserviceOptions); +app.use(helmet()); app.enableVersioning({ defaultVersion: ['1'], @@ -38,18 +24,23 @@ app.enableVersioning({ }); const swaggerConfig = new DocumentBuilder() - .setTitle('Gaia-X OCM DID Manager API') - .setDescription('API documentation for Gaia-X OCM DID Manager') - .setVersion('1.0') + .setTitle(pkg.description) + .setVersion(pkg.version) .build(); const document = SwaggerModule.createDocument(app, swaggerConfig); -SwaggerModule.setup('/', app, document); -await app.startAllMicroservices(); +SwaggerModule.setup('/', app, document, { + swaggerOptions: { + docExpansion: 'none', + tryItOutEnabled: true, + }, +}); -const { host, port } = app.get(httpConfig.KEY) as ConfigType<typeof httpConfig>; -await app.listen(port as number, host as string); +const { hostname, port } = app.get(httpConfig.KEY) as ConfigType< + typeof httpConfig +>; +await app.listen(port, hostname); Logger.log(`Application is running on: ${await app.getUrl()}`); /* c8 ignore stop */ diff --git a/apps/proof-manager/.env.example b/apps/proof-manager/.env.example index f56f879..0bdd0d9 100644 --- a/apps/proof-manager/.env.example +++ b/apps/proof-manager/.env.example @@ -1,6 +1,6 @@ -HTTP_HOST=0.0.0.0 -HTTP_PORT=3004 +HTTP_HOSTNAME=0.0.0.0 +HTTP_PORT=4004 NATS_URL=nats://localhost:4222 NATS_USER=nats_user -NATS_PASSWORD= +NATS_PASSWORD=nats_password NATS_MONITORING_URL=http://localhost:8222 diff --git a/apps/proof-manager/package.json b/apps/proof-manager/package.json index 931642d..1026009 100644 --- a/apps/proof-manager/package.json +++ b/apps/proof-manager/package.json @@ -1,7 +1,7 @@ { "name": "@ocm/proof-manager", - "version": "0.0.1", - "description": "The Proof Manager provides an endpoint to request a presentation over an existing connection", + "version": "1.0.0", + "description": "Gaia-X OCM Proof Manager", "author": "Sagar", "private": true, "license": "Apache-2.0", @@ -22,9 +22,10 @@ "@nestjs/platform-express": "^10.3.0", "@nestjs/swagger": "^7.1.16", "@ocm/shared": "workspace:*", - "class-validator": "^0.14.0", "class-transformer": "^0.5.1", + "class-validator": "^0.14.0", "express": "^4.17.3", + "helmet": "^7.1.0", "joi": "^17.11.0", "nats": "^2.18.0", "reflect-metadata": "^0.1.13", diff --git a/apps/proof-manager/src/application.ts b/apps/proof-manager/src/application.ts index 19c04cb..ffc45aa 100644 --- a/apps/proof-manager/src/application.ts +++ b/apps/proof-manager/src/application.ts @@ -1,10 +1,10 @@ +import type { OnApplicationBootstrap } from '@nestjs/common'; import type { ConfigType } from '@nestjs/config'; -import type { ClientProvider } from '@nestjs/microservices'; -import { Module } from '@nestjs/common'; +import { Inject, Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { RouterModule } from '@nestjs/core'; -import { ClientsModule, Transport } from '@nestjs/microservices'; +import { ClientProxy, ClientsModule, Transport } from '@nestjs/microservices'; import { HealthModule } from '@ocm/shared'; import { NATS_CLIENT } from './common/constants.js'; @@ -33,21 +33,14 @@ import { ProofsModule } from './proofs/proofs.module.js'; { name: NATS_CLIENT, inject: [natsConfig.KEY], - useFactory: (config: ConfigType<typeof natsConfig>) => { - const provider: Required<ClientProvider> = { - transport: Transport.NATS, - options: { - servers: config.url as string, - }, - }; - - if ('user' in config && 'password' in config) { - provider.options.user = config.user as string; - provider.options.pass = config.password as string; - } - - return provider; - }, + useFactory: (config: ConfigType<typeof natsConfig>) => ({ + transport: Transport.NATS, + options: { + servers: [config.url], + user: config.user as string, + pass: config.password as string, + }, + }), }, ], }), @@ -75,4 +68,12 @@ import { ProofsModule } from './proofs/proofs.module.js'; ]), ], }) -export class Application {} +export class Application implements OnApplicationBootstrap { + public constructor( + @Inject(NATS_CLIENT) private readonly client: ClientProxy, + ) {} + + public async onApplicationBootstrap(): Promise<void> { + await this.client.connect(); + } +} diff --git a/apps/proof-manager/src/main.ts b/apps/proof-manager/src/main.ts index fe4aad7..e926eb7 100644 --- a/apps/proof-manager/src/main.ts +++ b/apps/proof-manager/src/main.ts @@ -1,36 +1,22 @@ /* c8 ignore start */ import type { ConfigType } from '@nestjs/config'; -import type { MicroserviceOptions, NatsOptions } from '@nestjs/microservices'; import { Logger, VersioningType } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; -import { Transport } from '@nestjs/microservices'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; +import helmet from 'helmet'; +import { createRequire } from 'module'; +import { resolve } from 'node:path'; import { Application } from './application.js'; import { httpConfig } from './config/http.config.js'; -import { natsConfig } from './config/nats.config.js'; -const app = await NestFactory.create(Application); -app.enableCors(); - -const { url, user, password } = app.get(natsConfig.KEY) as ConfigType< - typeof natsConfig ->; - -const microserviceOptions: Required<NatsOptions> = { - transport: Transport.NATS, - options: { - servers: [url], - }, -}; +const pkgPath = resolve('package.json'); +const pkg = createRequire(import.meta.url)(pkgPath); -if (user && password) { - microserviceOptions.options.user = user; - microserviceOptions.options.pass = password; -} +const app = await NestFactory.create(Application); -app.connectMicroservice<MicroserviceOptions>(microserviceOptions); +app.use(helmet()); app.enableVersioning({ defaultVersion: ['1'], @@ -38,18 +24,23 @@ app.enableVersioning({ }); const swaggerConfig = new DocumentBuilder() - .setTitle('Gaia-X OCM Proof Manager API') - .setDescription('API documentation for Gaia-X OCM Proof Manager') - .setVersion('1.0') + .setTitle(pkg.description) + .setVersion(pkg.version) .build(); const document = SwaggerModule.createDocument(app, swaggerConfig); -SwaggerModule.setup('/swagger', app, document); -await app.startAllMicroservices(); +SwaggerModule.setup('/', app, document, { + swaggerOptions: { + docExpansion: 'none', + tryItOutEnabled: true, + }, +}); -const { host, port } = app.get(httpConfig.KEY) as ConfigType<typeof httpConfig>; -await app.listen(port as number, host as string); +const { hostname, port } = app.get(httpConfig.KEY) as ConfigType< + typeof httpConfig +>; +await app.listen(port, hostname); Logger.log(`Application is running on: ${await app.getUrl()}`); /* c8 ignore stop */ diff --git a/apps/schema-manager/.env.example b/apps/schema-manager/.env.example index 55e246f..e6e7935 100644 --- a/apps/schema-manager/.env.example +++ b/apps/schema-manager/.env.example @@ -1,6 +1,6 @@ -HTTP_HOST=0.0.0.0 -HTTP_PORT=3001 +HTTP_HOSTNAME=0.0.0.0 +HTTP_PORT=4001 NATS_URL=nats://localhost:4222 NATS_USER=nats_user -NATS_PASSWORD= +NATS_PASSWORD=nats_password NATS_MONITORING_URL=http://localhost:8222 diff --git a/apps/schema-manager/package.json b/apps/schema-manager/package.json index 4e08b78..748940b 100644 --- a/apps/schema-manager/package.json +++ b/apps/schema-manager/package.json @@ -1,7 +1,7 @@ { "name": "@ocm/schema-manager", "version": "1.0.0", - "description": "", + "description": "Gaia-X OCM Schema Manager", "author": "Konstantin Tsabolov <konstantin.tsabolov@spherity.com>", "contributors": [ "Konstantin Tsabolov <konstantin.tsabolov@spherity.com>" @@ -27,6 +27,7 @@ "class-transformer": "^0.5.1", "class-validator": "^0.14.0", "express": "^4.17.3", + "helmet": "^7.1.0", "joi": "^17.11.0", "nats": "^2.18.0", "reflect-metadata": "^0.1.13", diff --git a/apps/schema-manager/src/application.ts b/apps/schema-manager/src/application.ts index 649befb..b67928f 100644 --- a/apps/schema-manager/src/application.ts +++ b/apps/schema-manager/src/application.ts @@ -1,10 +1,10 @@ +import type { OnApplicationBootstrap } from '@nestjs/common'; import type { ConfigType } from '@nestjs/config'; -import type { ClientProvider } from '@nestjs/microservices'; -import { Module } from '@nestjs/common'; +import { Inject, Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { RouterModule } from '@nestjs/core'; -import { ClientsModule, Transport } from '@nestjs/microservices'; +import { ClientProxy, ClientsModule, Transport } from '@nestjs/microservices'; import { HealthModule } from '@ocm/shared'; import { NATS_CLIENT } from './common/constants.js'; @@ -34,21 +34,14 @@ import { SchemasModule } from './schemas/schemas.module.js'; { name: NATS_CLIENT, inject: [natsConfig.KEY], - useFactory: (config: ConfigType<typeof natsConfig>) => { - const provider: Required<ClientProvider> = { - transport: Transport.NATS, - options: { - servers: config.url as string, - }, - }; - - if ('user' in config && 'password' in config) { - provider.options.user = config.user as string; - provider.options.pass = config.password as string; - } - - return provider; - }, + useFactory: (config: ConfigType<typeof natsConfig>) => ({ + transport: Transport.NATS, + options: { + servers: [config.url], + user: config.user as string, + pass: config.password as string, + }, + }), }, ], }), @@ -78,4 +71,12 @@ import { SchemasModule } from './schemas/schemas.module.js'; ]), ], }) -export class Application {} +export class Application implements OnApplicationBootstrap { + public constructor( + @Inject(NATS_CLIENT) private readonly client: ClientProxy, + ) {} + + public async onApplicationBootstrap(): Promise<void> { + await this.client.connect(); + } +} diff --git a/apps/schema-manager/src/main.ts b/apps/schema-manager/src/main.ts index 1472b8c..e926eb7 100644 --- a/apps/schema-manager/src/main.ts +++ b/apps/schema-manager/src/main.ts @@ -1,36 +1,22 @@ /* c8 ignore start */ import type { ConfigType } from '@nestjs/config'; -import type { MicroserviceOptions, NatsOptions } from '@nestjs/microservices'; import { Logger, VersioningType } from '@nestjs/common'; import { NestFactory } from '@nestjs/core'; -import { Transport } from '@nestjs/microservices'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; +import helmet from 'helmet'; +import { createRequire } from 'module'; +import { resolve } from 'node:path'; import { Application } from './application.js'; import { httpConfig } from './config/http.config.js'; -import { natsConfig } from './config/nats.config.js'; -const app = await NestFactory.create(Application); -app.enableCors(); - -const { url, user, password } = app.get(natsConfig.KEY) as ConfigType< - typeof natsConfig ->; - -const microserviceOptions: Required<NatsOptions> = { - transport: Transport.NATS, - options: { - servers: [url], - }, -}; +const pkgPath = resolve('package.json'); +const pkg = createRequire(import.meta.url)(pkgPath); -if (user && password) { - microserviceOptions.options.user = user; - microserviceOptions.options.pass = password; -} +const app = await NestFactory.create(Application); -app.connectMicroservice<MicroserviceOptions>(microserviceOptions); +app.use(helmet()); app.enableVersioning({ defaultVersion: ['1'], @@ -38,18 +24,23 @@ app.enableVersioning({ }); const swaggerConfig = new DocumentBuilder() - .setTitle('Gaia-X OCM Schema Manager API') - .setDescription('API documentation for Gaia-X OCM Schema Manager') - .setVersion('1.0') + .setTitle(pkg.description) + .setVersion(pkg.version) .build(); const document = SwaggerModule.createDocument(app, swaggerConfig); -SwaggerModule.setup('/swagger', app, document); -await app.startAllMicroservices(); +SwaggerModule.setup('/', app, document, { + swaggerOptions: { + docExpansion: 'none', + tryItOutEnabled: true, + }, +}); -const { host, port } = app.get(httpConfig.KEY) as ConfigType<typeof httpConfig>; -await app.listen(port as number, host as string); +const { hostname, port } = app.get(httpConfig.KEY) as ConfigType< + typeof httpConfig +>; +await app.listen(port, hostname); Logger.log(`Application is running on: ${await app.getUrl()}`); /* c8 ignore stop */ -- GitLab