configuration module

This commit is contained in:
Gsk54 2023-01-26 17:28:50 +01:00
parent bfa0ef1187
commit 42bd61d6ca
5 changed files with 51 additions and 15 deletions

View File

@ -1,6 +1,7 @@
# SERVICE # SERVICE
SERVICE_URL=0.0.0.0 SERVICE_URL=0.0.0.0
SERVICE_PORT=5001 SERVICE_PORT=5001
SERVICE_CONFIGURATION_DOMAIN=USER
# PRISMA # PRISMA
DATABASE_URL="postgresql://user:user@v3-user-db:5432/user?schema=public" DATABASE_URL="postgresql://user:user@v3-user-db:5432/user?schema=public"
@ -13,3 +14,5 @@ POSTGRES_IMAGE=postgres:15.0
# REDIS # REDIS
REDIS_IMAGE=redis/redis-stack:latest REDIS_IMAGE=redis/redis-stack:latest
REDIS_HOST=v3-user-redis
REDIS_PORT=6379

View File

@ -1,6 +1,7 @@
# SERVICE # SERVICE
SERVICE_URL=0.0.0.0 SERVICE_URL=0.0.0.0
SERVICE_PORT=5001 SERVICE_PORT=5001
SERVICE_CONFIGURATION_DOMAIN=USER
# PRISMA # PRISMA
DATABASE_URL="postgresql://user:user@localhost:5601/user?schema=public" DATABASE_URL="postgresql://user:user@localhost:5601/user?schema=public"
@ -13,3 +14,5 @@ POSTGRES_IMAGE=postgres:15.0
# REDIS # REDIS
REDIS_IMAGE=redis/redis-stack:latest REDIS_IMAGE=redis/redis-stack:latest
REDIS_HOST=v3-user-redis
REDIS_PORT=6379

View File

@ -21,6 +21,7 @@
"test:integration:ci": "npm run migrate:test:ci && dotenv -e ci/.env.ci -- jest --testPathPattern 'tests/integration/'", "test:integration:ci": "npm run migrate:test:ci && dotenv -e ci/.env.ci -- jest --testPathPattern 'tests/integration/'",
"test:cov": "jest --testPathPattern 'tests/unit/' --coverage", "test:cov": "jest --testPathPattern 'tests/unit/' --coverage",
"test:e2e": "jest --config ./test/jest-e2e.json", "test:e2e": "jest --config ./test/jest-e2e.json",
"generate": "docker exec v3-user sh -c 'npx prisma generate'",
"migrate": "docker exec v3-user sh -c 'npx prisma migrate dev'", "migrate": "docker exec v3-user sh -c 'npx prisma migrate dev'",
"migrate:test": "dotenv -e .env.test -- npx prisma migrate deploy", "migrate:test": "dotenv -e .env.test -- npx prisma migrate deploy",
"migrate:test:ci": "dotenv -e ci/.env.ci -- npx prisma migrate deploy" "migrate:test:ci": "dotenv -e ci/.env.ci -- npx prisma migrate deploy"

View File

@ -21,6 +21,10 @@ export class ConfigurationMessagerController {
}) })
public async setConfigurationHandler(message: string) { public async setConfigurationHandler(message: string) {
const configuration = JSON.parse(message); const configuration = JSON.parse(message);
if (
configuration.domain ==
this._configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
) {
const setConfigurationRequest: SetConfigurationRequest = const setConfigurationRequest: SetConfigurationRequest =
new SetConfigurationRequest(); new SetConfigurationRequest();
setConfigurationRequest.domain = configuration.domain; setConfigurationRequest.domain = configuration.domain;
@ -30,6 +34,7 @@ export class ConfigurationMessagerController {
new SetConfigurationCommand(setConfigurationRequest), new SetConfigurationCommand(setConfigurationRequest),
); );
} }
}
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'configuration', exchange: 'configuration',
@ -38,6 +43,10 @@ export class ConfigurationMessagerController {
}) })
public async configurationDeletedHandler(message: string) { public async configurationDeletedHandler(message: string) {
const deletedConfiguration = JSON.parse(message); const deletedConfiguration = JSON.parse(message);
if (
deletedConfiguration.domain ==
this._configService.get<string>('SERVICE_CONFIGURATION_DOMAIN')
) {
const deleteConfigurationRequest = new DeleteConfigurationRequest(); const deleteConfigurationRequest = new DeleteConfigurationRequest();
deleteConfigurationRequest.domain = deletedConfiguration.domain; deleteConfigurationRequest.domain = deletedConfiguration.domain;
deleteConfigurationRequest.key = deletedConfiguration.key; deleteConfigurationRequest.key = deletedConfiguration.key;
@ -45,4 +54,5 @@ export class ConfigurationMessagerController {
new DeleteConfigurationCommand(deleteConfigurationRequest), new DeleteConfigurationCommand(deleteConfigurationRequest),
); );
} }
}
} }

View File

@ -1,8 +1,11 @@
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
import { RedisModule, RedisModuleOptions } from '@liaoliaots/nestjs-redis'; import { RedisModule, RedisModuleOptions } from '@liaoliaots/nestjs-redis';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config'; import { ConfigModule, ConfigService } from '@nestjs/config';
import { CqrsModule } from '@nestjs/cqrs'; import { CqrsModule } from '@nestjs/cqrs';
import { ConfigurationMessagerController } from './adapters/primaries/configuration-messager.controller';
import { RedisConfigurationRepository } from './adapters/secondaries/redis-configuration.repository'; import { RedisConfigurationRepository } from './adapters/secondaries/redis-configuration.repository';
import { DeleteConfigurationUseCase } from './domain/usecases/delete-configuration.usecase';
import { GetConfigurationUseCase } from './domain/usecases/get-configuration.usecase'; import { GetConfigurationUseCase } from './domain/usecases/get-configuration.usecase';
import { SetConfigurationUseCase } from './domain/usecases/set-configuration.usecase'; import { SetConfigurationUseCase } from './domain/usecases/set-configuration.usecase';
@ -23,11 +26,27 @@ import { SetConfigurationUseCase } from './domain/usecases/set-configuration.use
}; };
}, },
}), }),
RabbitMQModule.forRootAsync(RabbitMQModule, {
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
exchanges: [
{
name: 'configuration',
type: 'topic',
},
], ],
controllers: [], uri: configService.get<string>('RMQ_URI'),
connectionInitOptions: { wait: false },
enableControllerDiscovery: true,
}),
inject: [ConfigService],
}),
],
controllers: [ConfigurationMessagerController],
providers: [ providers: [
GetConfigurationUseCase, GetConfigurationUseCase,
SetConfigurationUseCase, SetConfigurationUseCase,
DeleteConfigurationUseCase,
RedisConfigurationRepository, RedisConfigurationRepository,
], ],
}) })