-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add health check aware of infrastructure dependencies and conf…
…igure aws load-balancer accordingly
- Loading branch information
Showing
8 changed files
with
217 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { AppService } from './app.service'; | ||
import { | ||
HealthCheck, | ||
HealthCheckService, | ||
TypeOrmHealthIndicator, | ||
} from '@nestjs/terminus'; | ||
import { ControllerResponse } from '@api/types/controller-response.type'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
constructor( | ||
private readonly health: HealthCheckService, | ||
private readonly db: TypeOrmHealthIndicator, | ||
) {} | ||
|
||
@Get() | ||
getHello(): string { | ||
return this.appService.getHello(); | ||
@Get('/') | ||
public root(): ControllerResponse { | ||
return null; | ||
} | ||
|
||
@Get('/health') | ||
@HealthCheck({ noCache: true }) | ||
public checkHealth(): ControllerResponse { | ||
return this.health.check([ | ||
async () => this.db.pingCheck('database', { timeout: 1500 }), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { TestManager } from 'api/test/utils/test-manager'; | ||
|
||
describe('Health', () => { | ||
let testManager: TestManager; | ||
|
||
beforeAll(async () => { | ||
testManager = await TestManager.createTestManager({ logger: false }); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await testManager.clearDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testManager.close(); | ||
}); | ||
|
||
it("Should return the app's health status: OK", async () => { | ||
// Given | ||
// The app is running | ||
|
||
// When | ||
const { statusCode, body } = await testManager.request().get('/health'); | ||
|
||
// Then | ||
expect(statusCode).toBe(200); | ||
expect(body).toStrictEqual({ | ||
status: 'ok', | ||
info: { database: { status: 'up' } }, | ||
error: {}, | ||
details: { database: { status: 'up' } }, | ||
}); | ||
}); | ||
|
||
it("Should return the app's health status: Unavailable", async () => { | ||
// Given | ||
// The app is running and the database becomes unavailable | ||
await testManager.dataSource.destroy(); | ||
|
||
// When | ||
const { statusCode } = await testManager.request().get('/health'); | ||
|
||
// Then | ||
expect(statusCode).toBe(503); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export async function GET() { | ||
try { | ||
const res = await fetch(process.env.NEXT_PUBLIC_API_URL + "/health", { | ||
cache: "no-cache", | ||
}); | ||
if (res.status === 200) { | ||
return new Response("OK", { | ||
status: 200, | ||
headers: { "Cache-Control": "max-age=5, must-revalidate" }, | ||
}); | ||
} else { | ||
return new Response("KO", { | ||
status: 503, | ||
headers: { "Cache-Control": "max-age=5, must-revalidate" }, | ||
}); | ||
} | ||
} catch (error) { | ||
return new Response("KO", { | ||
status: 503, | ||
headers: { "Cache-Control": "max-age=5, must-revalidate" }, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.