Skip to content

Commit

Permalink
Update local db setup guide and expose health check module (#62)
Browse files Browse the repository at this point in the history
* Update local db setup guide and expose health check module

* fix typing err in readme file

---------

Co-authored-by: NHT <[email protected]>
  • Loading branch information
voanhtuanvn12 and hoangtuan910 authored Mar 5, 2024
1 parent 148ef1c commit 2ed72a6
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
APP_PORT=
FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY=

ENABLE_HEALTHCHECK=true
HEALTH_CHECK_PORT=9991

# DATABASE
DB_TYPE=
DB_HOST=
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# compiled output
/dist
/node_modules
**/sql-scripts/**

# Logs
logs
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

## Node version

v20.10.0

## Description

[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
Expand Down
10 changes: 10 additions & 0 deletions local-db-init/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM mysql:latest

# Set environment variables
ENV MYSQL_ROOT_PASSWORD=mysql
ENV MYSQL_DATABASE=new-2all-dev
ENV MYSQL_USER=local
ENV MYSQL_PASSWORD=mysql

# Copy SQL files into the Docker container
COPY ./sql-scripts/ /docker-entrypoint-initdb.d/
47 changes: 47 additions & 0 deletions local-db-init/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Start db with docker compose

## author

voanhtuanvn12

### Step 1

Ensure that there is a folder called `sql-scripts` that existed (if you want to load data)

### Step 2

Make the root of terminal session at `local-db-init`

```
cd local-db-init
```

### Step 3

Build the docker compose

```
docker compose build
```

### Step 4

Run docker compose up in detach mode

```
docker-compose up -d
```

### Step 5

If you want to delete the mysql container

```
docker-compose down
```

If you also want to remove the volumes associated with the containers, you can add the -v option

```
docker-compose down -v
```
10 changes: 10 additions & 0 deletions local-db-init/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.8'

services:
mysql:
build:
context: .
ports:
- "3306:3306"
volumes:
- ./sql-scripts:/docker-entrypoint-initdb.d
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { CartModule } from './feature/cart/cart.module';
import { RatingAndReviewModule } from './feature/rating-and-review/rating-and-review.module';
import { AhamoveModule } from './dependency/ahamove/ahamove.module';
import { OrderModule } from './feature/order/order.module';
import { HealthCheckController } from './healthcheck/health-check.controller';

@Module({
imports: [
Expand Down Expand Up @@ -51,7 +52,7 @@ import { OrderModule } from './feature/order/order.module';
AhamoveModule,
OrderModule,
],
controllers: [AppController],
controllers: [AppController, HealthCheckController],
providers: [AppService],
})
export class AppModule {}
9 changes: 9 additions & 0 deletions src/healthcheck/health-check.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Controller, Get } from '@nestjs/common';

@Controller('health')
export class HealthCheckController {
@Get()
healthCheck(): string {
return 'Yes, microservice is healthy';
}
}
7 changes: 7 additions & 0 deletions src/healthcheck/health-check.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Module } from '@nestjs/common';
import { HealthCheckController } from './health-check.controller';

@Module({
controllers: [HealthCheckController],
})
export class HealthCheckModule {}
9 changes: 9 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { DEFAULT_PORT } from './constant/config.constant';
import { HealthCheckModule } from './healthcheck/health-check.module';

const appPort = parseInt(process.env.APP_PORT) || DEFAULT_PORT;

Expand All @@ -20,5 +21,13 @@ async function bootstrap() {
//Set timezone
process.env.TZ = 'UTC';
console.log(`The default timezone at ${process.env.TZ}`);
const enableHealthCheck = JSON.parse(process.env.ENABLE_HEALTHCHECK) || false;

// Create a separate HTTP server for health checks
if (enableHealthCheck) {
const healthCheckApp = await NestFactory.create(HealthCheckModule);
const healthCheckPort = parseInt(process.env.HEALTH_CHECK_PORT) || 9991;
await healthCheckApp.listen(healthCheckPort); // Choose a different port for the HTTP server
}
}
bootstrap();

0 comments on commit 2ed72a6

Please sign in to comment.