Skip to content

Commit

Permalink
feat: add env checker
Browse files Browse the repository at this point in the history
  • Loading branch information
NedcloarBR committed Dec 29, 2024
1 parent 67d64ef commit a3469db
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/lib/env-checker.ts
Original file line number Diff line number Diff line change
@@ -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<Config["Discord"]>("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<Config["Database"]>("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<Config["TopGGToken"]>("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<Config["API"]>("API");

if (!APIConfig.JwtSecret) {
throw new Error("JWT Secret not found");
}

if (!APIConfig.CookieSecret) {
throw new Error("Cookie Secret not found");
}
}
1 change: 1 addition & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./prisma-utils";
export * from "./sharding";
export * from "./top.gg-autoposter";
export * from "./tracing";
export * from "./env-checker";
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import {
EnvChecker,
NodeHandler,
PrismaExceptionFilter,
ShardingManager,
Expand All @@ -16,6 +17,7 @@ async function bootstrap() {
const configService = app.get<ConfigService>(ConfigService);
const httpAdapter = app.getHttpAdapter();
const logger = new Logger("Main");
EnvChecker(configService);
const ShardManager = new ShardingManager(configService);
const TopGGPoster = new TopGGAutoPoster(
configService.getOrThrow("TopGGToken"),
Expand Down

0 comments on commit a3469db

Please sign in to comment.