diff --git a/.env.example b/.env.example index f297c23..95edd4e 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,9 @@ APP_PORT= FLAGSMITH_SERVER_SIDE_ENVIRONMENT_KEY= +ENABLE_HEALTHCHECK=true +HEALTH_CHECK_PORT=9991 + # DATABASE DB_TYPE= DB_HOST= diff --git a/.gitignore b/.gitignore index b526704..78b29eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # compiled output /dist /node_modules +**/sql-scripts/** # Logs logs diff --git a/README.md b/README.md index 1fca135..9909f9f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ +## Node version + +v20.10.0 + ## Description [Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. diff --git a/local-db-init/Dockerfile b/local-db-init/Dockerfile new file mode 100644 index 0000000..ed19d60 --- /dev/null +++ b/local-db-init/Dockerfile @@ -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/ \ No newline at end of file diff --git a/local-db-init/README.md b/local-db-init/README.md new file mode 100644 index 0000000..2a7992b --- /dev/null +++ b/local-db-init/README.md @@ -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 +``` diff --git a/local-db-init/docker-compose.yaml b/local-db-init/docker-compose.yaml new file mode 100644 index 0000000..dcdf77c --- /dev/null +++ b/local-db-init/docker-compose.yaml @@ -0,0 +1,10 @@ +version: '3.8' + +services: + mysql: + build: + context: . + ports: + - "3306:3306" + volumes: + - ./sql-scripts:/docker-entrypoint-initdb.d \ No newline at end of file diff --git a/src/app.module.ts b/src/app.module.ts index 0ff5106..874590e 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -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: [ @@ -51,7 +52,7 @@ import { OrderModule } from './feature/order/order.module'; AhamoveModule, OrderModule, ], - controllers: [AppController], + controllers: [AppController, HealthCheckController], providers: [AppService], }) export class AppModule {} diff --git a/src/healthcheck/health-check.controller.ts b/src/healthcheck/health-check.controller.ts new file mode 100644 index 0000000..bf8c558 --- /dev/null +++ b/src/healthcheck/health-check.controller.ts @@ -0,0 +1,9 @@ +import { Controller, Get } from '@nestjs/common'; + +@Controller('health') +export class HealthCheckController { + @Get() + healthCheck(): string { + return 'Yes, microservice is healthy'; + } +} diff --git a/src/healthcheck/health-check.module.ts b/src/healthcheck/health-check.module.ts new file mode 100644 index 0000000..8096e8d --- /dev/null +++ b/src/healthcheck/health-check.module.ts @@ -0,0 +1,7 @@ +import { Module } from '@nestjs/common'; +import { HealthCheckController } from './health-check.controller'; + +@Module({ + controllers: [HealthCheckController], +}) +export class HealthCheckModule {} diff --git a/src/main.ts b/src/main.ts index f1f966f..d902674 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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; @@ -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();