Skip to content

Commit

Permalink
Lazy load env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
diksipav committed Aug 1, 2024
1 parent 1d4fc0d commit d94b71d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ yarn-error.log*
# local env files
.env*.local

.env.prod
.env.production

# vercel
.vercel
Expand Down
6 changes: 3 additions & 3 deletions app/api/interactions/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import {
InteractionType,
} from "discord-api-types/v10";
import { Bot } from "@/app/lib/discord/bot";
import { discordClientPublicKey, discordToken } from "@/app/envs";
import { getEnvironment } from "../../../envs";
import createClient from "edgedb";
import { verifyInteractionRequest } from "@/app/lib/discord/verify-interaction-request";

const bot = new Bot(createClient(), discordToken);
const bot = new Bot(createClient(), getEnvironment().discordToken);
await bot.initialize();

export async function POST(req: Request) {
const verifyResult = await verifyInteractionRequest(
req,
discordClientPublicKey
getEnvironment().discordClientPublicKey
);

if (!verifyResult.isValid || !verifyResult.interaction) {
Expand Down
30 changes: 0 additions & 30 deletions app/envs.ts

This file was deleted.

10 changes: 7 additions & 3 deletions app/lib/discord/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { REST } from "@discordjs/rest";
import { Routes, APIGuildMember, APIUser } from "discord-api-types/v10";
import { client } from "../../lib/edgedb";
import { discordGuildId, authorizedRoleIds } from "@/app/envs";
import { getEnvironment } from "../../../envs";
import { getCurrentUser } from "./queries/getCurrentUser.query";

export async function discordSignin({
Expand All @@ -18,14 +18,18 @@ export async function discordSignin({
);

const discordUser = (await discordClient.get(
Routes.userGuildMember(discordGuildId)
Routes.userGuildMember(getEnvironment().discordGuildId)
)) as APIGuildMember;

if (!discordUser) {
throw new Error("No guild member information for user");
}

if (!discordUser.roles.some((role) => authorizedRoleIds.includes(role))) {
if (
!discordUser.roles.some((role) =>
getEnvironment().authorizedRoleIds.includes(role)
)
) {
throw new Error("Discord user does not have any authorized roles");
}

Expand Down
4 changes: 2 additions & 2 deletions app/lib/discord/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { REST, RESTOptions } from "@discordjs/rest";
import { Client } from "edgedb";
import { InteractionPromise } from "./interactionPromise";
import { getHelpChannels } from "./queries/getHelpChannels.query";
import { discordClientId } from "@/app/envs";
import { getEnvironment } from "../../../envs";

export class Bot extends REST {
public readonly edgedb: Client;
Expand All @@ -27,7 +27,7 @@ export class Bot extends REST {
constructor(edgedb: Client, token: string, options?: Partial<RESTOptions>) {
super(options);

this.applicationId = discordClientId;
this.applicationId = getEnvironment().discordClientId;

this.edgedb = edgedb;
this.token = token;
Expand Down
5 changes: 4 additions & 1 deletion app/lib/discord/utils/reviewCard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "discord-api-types/v10";
import { Bot } from "../bot";
import { SuggestThreadReturns } from "../queries/suggestThread.query";
import { reviewChannelId } from "@/app/envs";
import { getEnvironment } from "../../../../envs";

const createReviewCard = async (
bot: Bot,
Expand Down Expand Up @@ -34,6 +34,9 @@ const createReviewCard = async (
],
color: 0x0ccb93,
};

const reviewChannelId = getEnvironment().reviewChannelId;

const message = (await bot.post(Routes.channelMessages(reviewChannelId), {
body: {
embeds: [embed],
Expand Down
58 changes: 58 additions & 0 deletions envs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export interface Environment {
discordClientId: string;
discordClientPublicKey: string;
discordToken: string;
discordGuildId: string;
authorizedRoleIds: string[];
reviewChannelId: string;
}

let environment: Environment | null = null;

export function getEnvironment(): Environment {
if (environment) {
return environment;
}

if (!process.env.DISCORD_CLIENT_ID) {
throw new Error("DISCORD_CLIENT_ID env var not configured");
}
const discordClientId = process.env.DISCORD_CLIENT_ID;

if (!process.env.DISCORD_CLIENT_PUBLIC_KEY) {
throw new Error("DISCORD_CLIENT_PUBLIC_KEY env var not configured");
}
const discordClientPublicKey = process.env.DISCORD_CLIENT_PUBLIC_KEY;

if (!process.env.DISCORD_TOKEN) {
throw new Error(`DISCORD_TOKEN env var not configured`);
}
const discordToken = process.env.DISCORD_TOKEN;

if (!process.env.DISCORD_GUILD_ID) {
throw new Error("DISCORD_GUILD_ID env var not configured");
}
const discordGuildId = process.env.DISCORD_GUILD_ID;

if (!process.env.DISCORD_MODERATION_ACCESS_ROLES) {
throw new Error("DISCORD_MODERATION_ACCESS_ROLES env var not configured");
}
const authorizedRoleIds =
process.env.DISCORD_MODERATION_ACCESS_ROLES.split(",");

if (!process.env.REVIEW_CHANNEL_ID) {
throw new Error("REVIEW_CHANNEL_ID env var not configured");
}
const reviewChannelId = process.env.REVIEW_CHANNEL_ID;

environment = {
discordClientId,
discordClientPublicKey,
discordToken,
discordGuildId,
authorizedRoleIds,
reviewChannelId,
};

return environment;
}

0 comments on commit d94b71d

Please sign in to comment.