Skip to content

Commit

Permalink
refactor(apps): Adopt Nest monorepo structure to allow module sharing (
Browse files Browse the repository at this point in the history
…#1078)

* refactor(apps): adopt Nest monorepo structure for code sharing between apps

* refactor(apps): point clean script to apps/dist

* refactor(apps): remove redundant paths from tsconfig

* refactor(apps): generalise tsconfig and jest moduleNameMapper

* refactor(apps): remove redundant compilerOptions

* refactor(apps): reinstate paths mapping in compilerOptions so IDE doesn't complain

* chore(apps): bump typeorm

* chore(apps): add testcontainers devDependency

- accidentally dropped from merging main into branch

* refactor(apps): update package-lock.json

* refactor(apps): remove website from lerna packages

* refactor(apps): change apps library prefix

* refactor(apps): move IDEA ide .run config into .idea

* refactor(apps): remove unnecessary start commands
  • Loading branch information
eli-lim authored Feb 28, 2022
1 parent 2fe9736 commit a7375c9
Show file tree
Hide file tree
Showing 28 changed files with 2,334 additions and 1,436 deletions.
24 changes: 0 additions & 24 deletions apps/legacy-api/package.json

This file was deleted.

10 changes: 0 additions & 10 deletions apps/legacy-api/tsconfig.build.json

This file was deleted.

205 changes: 205 additions & 0 deletions apps/libs/actuator/__tests__/ActuatorController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import { Test } from '@nestjs/testing'
import { HealthIndicatorResult } from '@nestjs/terminus'
import { ActuatorController, ActuatorModule, ActuatorProbes, ProbeIndicator } from '../src'
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify'
import { Module } from '@nestjs/common'

describe('ActuatorController - Healthy service', () => {
/**
* Simulate a service that has dead simple health indicators
*/
class HealthyServiceProbe extends ProbeIndicator {
async liveness (): Promise<HealthIndicatorResult> {
return this.withAlive('healthyService', { livenessDetail: 100 })
}

async readiness (): Promise<HealthIndicatorResult> {
return this.withAlive('healthyService', { readinessDetail: 200 })
}
}

/**
* Some module whose health needs to be probed.
* onApplicationBootstrap hook gives us the appropriate app lifecycle stage
* to register the probe.
*/
@Module({ providers: [HealthyServiceProbe] })
class HealthyServiceActuatorModule {
constructor (
private readonly probes: ActuatorProbes,
private readonly probe: HealthyServiceProbe) {
}

async onApplicationBootstrap (): Promise<void> {
this.probes.add(this.probe)
}
}

let app: NestFastifyApplication

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
HealthyServiceActuatorModule,
ActuatorModule
],
providers: [HealthyServiceProbe],
controllers: [ActuatorController]
}).compile()

app = moduleRef.createNestApplication<NestFastifyApplication>(new FastifyAdapter({ logger: false }))
await app.init()
})

afterAll(async () => {
await app.close()
})

it('/_actuator/probes/liveness healthy', async () => {
const response = await app.inject({
method: 'GET',
url: '/_actuator/probes/liveness'
})

expect(response.json()).toStrictEqual({
details: {
healthyService: {
livenessDetail: 100,
status: 'up'
}
},
error: {},
info: {
healthyService: {
livenessDetail: 100,
status: 'up'
}
},
status: 'ok'
})
})

it('/_actuator/probes/readiness healthy', async () => {
const response = await app.inject({
method: 'GET',
url: '/_actuator/probes/readiness'
})

expect(response.json()).toStrictEqual({
details: {
healthyService: {
readinessDetail: 200,
status: 'up'
}
},
error: {},
info: {
healthyService: {
readinessDetail: 200,
status: 'up'
}
},
status: 'ok'
})
})
})

describe('ActuatorController - Unhealthy service', () => {
/**
* Simulate a service that has dead simple health indicators
*/
class UnhealthyServiceProbe extends ProbeIndicator {
async liveness (): Promise<HealthIndicatorResult> {
return this.withDead('unhealthyService', 'some causative message', { livenessDetail: 0 })
}

async readiness (): Promise<HealthIndicatorResult> {
return this.withDead('unhealthyService', 'some causative message', { readinessDetail: 1 })
}
}

/**
* Some module whose health needs to be probed.
* onApplicationBootstrap hook gives us the appropriate app lifecycle stage
* to register the probe.
*/
@Module({ providers: [UnhealthyServiceProbe] })
class UnhealthyServiceActuatorModule {
constructor (
private readonly probes: ActuatorProbes,
private readonly probe: UnhealthyServiceProbe) {
}

async onApplicationBootstrap (): Promise<void> {
this.probes.add(this.probe)
}
}

let app: NestFastifyApplication

beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
UnhealthyServiceActuatorModule,
ActuatorModule
],
providers: [UnhealthyServiceProbe],
controllers: [ActuatorController]
}).compile()

app = moduleRef.createNestApplication<NestFastifyApplication>(new FastifyAdapter({ logger: false }))
await app.init()
})

afterAll(async () => {
await app.close()
})

it('/_actuator/probes/liveness unhealthy', async () => {
const response = await app.inject({
method: 'GET',
url: '/_actuator/probes/liveness'
})

expect(response.json()).toStrictEqual({
details: {
unhealthyService: {
livenessDetail: 0,
status: 'down'
}
},
error: {
unhealthyService: {
livenessDetail: 0,
status: 'down'
}
},
info: {},
status: 'error'
})
})

it('/_actuator/probes/readiness unhealthy', async () => {
const response = await app.inject({
method: 'GET',
url: '/_actuator/probes/readiness'
})

expect(response.json()).toStrictEqual({
details: {
unhealthyService: {
readinessDetail: 1,
status: 'down'
}
},
error: {
unhealthyService: {
readinessDetail: 1,
status: 'down'
}
},
info: {},
status: 'error'
})
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get } from '@nestjs/common'
import { HealthCheck, HealthCheckResult, HealthCheckService } from '@nestjs/terminus'
import { ActuatorProbes } from '../modules/ActuatorModule'
import { ActuatorProbes } from './ActuatorModule'

@Controller('/_actuator')
export class ActuatorController {
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions apps/libs/actuator/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './ActuatorModule'
export * from './ActuatorController'
export * from './BlockchainCppModule'
9 changes: 9 additions & 0 deletions apps/libs/actuator/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "../../dist/libs/actuator"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*test.ts"]
}
36 changes: 36 additions & 0 deletions apps/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"collection": "@nestjs/schematics",
"monorepo": true,
"compilerOptions": {
"tsConfigPath": "tsconfig.json"
},
"projects": {
"legacy-api": {
"type": "application",
"root": "legacy-api",
"entryFile": "apps/legacy-api/src/index",
"sourceRoot": "legacy-api/src"
},
"ocean-api": {
"type": "application",
"root": "ocean-api",
"entryFile": "apps/ocean-api/src/index",
"sourceRoot": "ocean-api/src"
},
"rich-list-api": {
"type": "application",
"root": "rich-list-api",
"entryFile": "apps/rich-list-api/src/index",
"sourceRoot": "rich-list-api/src"
},
"actuator": {
"type": "library",
"root": "libs/actuator",
"entryFile": "index",
"sourceRoot": "libs/actuator/src",
"compilerOptions": {
"tsConfigPath": "libs/actuator/tsconfig.lib.json"
}
}
}
}
37 changes: 0 additions & 37 deletions apps/ocean-api/package.json

This file was deleted.

37 changes: 0 additions & 37 deletions apps/ocean-api/src/controllers/ActuatorController.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/ocean-api/src/modules/ControllerModule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CacheModule, Module } from '@nestjs/common'
import { ActuatorController } from '../controllers/ActuatorController'
import { ActuatorController } from '@defichain-apps/libs/actuator'
import { APP_FILTER, APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core'
import { GlobalValidationPipe } from '../controllers/filters/GlobalValidationPipe'
import { ResponseInterceptor } from '../controllers/filters/ResponseInterceptor'
Expand Down
3 changes: 1 addition & 2 deletions apps/ocean-api/src/modules/RootModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as Joi from 'joi'
import { Module } from '@nestjs/common'
import { ConfigModule } from '@nestjs/config'
import { ControllerModule } from './ControllerModule'
import { BlockchainCppModule } from './BlockchainCppModule'
import { ActuatorModule } from './ActuatorModule'
import { ActuatorModule, BlockchainCppModule } from '@defichain-apps/libs/actuator'
import { PlaygroundModule } from './PlaygroundModule'

@Module({
Expand Down
Loading

0 comments on commit a7375c9

Please sign in to comment.