persona-finance-tracker-demo/app/config.py
2025-09-13 17:40:23 +05:30

97 lines
2.6 KiB
Python

"""
Application Configuration
This module contains configuration classes for different environments.
Each configuration class inherits from the base Config class and can override
specific settings as needed.
"""
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
class Config:
"""Base configuration class with common settings."""
# Flask settings
SECRET_KEY = os.environ.get('SECRET_KEY') or 'dev-secret-key-change-in-production'
# Database settings
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'postgresql://localhost/personal_finance_dev'
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ECHO = False
# API settings
JSON_SORT_KEYS = False
JSONIFY_PRETTYPRINT_REGULAR = True
class DevelopmentConfig(Config):
"""Development environment configuration."""
DEBUG = True
SQLALCHEMY_ECHO = True # Log SQL queries in development
# Override database URL for development
SQLALCHEMY_DATABASE_URI = os.environ.get('DEV_DATABASE_URL') or \
'postgresql://localhost/personal_finance_dev'
class TestingConfig(Config):
"""Testing environment configuration."""
TESTING = True
WTF_CSRF_ENABLED = False
# Use in-memory SQLite for testing
SQLALCHEMY_DATABASE_URI = os.environ.get('TEST_DATABASE_URL') or \
'sqlite:///:memory:'
class ProductionConfig(Config):
"""Production environment configuration."""
# Ensure SECRET_KEY is set in production
SECRET_KEY = os.environ.get('SECRET_KEY')
if not SECRET_KEY:
raise ValueError("SECRET_KEY environment variable must be set in production")
# Production database URL is required
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
if not SQLALCHEMY_DATABASE_URI:
raise ValueError("DATABASE_URL environment variable must be set in production")
# Production optimizations
SQLALCHEMY_ENGINE_OPTIONS = {
'pool_pre_ping': True,
'pool_recycle': 300,
}
# Configuration mapping
config_map = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'production': ProductionConfig,
'default': DevelopmentConfig
}
def get_config(config_name=None):
"""
Get configuration class for the specified environment.
Args:
config_name (str): Configuration name ('development', 'testing', 'production')
Returns:
Config: Configuration class instance
"""
if config_name is None:
config_name = 'default'
return config_map.get(config_name, DevelopmentConfig)