Centralized Database Configuration Implementation
The iDrv5-MyFR8 application has been successfully updated to use a centralized database configuration system.
Overview
Section titled “Overview”The iDrv5-MyFR8 application has been successfully updated to use a centralized database configuration system. This eliminates hardcoded database URLs throughout the codebase and provides a single source of truth for database connections.
Key Changes Made
Section titled “Key Changes Made”1. Created Centralized Configuration System (utils/config.py)
Section titled “1. Created Centralized Configuration System (utils/config.py)”- Automatically loads environment variables from
.envfiles - Supports both development (
.env.dev) and production (.env) environments - Validates required configuration variables
- Provides type-safe access to configuration values
- Handles database connection parameters optimized for PostgreSQL
2. Updated Core Database Files
Section titled “2. Updated Core Database Files”models/database.py: Now uses centralized config instead of hardcoded URLsutils/database.py: Updated to use centralized configurationutils/database_context.py: Updated to use centralized configuration
3. Updated Main Application (main.py)
Section titled “3. Updated Main Application (main.py)”- Now uses centralized configuration for all Flask settings
- Automatically loads appropriate environment file based on
FLASK_ENV
4. Updated API Files
Section titled “4. Updated API Files”All API files already used SQLAlchemy, so they automatically work with the new system:
api/customers_api.pyapi/rate_cards_api.pyapi/rate_entries_api.pyapi/zones_api.pyapi/service_levels_api.py
5. Updated Module Files
Section titled “5. Updated Module Files”Removed hardcoded database URLs from:
services/unified_quote_service.pyroutes/demurrage_routes.pymodules/financial_management/services.pymodules/ltl_operations/routes.pymodules/freight_management/unified_routes.pymodules/crm/routes.pymodules/admin/pallet_type_routes.py
Environment Configuration
Section titled “Environment Configuration”Development Environment
Section titled “Development Environment”- Uses
.env.devfile - Automatically loaded when
FLASK_ENV=development(default) - Contains development database credentials
Production Environment
Section titled “Production Environment”- Uses
.envfile - Automatically loaded when
FLASK_ENV=production - Falls back to
.env.exampleif.envdoesn’t exist
Starting the Application
Section titled “Starting the Application”Development Mode:
FLASK_ENV=development python main.pyProduction Mode:
FLASK_ENV=production python main.pyConfiguration Access in Code
Section titled “Configuration Access in Code”from utils.config import config
# Access database URLdatabase_url = config.database_url
# Access other settingsapp_name = config.system_namedebug_mode = config.flask_debugEnvironment Variables
Section titled “Environment Variables”Required Variables
Section titled “Required Variables”DATABASE_URL: PostgreSQL connection string
Optional Variables
Section titled “Optional Variables”FLASK_ENV: Environment mode (development/production)FLASK_DEBUG: Debug mode (True/False)SESSION_SECRET: Session secret keyJWT_SECRET_KEY: JWT secret keySYSTEM_NAME: Application nameCOMPANY_NAME: Company nameSERVER_NAME: Server domain namePREFERRED_URL_SCHEME: URL scheme (http/https)UPLOAD_FOLDER: File upload directory
Benefits
Section titled “Benefits”- Single Source of Truth: All database connections use the same configuration
- Environment Flexibility: Easy switching between development and production
- Security: No hardcoded credentials in source code
- Maintainability: Configuration changes only need to be made in one place
- Validation: Automatic validation of required configuration variables
Testing Results
Section titled “Testing Results”Development Mode
Section titled “Development Mode”- ✅ Configuration loads from
.env.dev - ✅ Database connections work correctly
- ✅ APIs respond properly
- ✅ Rate cards API returns correct data
- ✅ Customers API returns correct data
Production Mode
Section titled “Production Mode”- ✅ Configuration loads from
.env.example(fallback) - ✅ Debug mode is disabled
- ✅ Database connections work correctly
Security Considerations
Section titled “Security Considerations”- Environment Files:
.envfiles are excluded from version control - Fallback Behavior: System falls back to
.env.exampleif production.envis missing - Validation: Required variables are validated at startup
- SSL Configuration: Production database connections use SSL by default
Conclusion
Section titled “Conclusion”The centralized database configuration system has been successfully implemented and tested. The application now uses a single DATABASE_URL environment variable for all database connections, making it easy to switch between development and production environments while maintaining security and maintainability.