Skip to content

Commit 54319b1

Browse files
committed
feat: add health check
1 parent 5acff0c commit 54319b1

File tree

7 files changed

+136
-9
lines changed

7 files changed

+136
-9
lines changed

.changeset/config.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88
"@quassel/backend"
99
]
1010
],
11-
"linked": [
12-
[
13-
"@quassel/frontend",
14-
"@quassel/ui"
15-
]
16-
],
11+
"linked": [],
1712
"access": "restricted",
1813
"baseBranch": "main",
1914
"updateInternalDependencies": "patch",

.changeset/forty-crabs-repeat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@quassel/backend": patch
3+
---
4+
5+
Add healthchecks

apps/backend/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ RUN npm install --omit=dev
88

99
# Production image, copy all the files and run nest
1010
FROM docker.io/node:20-alpine AS runner
11-
RUN apk add --no-cache dumb-init
11+
RUN apk add --no-cache dumb-init curl
1212
ENV NODE_ENV=production
1313
ENV PORT=3000
1414
WORKDIR /usr/src/app
@@ -18,4 +18,6 @@ COPY dist/ .
1818
RUN chown -R node:node .
1919
USER node
2020
EXPOSE 3000
21+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
22+
CMD curl --fail http://localhost:3000/health || exit 1
2123
CMD ["dumb-init", "node", "src/main.js"]

apps/backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@nestjs/jwt": "^10.2.0",
4040
"@nestjs/platform-fastify": "^10.4.7",
4141
"@nestjs/swagger": "^8.0.3",
42+
"@nestjs/terminus": "^10.2.3",
4243
"class-transformer": "^0.5.1",
4344
"class-validator": "^0.14.1",
4445
"fastify": "^4.28.1",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Controller, Get } from "@nestjs/common";
2+
import { HealthCheck, HealthCheckService, MikroOrmHealthIndicator } from "@nestjs/terminus";
3+
import { Public } from "../session/public.decorator";
4+
5+
@Controller("health")
6+
export class HealthController {
7+
constructor(
8+
private health: HealthCheckService,
9+
private database: MikroOrmHealthIndicator
10+
) {}
11+
12+
@Get()
13+
@HealthCheck()
14+
@Public()
15+
check() {
16+
return this.health.check([() => this.database.pingCheck("database")]);
17+
}
18+
}

apps/backend/src/system/system.module.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { SessionController } from "./session/session.controller";
88
import { APP_GUARD } from "@nestjs/core";
99
import { SessionGuard } from "./session/session.guard";
1010
import { RolesGuard } from "./users/roles.guard";
11+
import { HealthController } from "./health/health.controller";
12+
import { TerminusModule } from "@nestjs/terminus";
1113

1214
@Module({
13-
imports: [MikroOrmModule.forFeature([User])],
14-
controllers: [UsersController, SessionController],
15+
imports: [MikroOrmModule.forFeature([User]), TerminusModule],
16+
controllers: [UsersController, SessionController, HealthController],
1517
providers: [UsersService, SessionService, { provide: APP_GUARD, useClass: SessionGuard }, { provide: APP_GUARD, useClass: RolesGuard }],
1618
})
1719
export class SystemModule {}

pnpm-lock.yaml

Lines changed: 104 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)