From a3469dbbf3331453f96d09362478c2fb96562edf Mon Sep 17 00:00:00 2001 From: NedcloarBR Date: Sat, 28 Dec 2024 23:36:07 -0300 Subject: [PATCH] feat: add env checker --- src/lib/env-checker.ts | 82 ++++++++++++++++++++++++++++++++++++++++++ src/lib/index.ts | 1 + src/main.ts | 2 ++ 3 files changed, 85 insertions(+) create mode 100644 src/lib/env-checker.ts diff --git a/src/lib/env-checker.ts b/src/lib/env-checker.ts new file mode 100644 index 00000000..51048016 --- /dev/null +++ b/src/lib/env-checker.ts @@ -0,0 +1,82 @@ +import type { Config } from "@/modules/config/types"; +import type { ConfigService } from "@nestjs/config"; + +export function EnvChecker(configService: ConfigService) { + const DiscordConfig = configService.get("Discord"); + if (!DiscordConfig) { + throw new Error("Discord config not found"); + } + + if (!DiscordConfig.Client.ID) { + throw new Error("Client ID not found"); + } + + if (!DiscordConfig.Client.Secret) { + throw new Error( + `Client Secret not found | GET a token in https://discord.com/developers/applications/${DiscordConfig.Client.ID}/oauth2`, + ); + } + + if (process.env.NODE_ENV === "PRODUCTION" && !DiscordConfig.Token) { + throw new Error( + `[PRODUCTION] Discord token not found | GET a token in https://discord.com/developers/applications/${DiscordConfig.Client.ID}/bot`, + ); + } + if (process.env.NODE_ENV === "DEVELOPMENT" && !DiscordConfig.DevToken) { + throw new Error( + "[DEVELOPMENT] Discord token not found | GET a token in https://discord.com/developers/applications/${config.Client.ID}/bot", + ); + } + + if (!DiscordConfig.Client.CallbackURL) { + throw new Error("Callback URL not found"); + } + + const DatabaseConfig = configService.get("Database"); + + if (!DatabaseConfig) { + throw new Error("Database config not found"); + } + + if (!DatabaseConfig.URL) { + throw new Error("Database URL not found"); + } + + if (!DatabaseConfig.Name) { + throw new Error("Database Name not found"); + } + + if (!DatabaseConfig.Password) { + throw new Error("Database Password not found"); + } + + // if (!DatabaseConfig.Redis) { + // throw new Error("Redis config not found"); + // } + + // if (!DatabaseConfig.Redis.Host) { + // throw new Error("Redis Host not found"); + // } + + // if (!DatabaseConfig.Redis.Port) { + // throw new Error("Redis Port not found"); + // } + + const TopGGToken = configService.get("TopGGToken"); + + if (!TopGGToken) { + throw new Error( + `TopGG Token not found | GET a token in https://top.gg/bot/${DiscordConfig.Client.ID}/webhooks`, + ); + } + + const APIConfig = configService.get("API"); + + if (!APIConfig.JwtSecret) { + throw new Error("JWT Secret not found"); + } + + if (!APIConfig.CookieSecret) { + throw new Error("Cookie Secret not found"); + } +} diff --git a/src/lib/index.ts b/src/lib/index.ts index 13aa034a..4ce0a98f 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -3,3 +3,4 @@ export * from "./prisma-utils"; export * from "./sharding"; export * from "./top.gg-autoposter"; export * from "./tracing"; +export * from "./env-checker"; diff --git a/src/main.ts b/src/main.ts index 7eae2908..17c1c724 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,6 +3,7 @@ import { ConfigService } from "@nestjs/config"; import { NestFactory } from "@nestjs/core"; import { AppModule } from "./app.module"; import { + EnvChecker, NodeHandler, PrismaExceptionFilter, ShardingManager, @@ -16,6 +17,7 @@ async function bootstrap() { const configService = app.get(ConfigService); const httpAdapter = app.getHttpAdapter(); const logger = new Logger("Main"); + EnvChecker(configService); const ShardManager = new ShardingManager(configService); const TopGGPoster = new TopGGAutoPoster( configService.getOrThrow("TopGGToken"),