-
Notifications
You must be signed in to change notification settings - Fork 13
Add centralized environment variable validation during application startup #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import dotenv from "dotenv"; | ||
| dotenv.config(); | ||
|
|
||
| const requiredEnv = [ | ||
| "PORT", | ||
| "MONGO_URI", | ||
| "JWT_SECRET", | ||
| "GOOGLE_CLIENT_ID", | ||
| "CLIENT_URL", | ||
| "GEMINI_API_KEY" | ||
| ] | ||
|
|
||
| requiredEnv.forEach((key) => { | ||
| if(!process.env[key]) { | ||
| throw new Error(`Missing required environment variable: ${key}`); | ||
| } | ||
| }) | ||
|
|
||
| export const config = { | ||
| PORT: process.env.PORT, | ||
| MONGO_URI: process.env.MONGO_URI, | ||
| JWT_SECRET: process.env.JWT_SECRET, | ||
| GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID, | ||
| CLIENT_URL: process.env.CLIENT_URL, | ||
| GEMINI_API_KEY: process.env.GEMINI_API_KEY | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| import { PrismaClient } from '@prisma/client'; | ||
| import dotenv from 'dotenv'; | ||
| import path from 'path'; | ||
| import { config } from '../config/env'; | ||
|
|
||
| dotenv.config({ path: path.join(__dirname, '../../.env') }); | ||
|
|
||
| const uri = process.env.MONGO_URI as string; | ||
| const uri = config.MONGO_URI as string; | ||
|
Comment on lines
+4
to
+8
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
printf '\n== file list ==\n'
git ls-files backend/src/scripts/migrate_users.ts backend/src/config/env.ts
printf '\n== outlines ==\n'
ast-grep outline backend/src/scripts/migrate_users.ts --view expanded || true
ast-grep outline backend/src/config/env.ts --view expanded || true
printf '\n== migrate_users.ts ==\n'
cat -n backend/src/scripts/migrate_users.ts | sed -n '1,120p'
printf '\n== env.ts ==\n'
cat -n backend/src/config/env.ts | sed -n '1,220p'Repository: Rishabhworkspace/AlgoForge Length of output: 7399 Load dotenv before importing
🤖 Prompt for AI Agents |
||
|
|
||
| async function migrate() { | ||
| console.log("Starting migration using Prisma engine to bypass DNS issues..."); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Materialize validated values instead of re-reading raw
process.env.This only checks truthiness, so whitespace-only values still pass startup, and
configis rebuilt from raw env strings afterward. That misses the “misconfigured” part of the fail-fast contract. Validate each value once with a helper (for example, rejectvalue.trim() === '') and export those validated results fromconfig.🤖 Prompt for AI Agents