Skip to content

Centralized Database Configuration Implementation

The iDrv5-MyFR8 application has been successfully updated to use a centralized database configuration system.

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.

1. Created Centralized Configuration System (utils/config.py)

Section titled “1. Created Centralized Configuration System (utils/config.py)”
  • Automatically loads environment variables from .env files
  • 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
  • models/database.py: Now uses centralized config instead of hardcoded URLs
  • utils/database.py: Updated to use centralized configuration
  • utils/database_context.py: Updated to use centralized configuration
  • Now uses centralized configuration for all Flask settings
  • Automatically loads appropriate environment file based on FLASK_ENV

All API files already used SQLAlchemy, so they automatically work with the new system:

  • api/customers_api.py
  • api/rate_cards_api.py
  • api/rate_entries_api.py
  • api/zones_api.py
  • api/service_levels_api.py

Removed hardcoded database URLs from:

  • services/unified_quote_service.py
  • routes/demurrage_routes.py
  • modules/financial_management/services.py
  • modules/ltl_operations/routes.py
  • modules/freight_management/unified_routes.py
  • modules/crm/routes.py
  • modules/admin/pallet_type_routes.py
  • Uses .env.dev file
  • Automatically loaded when FLASK_ENV=development (default)
  • Contains development database credentials
  • Uses .env file
  • Automatically loaded when FLASK_ENV=production
  • Falls back to .env.example if .env doesn’t exist

Development Mode:

Terminal window
FLASK_ENV=development python main.py

Production Mode:

Terminal window
FLASK_ENV=production python main.py
from utils.config import config
# Access database URL
database_url = config.database_url
# Access other settings
app_name = config.system_name
debug_mode = config.flask_debug
  • DATABASE_URL: PostgreSQL connection string
  • FLASK_ENV: Environment mode (development/production)
  • FLASK_DEBUG: Debug mode (True/False)
  • SESSION_SECRET: Session secret key
  • JWT_SECRET_KEY: JWT secret key
  • SYSTEM_NAME: Application name
  • COMPANY_NAME: Company name
  • SERVER_NAME: Server domain name
  • PREFERRED_URL_SCHEME: URL scheme (http/https)
  • UPLOAD_FOLDER: File upload directory
  1. Single Source of Truth: All database connections use the same configuration
  2. Environment Flexibility: Easy switching between development and production
  3. Security: No hardcoded credentials in source code
  4. Maintainability: Configuration changes only need to be made in one place
  5. Validation: Automatic validation of required configuration variables
  • ✅ Configuration loads from .env.dev
  • ✅ Database connections work correctly
  • ✅ APIs respond properly
  • ✅ Rate cards API returns correct data
  • ✅ Customers API returns correct data
  • ✅ Configuration loads from .env.example (fallback)
  • ✅ Debug mode is disabled
  • ✅ Database connections work correctly
  1. Environment Files: .env files are excluded from version control
  2. Fallback Behavior: System falls back to .env.example if production .env is missing
  3. Validation: Required variables are validated at startup
  4. SSL Configuration: Production database connections use SSL by default

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.