Skip to content

Conversation

@Raafay-Qureshi
Copy link
Contributor

Description

This Pull Request sets up a centralized environment configuration system that validates all required variables when the app starts. Instead of scattering process.env calls throughout the codebase, everything now goes through a single validated config module with TypeScript type safety.

The config system supports development, production, and test environments, and includes helper functions to check which environment we're running in.

Changes Made

  • Created env.config.ts with TypeScript Config interface
  • Implemented validation that fails fast if required variables are missing
  • Added PORT validation (must be valid number 0-65535)
  • Added support for all required environment variables:
    • Server: PORT, NODE_ENV
    • Database: MONGODB_URI
    • Authentication: JWT_SECRET, JWT_EXPIRES_IN
    • CORS: ALLOWED_ORIGINS
    • Email: EMAIL_SERVICE, EMAIL_USER, EMAIL_PASS
  • Updated all existing files in backend/src to use centralized config
  • Removed duplicate dotenv.config() calls
  • Added helper functions: isDevelopment(), isProduction(), isTest()
  • Updated .env and .env.example with all required variables

Testing

Manually tested:

  • App starts normally with valid config
  • App crashes immediately with helpful error when variables are missing
  • PORT validation catches invalid values like "abc" or negative numbers
  • Production mode correctly uses specified CORS origins
  • Development mode allows all localhost requests

Files Modified

  • backend/src/config/env.config.ts (new)
  • backend/src/config/db.ts
  • backend/src/index.ts
  • backend/src/middleware/errorMiddleware.ts
  • backend/src/routes/statusRoutes.ts
  • backend/src/routes/index.ts
  • backend/.env
  • backend/.env.example

Closes #95

@Raafay-Qureshi Raafay-Qureshi added the backend 🔧 Any issue related to backend development label Oct 23, 2025
@Raafay-Qureshi Raafay-Qureshi linked an issue Oct 23, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend 🔧 Any issue related to backend development

Projects

None yet

Development

Successfully merging this pull request may close these issues.

⚙️ Set up environment configuration system

1 participant