From 83c3e938a7537db023fdd0a059d17bb03b4e8034 Mon Sep 17 00:00:00 2001 From: a20688392 Date: Wed, 1 Feb 2023 22:35:35 +0800 Subject: [PATCH] refactor: setup project mode --- .env.example | 2 ++ src/config/env.validation.ts | 18 +++++++++++++++++- src/main.ts | 5 ++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 3817ed2..0c28b07 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,5 @@ +NODE_ENV=production + DB_HOST=127.0.0.1 DB_PORT=3306 DB_USERNAME=cophr diff --git a/src/config/env.validation.ts b/src/config/env.validation.ts index ced883a..e6ff7b6 100644 --- a/src/config/env.validation.ts +++ b/src/config/env.validation.ts @@ -1,7 +1,23 @@ import { plainToInstance } from "class-transformer"; -import { IsNotEmpty, IsNumber, IsString, validateSync } from "class-validator"; +import { + IsEnum, + IsNotEmpty, + IsNumber, + IsString, + validateSync, +} from "class-validator"; + +export enum Environment { + Development = "development", + Production = "production", + Test = "test", + Provision = "provision", +} class EnvironmentVariables { + @IsEnum(Environment) + NODE_ENV: Environment = Environment.Production; + @IsString() @IsNotEmpty() DB_HOST: string; diff --git a/src/main.ts b/src/main.ts index 299a838..350db64 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,11 +4,14 @@ import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger"; import * as dotenv from "dotenv"; import { AppModule } from "./app.module"; +import { Environment } from "./config/env.validation"; dotenv.config(); async function bootstrap() { const app = await NestFactory.create(AppModule); - setupSwagger(app); + if (process.env.NODE_ENV !== Environment.Production) { + setupSwagger(app); + } await app.listen(3000); }