refactor message broker

This commit is contained in:
sbriat 2023-03-15 17:41:16 +01:00
parent a9c180fbb2
commit e7f0ab0652
7 changed files with 157 additions and 92 deletions

View File

@ -4,3 +4,4 @@ SERVICE_PORT=5099
# RABBIT MQ # RABBIT MQ
RMQ_URI=amqp://v3-broker:5672 RMQ_URI=amqp://v3-broker:5672
RMQ_EXCHANGE=mobicoop

View File

@ -1,8 +1,10 @@
import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
await app.listen(3000); const configService: ConfigService = app.get(ConfigService);
await app.listen(configService.get<number>('SERVICE_PORT'));
} }
bootstrap(); bootstrap();

View File

@ -15,10 +15,132 @@ import { TerritoryController } from './logger/adapters/primaries/territory.contr
useFactory: async (configService: ConfigService) => ({ useFactory: async (configService: ConfigService) => ({
exchanges: [ exchanges: [
{ {
name: 'logging', name: configService.get<string>('RMQ_EXCHANGE'),
type: 'topic', type: 'topic',
}, },
], ],
handlers: {
loggingAuthCreateCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.auth.create.crit',
},
loggingAuthDeleteCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.auth.delete.crit',
},
loggingAuthUsernameAddWarning: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.auth.username.add.warning',
},
loggingAuthUsernameDeleteWarning: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.auth.username.delete.warning',
},
loggingAuthPasswordUpdateWarning: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.auth.password.update.warning',
},
loggingAuthUsernameUpdateWarning: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.auth.username.update.warning',
},
loggingConfigurationCreateInfo: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.configuration.create.info',
},
loggingConfigurationCreateWarning: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.configuration.create.warning',
},
loggingConfigurationCreateCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.configuration.create.crit',
},
loggingConfigurationDeleteInfo: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.configuration.delete.info',
},
loggingConfigurationDeleteCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.configuration.delete.crit',
},
loggingConfigurationReadWarning: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.configuration.read.warning',
},
loggingConfigurationUpdateInfo: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.configuration.update.info',
},
loggingConfigurationUpdateCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.configuration.update.crit',
},
loggingTerritoryCreateInfo: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.territory.create.info',
},
loggingTerritoryCreateWarning: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.territory.create.warning',
},
loggingTerritoryCreateCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.territory.create.crit',
},
loggingTerritoryDeleteInfo: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.territory.delete.info',
},
loggingTerritoryDeleteCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.territory.delete.crit',
},
loggingTerritoryReadWarning: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.territory.read.warning',
},
loggingTerritoryUpdateInfo: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.territory.update.info',
},
loggingTerritoryUpdateCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.territory.update.crit',
},
loggingUserCreateInfo: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.user.create.info',
},
loggingUserCreateWarning: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.user.create.warning',
},
loggingUserCreateCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.user.create.crit',
},
loggingUserDeleteInfo: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.user.delete.info',
},
loggingUserDeleteCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.user.delete.crit',
},
loggingUserReadWarning: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.user.read.warning',
},
loggingUserUpdateInfo: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.user.update.info',
},
loggingUserUpdateCrit: {
exchange: configService.get<string>('RMQ_EXCHANGE'),
routingKey: 'logging.user.update.crit',
},
},
uri: configService.get<string>('RMQ_URI'), uri: configService.get<string>('RMQ_URI'),
enableControllerDiscovery: true, enableControllerDiscovery: true,
}), }),

View File

@ -12,9 +12,7 @@ export class AuthController {
) {} ) {}
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingAuthCreateCrit',
routingKey: 'auth.create.crit',
queue: 'logging-auth-create-crit',
}) })
public async authCreatedCriticalHandler(message: string) { public async authCreatedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -24,9 +22,7 @@ export class AuthController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingAuthDeleteCrit',
routingKey: 'auth.delete.crit',
queue: 'logging-auth-delete-crit',
}) })
public async authDeletedCriticalHandler(message: string) { public async authDeletedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -36,9 +32,7 @@ export class AuthController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingAuthUsernameAddWarning',
routingKey: 'auth.username.add.warning',
queue: 'logging-auth-username-add-warning',
}) })
public async authUsernameAddedWarningHandler(message: string) { public async authUsernameAddedWarningHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -48,9 +42,7 @@ export class AuthController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingAuthUsernameDeleteWarning',
routingKey: 'auth.username.delete.warning',
queue: 'logging-auth-username-delete-warning',
}) })
public async authUsernameDeletedWarningHandler(message: string) { public async authUsernameDeletedWarningHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -60,9 +52,7 @@ export class AuthController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingAuthPasswordUpdateWarning',
routingKey: 'auth.password.update.warning',
queue: 'logging-auth-password-update-warning',
}) })
public async authPasswordUpdatedWarningHandler(message: string) { public async authPasswordUpdatedWarningHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -72,9 +62,7 @@ export class AuthController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingAuthUsernameUpdateWarning',
routingKey: 'auth.username.update.warning',
queue: 'logging-auth-username-update-warning',
}) })
public async authUsernameUpdatedWarningHandler(message: string) { public async authUsernameUpdatedWarningHandler(message: string) {
this.logger.configure( this.logger.configure(

View File

@ -12,9 +12,7 @@ export class ConfigurationController {
) {} ) {}
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingConfigurationCreateInfo',
routingKey: 'configuration.create.info',
queue: 'logging-configuration-create-info',
}) })
public async configurationCreatedInfoHandler(message: string) { public async configurationCreatedInfoHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -24,9 +22,7 @@ export class ConfigurationController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingConfigurationCreateWarning',
routingKey: 'configuration.create.warning',
queue: 'logging-configuration-create-warning',
}) })
public async configurationCreatedWarningHandler(message: string) { public async configurationCreatedWarningHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -36,9 +32,7 @@ export class ConfigurationController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingConfigurationCreateCrit',
routingKey: 'configuration.create.crit',
queue: 'logging-configuration-create-crit',
}) })
public async configurationCreatedCriticalHandler(message: string) { public async configurationCreatedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -48,9 +42,7 @@ export class ConfigurationController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingConfigurationDeleteInfo',
routingKey: 'configuration.delete.info',
queue: 'logging-configuration-delete-info',
}) })
public async configurationDeletedInfoHandler(message: string) { public async configurationDeletedInfoHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -60,9 +52,7 @@ export class ConfigurationController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingConfigurationDeleteCrit',
routingKey: 'configuration.delete.crit',
queue: 'logging-configuration-delete-crit',
}) })
public async configurationDeletedCriticalHandler(message: string) { public async configurationDeletedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -72,9 +62,7 @@ export class ConfigurationController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingConfigurationReadWarning',
routingKey: 'configuration.read.warning',
queue: 'logging-configuration-read-warning',
}) })
public async configurationReadWarningHandler(message: string) { public async configurationReadWarningHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -84,9 +72,7 @@ export class ConfigurationController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingConfigurationUpdateInfo',
routingKey: 'configuration.update.info',
queue: 'logging-configuration-update-info',
}) })
public async configurationUpdatedInfoHandler(message: string) { public async configurationUpdatedInfoHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -96,9 +82,7 @@ export class ConfigurationController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingConfigurationUpdateCrit',
routingKey: 'configuration.update.crit',
queue: 'logging-configuration-update-crit',
}) })
public async configurationUpdatedCriticalHandler(message: string) { public async configurationUpdatedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(

View File

@ -12,9 +12,7 @@ export class TerritoryController {
) {} ) {}
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingTerritoryCreateInfo',
routingKey: 'territory.create.info',
queue: 'logging-territory-create-info',
}) })
public async territoryCreatedInfoHandler(message: string) { public async territoryCreatedInfoHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -24,9 +22,7 @@ export class TerritoryController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingTerritoryCreateWarning',
routingKey: 'territory.create.warning',
queue: 'logging-territory-create-warning',
}) })
public async territoryCreatedWarningHandler(message: string) { public async territoryCreatedWarningHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -36,9 +32,7 @@ export class TerritoryController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingTerritoryCreateCrit',
routingKey: 'territory.create.crit',
queue: 'logging-territory-create-crit',
}) })
public async territoryCreatedCriticalHandler(message: string) { public async territoryCreatedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -48,9 +42,7 @@ export class TerritoryController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingTerritoryDeleteInfo',
routingKey: 'territory.delete.info',
queue: 'logging-territory-delete-info',
}) })
public async territoryDeletedInfoHandler(message: string) { public async territoryDeletedInfoHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -60,9 +52,7 @@ export class TerritoryController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingTerritoryDeleteCrit',
routingKey: 'territory.delete.crit',
queue: 'logging-territory-delete-crit',
}) })
public async territoryDeletedCriticalHandler(message: string) { public async territoryDeletedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -72,9 +62,7 @@ export class TerritoryController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingTerritoryReadWarning',
routingKey: 'territory.read.warning',
queue: 'logging-territory-read-warning',
}) })
public async territoryReadWarningHandler(message: string) { public async territoryReadWarningHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -84,9 +72,7 @@ export class TerritoryController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingTerritoryUpdateInfo',
routingKey: 'territory.update.info',
queue: 'logging-territory-update-info',
}) })
public async territoryUpdatedInfoHandler(message: string) { public async territoryUpdatedInfoHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -96,9 +82,7 @@ export class TerritoryController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingTerritoryUpdateCrit',
routingKey: 'territory.update.crit',
queue: 'logging-territory-update-crit',
}) })
public async territoryUpdatedCriticalHandler(message: string) { public async territoryUpdatedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(

View File

@ -12,9 +12,7 @@ export class UserController {
) {} ) {}
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingUserCreateInfo',
routingKey: 'user.create.info',
queue: 'logging-user-create-info',
}) })
public async userCreatedInfoHandler(message: string) { public async userCreatedInfoHandler(message: string) {
this.logger.configure(loggerOptions('user', level.info, 'info', 'create')); this.logger.configure(loggerOptions('user', level.info, 'info', 'create'));
@ -22,9 +20,7 @@ export class UserController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingUserCreateWarning',
routingKey: 'user.create.warning',
queue: 'logging-user-create-warning',
}) })
public async userCreatedWarningHandler(message: string) { public async userCreatedWarningHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -34,9 +30,7 @@ export class UserController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingUserCreateCrit',
routingKey: 'user.create.crit',
queue: 'logging-user-create-crit',
}) })
public async userCreatedCriticalHandler(message: string) { public async userCreatedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -46,9 +40,7 @@ export class UserController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingUserDeleteInfo',
routingKey: 'user.delete.info',
queue: 'logging-user-delete-info',
}) })
public async userDeletedInfoHandler(message: string) { public async userDeletedInfoHandler(message: string) {
this.logger.configure(loggerOptions('user', level.info, 'info', 'delete')); this.logger.configure(loggerOptions('user', level.info, 'info', 'delete'));
@ -56,9 +48,7 @@ export class UserController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingUserDeleteCrit',
routingKey: 'user.delete.crit',
queue: 'logging-user-delete-crit',
}) })
public async userDeletedCriticalHandler(message: string) { public async userDeletedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -68,9 +58,7 @@ export class UserController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingUserReadWarning',
routingKey: 'user.read.warning',
queue: 'logging-user-read-warning',
}) })
public async userReadWarningHandler(message: string) { public async userReadWarningHandler(message: string) {
this.logger.configure( this.logger.configure(
@ -80,9 +68,7 @@ export class UserController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingUserUpdateInfo',
routingKey: 'user.update.info',
queue: 'logging-user-update-info',
}) })
public async userUpdatedInfoHandler(message: string) { public async userUpdatedInfoHandler(message: string) {
this.logger.configure(loggerOptions('user', level.info, 'info', 'update')); this.logger.configure(loggerOptions('user', level.info, 'info', 'update'));
@ -90,9 +76,7 @@ export class UserController {
} }
@RabbitSubscribe({ @RabbitSubscribe({
exchange: 'logging', name: 'loggingUserUpdateCrit',
routingKey: 'user.update.crit',
queue: 'logging-user-update-crit',
}) })
public async userUpdatedCriticalHandler(message: string) { public async userUpdatedCriticalHandler(message: string) {
this.logger.configure( this.logger.configure(