Environment Configuration Guide for iDrv5-MyFR8
The iDrv5-MyFR8 application now uses a centralized configuration system that loads settings from environment files.
Overview
Section titled “Overview”The iDrv5-MyFR8 application now uses a centralized configuration system that loads settings from environment files. This provides a single source of truth for database connections and other application settings.
Environment Files
Section titled “Environment Files”Development Environment (.env.dev)
Section titled “Development Environment (.env.dev)”Used for local development and testing.
Production Environment (.env)
Section titled “Production Environment (.env)”Used for production deployments.
Database Configuration
Section titled “Database Configuration”The application uses a single DATABASE_URL environment variable for all database connections. This eliminates the need for hardcoded database URLs throughout the codebase.
Setting up Development Environment
Section titled “Setting up Development Environment”-
Copy the development template:
Terminal window cp .env.dev .env.dev.local -
Update the DATABASE_URL in
.env.dev:DATABASE_URL=postgresql://postgres:YOUR_PASSWORD@YOUR_HOST:5432/YOUR_DATABASE -
Run the application:
Terminal window FLASK_ENV=development python main.py
Setting up Production Environment
Section titled “Setting up Production Environment”-
Create production environment file:
Terminal window cp .env.example .env -
Update the DATABASE_URL in
.env:FLASK_ENV=productionFLASK_DEBUG=FalseDATABASE_URL=postgresql://myfr8_user:your_secure_password@localhost:5432/idrv5_myfr8SESSION_SECRET=your_super_secure_session_secret_here_minimum_32_charactersJWT_SECRET_KEY=your_jwt_secret_key_here_minimum_32_characters -
Run the application:
Terminal window FLASK_ENV=production python main.py
Configuration System
Section titled “Configuration System”Centralized Config Class
Section titled “Centralized Config Class”The utils/config.py file contains a centralized configuration class that:
- Automatically loads the appropriate environment file based on
FLASK_ENV - Validates required configuration variables
- Provides type-safe access to configuration values
- Handles database connection parameters
Usage in Code
Section titled “Usage 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 stringSESSION_SECRET: Secret key for session managementJWT_SECRET_KEY: Secret key for JWT tokens
Optional Variables
Section titled “Optional Variables”FLASK_ENV: Environment mode (development/production)FLASK_DEBUG: Debug mode (True/False)SYSTEM_NAME: Application nameCOMPANY_NAME: Company nameSERVER_NAME: Server domain namePREFERRED_URL_SCHEME: URL scheme (http/https)UPLOAD_FOLDER: File upload directory
Database Connection Examples
Section titled “Database Connection Examples”Local PostgreSQL
Section titled “Local PostgreSQL”DATABASE_URL=postgresql://postgres:password@localhost:5432/idrv5_myfr8Remote PostgreSQL (AWS RDS)
Section titled “Remote PostgreSQL (AWS RDS)”DATABASE_URL=postgresql://username:password@your-rds-endpoint.amazonaws.com:5432/database_nameDocker PostgreSQL
Section titled “Docker PostgreSQL”DATABASE_URL=postgresql://myfr8_user:password@db:5432/idrv5_myfr8Switching Between Environments
Section titled “Switching Between Environments”Method 1: Environment Variable
Section titled “Method 1: Environment Variable”# For developmentexport FLASK_ENV=developmentpython main.py
# For productionexport FLASK_ENV=productionpython main.pyMethod 2: Direct File Selection
Section titled “Method 2: Direct File Selection”The system automatically selects:
.env.devfor development.envfor production
Security Considerations
Section titled “Security Considerations”- Never commit environment files containing real credentials to version control
- Use strong, unique secrets for production deployments
- Regularly rotate secrets in production
- Use SSL/TLS for database connections in production
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”-
“DATABASE_URL not configured”
- Ensure the appropriate environment file exists
- Check that DATABASE_URL is set in the environment file
-
“Missing required environment variables”
- Verify all required variables are present in the environment file
-
Database connection errors
- Check database server is running
- Verify connection parameters are correct
- Ensure database user has proper permissions
Debugging
Section titled “Debugging”Enable debug logging to see which environment file is being loaded:
import logginglogging.basicConfig(level=logging.DEBUG)Migration from Old System
Section titled “Migration from Old System”The new system replaces hardcoded database URLs in:
models/database.pyutils/database.pyutils/database_context.py- API files
All database connections now use the centralized configuration system.
Example Environment Files
Section titled “Example Environment Files”Development (.env.dev)
Section titled “Development (.env.dev)”FLASK_ENV=developmentFLASK_DEBUG=TrueDATABASE_URL=postgresql://postgres:dev_password@localhost:5432/idrv5_devSESSION_SECRET=dev_session_secret_32_characters_minJWT_SECRET_KEY=dev_jwt_secret_32_characters_minSYSTEM_NAME=iDrv5-MyFR8COMPANY_NAME=MyFR8 LogisticsProduction (.env)
Section titled “Production (.env)”FLASK_ENV=productionFLASK_DEBUG=FalseDATABASE_URL=postgresql://myfr8_user:secure_prod_password@prod-db.example.com:5432/idrv5_myfr8SESSION_SECRET=super_secure_session_secret_64_characters_minimum_for_productionJWT_SECRET_KEY=super_secure_jwt_secret_64_characters_minimum_for_productionSYSTEM_NAME=iDrv5-MyFR8COMPANY_NAME=MyFR8 LogisticsSERVER_NAME=your-domain.comPREFERRED_URL_SCHEME=httpsUPLOAD_FOLDER=/opt/idrv5-myfr8/static/uploads