80 lines
2.9 KiB
Python
80 lines
2.9 KiB
Python
"""
|
|
Feature Flag Service for managing application features
|
|
"""
|
|
import os
|
|
from typing import Dict, List, Any
|
|
from app.core.config import settings
|
|
|
|
|
|
class FeatureFlagService:
|
|
"""Service for managing feature flags"""
|
|
|
|
def __init__(self):
|
|
"""Initialize feature flags from environment variables"""
|
|
self._flags = self._load_flags_from_env()
|
|
|
|
def _load_flags_from_env(self) -> Dict[str, Any]:
|
|
"""Load feature flags from environment variables"""
|
|
# Get the FEATURE_FLAGS environment variable (comma-separated list)
|
|
feature_flags_env = os.getenv("FEATURE_FLAGS", "")
|
|
|
|
# Default feature flags - these can be overridden by environment
|
|
default_flags = {
|
|
"CASH_PAYMENT_ENABLED": True,
|
|
"EMAIL_NOTIFICATIONS_ENABLED": True,
|
|
"EVENT_MANAGEMENT_ENABLED": True,
|
|
"AUTO_RENEWAL_ENABLED": False,
|
|
"MEMBERSHIP_TRANSFERS_ENABLED": False,
|
|
"BULK_OPERATIONS_ENABLED": False,
|
|
"ADVANCED_REPORTING_ENABLED": False,
|
|
"API_RATE_LIMITING_ENABLED": True,
|
|
}
|
|
|
|
# Parse environment variable
|
|
flags = default_flags.copy()
|
|
|
|
if feature_flags_env:
|
|
# Parse comma-separated key=value pairs
|
|
for flag_pair in feature_flags_env.split(","):
|
|
flag_pair = flag_pair.strip()
|
|
if "=" in flag_pair:
|
|
key, value = flag_pair.split("=", 1)
|
|
key = key.strip().upper()
|
|
value = value.strip().lower()
|
|
|
|
# Convert string to boolean
|
|
if value in ("true", "1", "yes", "on"):
|
|
flags[key] = True
|
|
elif value in ("false", "0", "no", "off"):
|
|
flags[key] = False
|
|
else:
|
|
# For non-boolean values, keep as string
|
|
flags[key] = value
|
|
|
|
return flags
|
|
|
|
def is_enabled(self, flag_name: str) -> bool:
|
|
"""Check if a feature flag is enabled"""
|
|
flag_name = flag_name.upper()
|
|
return bool(self._flags.get(flag_name, False))
|
|
|
|
def get_flag_value(self, flag_name: str, default: Any = None) -> Any:
|
|
"""Get the value of a feature flag"""
|
|
flag_name = flag_name.upper()
|
|
return self._flags.get(flag_name, default)
|
|
|
|
def get_all_flags(self) -> Dict[str, Any]:
|
|
"""Get all feature flags"""
|
|
return self._flags.copy()
|
|
|
|
def get_enabled_flags(self) -> List[str]:
|
|
"""Get list of enabled feature flag names"""
|
|
return [name for name, value in self._flags.items() if value is True]
|
|
|
|
def reload_flags(self) -> None:
|
|
"""Reload feature flags from environment (useful for runtime updates)"""
|
|
self._flags = self._load_flags_from_env()
|
|
|
|
|
|
# Global instance
|
|
feature_flags = FeatureFlagService() |