Merge branch 'improveHealth' into 'main'

refactor; add health check

See merge request v3/service/territory!14
This commit is contained in:
Sylvain Briat 2023-03-31 08:36:56 +00:00
commit 2231d45546
57 changed files with 85 additions and 25 deletions

View File

@ -3,14 +3,16 @@ import { AutomapperModule } from '@automapper/nestjs';
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
import { ConfigurationModule } from './modules/configuration/configuration.module'; import { ConfigurationModule } from './modules/configuration/configuration.module';
import { TerritoriesModule } from './modules/territories/territories.module'; import { TerritoryModule } from './modules/territory/territory.module';
import { HealthModule } from './modules/health/health.module';
@Module({ @Module({
imports: [ imports: [
ConfigModule.forRoot({ isGlobal: true }), ConfigModule.forRoot({ isGlobal: true }),
AutomapperModule.forRoot({ strategyInitializer: classes() }), AutomapperModule.forRoot({ strategyInitializer: classes() }),
TerritoriesModule, TerritoryModule,
ConfigurationModule, ConfigurationModule,
HealthModule,
], ],
controllers: [], controllers: [],
providers: [], providers: [],

View File

@ -9,11 +9,14 @@ async function bootstrap() {
{ {
transport: Transport.GRPC, transport: Transport.GRPC,
options: { options: {
package: 'territory', package: ['territory', 'health'],
protoPath: join( protoPath: [
join(
__dirname, __dirname,
'modules/territories/adapters/primaries/territory.proto', 'modules/territories/adapters/primaries/territory.proto',
), ),
join(__dirname, 'modules/health/adapters/primaries/health.proto'),
],
url: process.env.SERVICE_URL + ':' + process.env.SERVICE_PORT, url: process.env.SERVICE_URL + ':' + process.env.SERVICE_PORT,
loader: { keepCase: true }, loader: { keepCase: true },
}, },

View File

@ -2,7 +2,7 @@ import { PrismaRepository } from '../adapters/secondaries/prisma-repository.abst
import { ICollection } from '../interfaces/collection.interface'; import { ICollection } from '../interfaces/collection.interface';
import { Point } from './point.type'; import { Point } from './point.type';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { Territory } from 'src/modules/territories/domain/entities/territory'; import { Territory } from 'src/modules/territory/domain/entities/territory';
import { DatabaseException } from '../exceptions/database.exception'; import { DatabaseException } from '../exceptions/database.exception';
export class TerritoryRepository<T> extends PrismaRepository<T> { export class TerritoryRepository<T> extends PrismaRepository<T> {

View File

@ -2,8 +2,8 @@ import { Injectable } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { PrismaService } from '../../src/adapters/secondaries/prisma-service'; import { PrismaService } from '../../src/adapters/secondaries/prisma-service';
import { TerritoryRepository } from '../../src/domain/territory-repository'; import { TerritoryRepository } from '../../src/domain/territory-repository';
import { Territory } from '../../../territories/domain/entities/territory'; import { Territory } from '../../../territory/domain/entities/territory';
import { Point } from '../../../territories/domain/entities/point'; import { Point } from '../../../territory/domain/entities/point';
import { DatabaseException } from '../../src/exceptions/database.exception'; import { DatabaseException } from '../../src/exceptions/database.exception';
const mockTerritories: Array<Territory> = [ const mockTerritories: Array<Territory> = [

View File

@ -0,0 +1,27 @@
import { Controller } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';
enum ServingStatus {
UNKNOWN = 0,
SERVING = 1,
NOT_SERVING = 2,
}
interface HealthCheckRequest {
service: string;
}
interface HealthCheckResponse {
status: ServingStatus;
}
@Controller()
export class HealthController {
@GrpcMethod('Health', 'Check')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
check(data: HealthCheckRequest, metadata: any): HealthCheckResponse {
return {
status: ServingStatus.SERVING,
};
}
}

View File

@ -0,0 +1,21 @@
syntax = "proto3";
package health;
service Health {
rpc Check(HealthCheckRequest) returns (HealthCheckResponse);
}
message HealthCheckRequest {
string service = 1;
}
message HealthCheckResponse {
enum ServingStatus {
UNKNOWN = 0;
SERVING = 1;
NOT_SERVING = 2;
}
ServingStatus status = 1;
}

View File

@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { HealthController } from './adapters/primaries/health.controller';
@Module({
controllers: [HealthController],
})
export class HealthModule {}

View File

@ -6,7 +6,7 @@ import { GrpcMethod, RpcException } from '@nestjs/microservices';
import { Territory } from '../../domain/entities/territory'; import { Territory } from '../../domain/entities/territory';
import { FindAllTerritoriesForPointQuery } from '../../queries/find-all-territories-for-point.query'; import { FindAllTerritoriesForPointQuery } from '../../queries/find-all-territories-for-point.query';
import { TerritoryPresenter } from './territory.presenter'; import { TerritoryPresenter } from './territory.presenter';
import { ICollection } from '../../../database/src/interfaces/collection.interface'; import { ICollection } from '../../../databases/src/interfaces/collection.interface';
import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe'; import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe';
import { FindAllTerritoriesForPointRequest } from '../../domain/dtos/find-all-territories-for-point.request'; import { FindAllTerritoriesForPointRequest } from '../../domain/dtos/find-all-territories-for-point.request';
import { FindAllTerritoriesRequest } from '../../domain/dtos/find-all-territories.request'; import { FindAllTerritoriesRequest } from '../../domain/dtos/find-all-territories.request';
@ -15,7 +15,7 @@ import { FindTerritoryByUuidRequest } from '../../domain/dtos/find-territory-by-
import { FindTerritoryByUuidQuery } from '../../queries/find-territory-by-uuid.query'; import { FindTerritoryByUuidQuery } from '../../queries/find-territory-by-uuid.query';
import { CreateTerritoryRequest } from '../../domain/dtos/create-territory.request'; import { CreateTerritoryRequest } from '../../domain/dtos/create-territory.request';
import { CreateTerritoryCommand } from '../../commands/create-territory.command'; import { CreateTerritoryCommand } from '../../commands/create-territory.command';
import { DatabaseException } from 'src/modules/database/src/exceptions/database.exception'; import { DatabaseException } from 'src/modules/databases/src/exceptions/database.exception';
import { UpdateTerritoryRequest } from '../../domain/dtos/update-territory.request'; import { UpdateTerritoryRequest } from '../../domain/dtos/update-territory.request';
import { UpdateTerritoryCommand } from '../../commands/update-territory.command'; import { UpdateTerritoryCommand } from '../../commands/update-territory.command';
import { DeleteTerritoryCommand } from '../../commands/delete-territory.command'; import { DeleteTerritoryCommand } from '../../commands/delete-territory.command';
@ -29,7 +29,7 @@ import { FindAllTerritoriesForPointsQuery } from '../../queries/find-all-territo
}), }),
) )
@Controller() @Controller()
export class TerritoriesController { export class TerritoryController {
constructor( constructor(
private readonly _commandBus: CommandBus, private readonly _commandBus: CommandBus,
private readonly _queryBus: QueryBus, private readonly _queryBus: QueryBus,

View File

@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { TerritoryRepository } from '../../../database/src/domain/territory-repository'; import { TerritoryRepository } from '../../../databases/src/domain/territory-repository';
import { Territory } from '../../domain/entities/territory'; import { Territory } from '../../domain/entities/territory';
@Injectable() @Injectable()

View File

@ -1,5 +1,5 @@
import { QueryHandler } from '@nestjs/cqrs'; import { QueryHandler } from '@nestjs/cqrs';
import { ICollection } from 'src/modules/database/src/interfaces/collection.interface'; import { ICollection } from 'src/modules/databases/src/interfaces/collection.interface';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { FindAllTerritoriesForPointQuery } from '../../queries/find-all-territories-for-point.query'; import { FindAllTerritoriesForPointQuery } from '../../queries/find-all-territories-for-point.query';
import { Territory } from '../entities/territory'; import { Territory } from '../entities/territory';

View File

@ -1,5 +1,5 @@
import { QueryHandler } from '@nestjs/cqrs'; import { QueryHandler } from '@nestjs/cqrs';
import { ICollection } from 'src/modules/database/src/interfaces/collection.interface'; import { ICollection } from 'src/modules/databases/src/interfaces/collection.interface';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { FindAllTerritoriesForPointsQuery } from '../../queries/find-all-territories-for-points.query'; import { FindAllTerritoriesForPointsQuery } from '../../queries/find-all-territories-for-points.query';
import { Territory } from '../entities/territory'; import { Territory } from '../entities/territory';

View File

@ -1,5 +1,5 @@
import { QueryHandler } from '@nestjs/cqrs'; import { QueryHandler } from '@nestjs/cqrs';
import { ICollection } from 'src/modules/database/src/interfaces/collection.interface'; import { ICollection } from 'src/modules/databases/src/interfaces/collection.interface';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { FindAllTerritoriesQuery } from '../../queries/find-all-territories.query'; import { FindAllTerritoriesQuery } from '../../queries/find-all-territories.query';
import { Territory } from '../entities/territory'; import { Territory } from '../entities/territory';

View File

@ -4,8 +4,8 @@ import { CacheModule, 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 { redisStore } from 'cache-manager-ioredis-yet'; import { redisStore } from 'cache-manager-ioredis-yet';
import { DatabaseModule } from '../database/database.module'; import { DatabaseModule } from '../databases/database.module';
import { TerritoriesController } from './adapters/primaries/territories.controller'; import { TerritoryController } from './adapters/primaries/territory.controller';
import { TerritoriesRepository } from './adapters/secondaries/territories.repository'; import { TerritoriesRepository } from './adapters/secondaries/territories.repository';
import { Messager } from './adapters/secondaries/messager'; import { Messager } from './adapters/secondaries/messager';
import { CreateTerritoryUseCase } from './domain/usecases/create-territory.usecase'; import { CreateTerritoryUseCase } from './domain/usecases/create-territory.usecase';
@ -48,7 +48,7 @@ import { TerritoryProfile } from './mappers/territory.profile';
inject: [ConfigService], inject: [ConfigService],
}), }),
], ],
controllers: [TerritoriesController], controllers: [TerritoryController],
providers: [ providers: [
TerritoryProfile, TerritoryProfile,
TerritoriesRepository, TerritoriesRepository,
@ -63,4 +63,4 @@ import { TerritoryProfile } from './mappers/territory.profile';
], ],
exports: [], exports: [],
}) })
export class TerritoriesModule {} export class TerritoryModule {}

View File

@ -1,7 +1,7 @@
import { TestingModule, Test } from '@nestjs/testing'; import { TestingModule, Test } from '@nestjs/testing';
import { DatabaseModule } from '../../../database/database.module'; import { DatabaseModule } from '../../../databases/database.module';
import { PrismaService } from '../../../database/src/adapters/secondaries/prisma-service'; import { PrismaService } from '../../../databases/src/adapters/secondaries/prisma-service';
import { DatabaseException } from '../../../database/src/exceptions/database.exception'; import { DatabaseException } from '../../../databases/src/exceptions/database.exception';
import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository'; import { TerritoriesRepository } from '../../adapters/secondaries/territories.repository';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import { Territory } from '../../domain/entities/territory'; import { Territory } from '../../domain/entities/territory';

View File

@ -1,5 +1,5 @@
import { ArgumentMetadata } from '@nestjs/common'; import { ArgumentMetadata } from '@nestjs/common';
import { UpdateTerritoryRequest } from '../../../modules/territories/domain/dtos/update-territory.request'; import { UpdateTerritoryRequest } from '../../../modules/territory/domain/dtos/update-territory.request';
import { RpcValidationPipe } from '../../pipes/rpc.validation-pipe'; import { RpcValidationPipe } from '../../pipes/rpc.validation-pipe';
describe('RpcValidationPipe', () => { describe('RpcValidationPipe', () => {