Overview
The Furniture Store Backend uses environment variables to manage configuration across different environments. All configuration is loaded through theconfig.py module, which reads from a .env file using python-dotenv.
Environment Variables
Create a.env file in the project root based on the .env-template file:
.env-template
Required Variables
The entry point for the Flask application. Set to
app.py or run.py.The Flask environment mode. Use
development for local development or production for production deployments.MySQL database username for authentication.
MySQL database password for authentication.
Database server hostname or IP address. Typically
localhost for development or a remote host for production.MySQL database port. Default is
3306.Name of the database to connect to.
Secret key for session management and CSRF protection.In development, if not provided, defaults to
"dev-secret-key-change-in-production".Configuration Structure
Theconfig.py file defines the Config class that loads and manages all configuration settings:
config.py
Key Configuration Options
SQLALCHEMY_DATABASE_URI
SQLALCHEMY_DATABASE_URI
Automatically constructed from individual database variables using the format:This uses the PyMySQL driver for MySQL connectivity.
SQLALCHEMY_TRACK_MODIFICATIONS
SQLALCHEMY_TRACK_MODIFICATIONS
Set to
False to disable Flask-SQLAlchemy’s event notification system, which adds unnecessary overhead and is not needed for most applications.Security Considerations
SECRET_KEY Generation
For production environments, generate a strong secret key:.env file:
Security Best Practices
Use Strong Passwords
Database passwords should be complex and unique for each environment.
Rotate Secrets Regularly
Change
SECRET_KEY and database credentials periodically.Environment Isolation
Use different credentials for development, staging, and production.
Access Control
Restrict database access to only the application servers that need it.
Development vs Production Settings
Development Configuration
.env
In development mode, Flask enables debug mode with automatic reloading and detailed error pages.
Production Configuration
.env
Application Initialization
The configuration is loaded during application creation inapp/__init__.py:
app/__init__.py
Verifying Configuration
You can verify your configuration is loaded correctly by running the application:run.py script includes a database connection test:
run.py
If the configuration is correct, you should see “Database connection successful!” when running the application.
Next Steps
Database Configuration
Learn about MySQL connection setup and SQLAlchemy configuration
Migrations
Set up and manage database migrations with Flask-Migrate