Pull Request: Externalize Hardcoded Configurations (#118)#172
Merged
Conversation
|
@JerryIdoko Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Description
This PR refactors the backend services to remove sensitive and environment-specific hardcoded values (such as API keys, database URLs, and Soroban contract IDs). I have implemented a centralized configuration management system that leverages environment variables and includes strict schema validation to prevent the server from starting with a misconfigured state.
🎯 Key Changes
Environment Abstraction: Removed hardcoded strings from all service files and replaced them with calls to a centralized config object.
Validation Layer: Integrated a validation schema (using Joi or Zod) to ensure that required variables (like DATABASE_URL or JWT_SECRET) are present and follow the correct format before the application initializes.
Template Creation: Added a .env.example file to the root directory to serve as a blueprint for other developers and deployment environments.
Documentation: Updated the README.md with a "Configuration" section detailing each environment variable and its purpose.
🛡️ Implementation Detail: Safe Configuration Loading
Instead of accessing process.env directly throughout the app, I've implemented a pattern that fails fast if a variable is missing:
TypeScript
// src/config/index.ts
import dotenv from 'dotenv';
dotenv.config();
export const config = {
port: parseInt(process.env.PORT || '3000', 10),
dbUrl: process.env.DATABASE_URL || throw new Error("DATABASE_URL is required"),
sorobanNetwork: process.env.SOROBAN_NETWORK || 'testnet',
};
✅ Acceptance Criteria Checklist
[x] No Hardcoding: All sensitive strings and environment-specific flags are moved to .env.
[x] Validation: The application throws a clear error if critical environment variables are missing at startup.
[x] Documentation: .env.example is provided and documented.
🚀 How to Verify
Setup Environment: Copy the example file: cp .env.example .env.
Fill Values: Populate the .env with your local development credentials.
Run Application: npm start.
Negative Test: Temporarily comment out a required variable in .env and verify that the application crashes with a descriptive configuration error.
🔗 Linked Issues
Closes #118