Spaces:
Sleeping
Sleeping
| """ | |
| Environment Configuration for Semantic Scalpel BSV | |
| Supports dev/staging/prod modes with appropriate defaults. | |
| Environment Variables: | |
| NUANCE_ENV - Environment (dev, staging, prod) | |
| NUANCE_API_URL - Backend API URL | |
| BSV_API_KEY - SimpleBSV API key for blockchain operations | |
| BSV_MODE - BSV mode (simulated, live) | |
| TELEMETRY_ENABLED - Enable telemetry (true/false) | |
| Created by Bryan Daugherty | |
| SmartLedger Blockchain Solutions Inc | |
| """ | |
| import os | |
| from dataclasses import dataclass, field | |
| from typing import Optional | |
| from enum import Enum | |
| class Environment(str, Enum): | |
| DEV = "dev" | |
| STAGING = "staging" | |
| PROD = "prod" | |
| class BSVMode(str, Enum): | |
| SIMULATED = "simulated" # Generate fake TXIDs for demo | |
| LIVE = "live" # Real BSV blockchain transactions | |
| class AppConfig: | |
| """Application configuration with environment-aware defaults.""" | |
| # Environment | |
| env: Environment = Environment.DEV | |
| # API Backend | |
| api_url: str = "http://159.203.35.45:8001" | |
| api_timeout: int = 30 | |
| # BSV Blockchain | |
| bsv_mode: BSVMode = BSVMode.SIMULATED | |
| bsv_api_url: str = "https://simplebsv.codenlighten.org" | |
| bsv_api_key: Optional[str] = None | |
| bsv_sync_mode: bool = False # async by default for speed | |
| # Telemetry | |
| telemetry_enabled: bool = True | |
| telemetry_endpoint: Optional[str] = None | |
| # Rate Limiting | |
| rate_limit_per_minute: int = 60 | |
| rate_limit_burst: int = 10 | |
| # Response Sanitization | |
| confidence_decimals: int = 2 | |
| # Feature Flags | |
| show_bsv_verification: bool = True | |
| show_cost_calculator: bool = True | |
| enable_share_buttons: bool = True | |
| def load_config() -> AppConfig: | |
| """ | |
| Load configuration from environment variables. | |
| Returns: | |
| AppConfig with appropriate settings for the environment | |
| """ | |
| # Determine environment | |
| env_str = os.environ.get("NUANCE_ENV", "dev").lower() | |
| try: | |
| env = Environment(env_str) | |
| except ValueError: | |
| env = Environment.DEV | |
| # Determine BSV mode | |
| bsv_mode_str = os.environ.get("BSV_MODE", "simulated").lower() | |
| try: | |
| bsv_mode = BSVMode(bsv_mode_str) | |
| except ValueError: | |
| bsv_mode = BSVMode.SIMULATED | |
| # Environment-specific defaults | |
| if env == Environment.PROD: | |
| defaults = { | |
| "api_timeout": 30, | |
| "rate_limit_per_minute": 100, | |
| "rate_limit_burst": 20, | |
| "telemetry_enabled": True, | |
| } | |
| elif env == Environment.STAGING: | |
| defaults = { | |
| "api_timeout": 60, | |
| "rate_limit_per_minute": 200, | |
| "rate_limit_burst": 50, | |
| "telemetry_enabled": True, | |
| } | |
| else: # DEV | |
| defaults = { | |
| "api_timeout": 120, | |
| "rate_limit_per_minute": 1000, | |
| "rate_limit_burst": 100, | |
| "telemetry_enabled": False, | |
| } | |
| # Build config | |
| config = AppConfig( | |
| env=env, | |
| api_url=os.environ.get("NUANCE_API_URL", "http://159.203.35.45:8001"), | |
| api_timeout=int(os.environ.get("API_TIMEOUT", defaults["api_timeout"])), | |
| bsv_mode=bsv_mode, | |
| bsv_api_url=os.environ.get("BSV_API_URL", "https://simplebsv.codenlighten.org"), | |
| bsv_api_key=os.environ.get("BSV_API_KEY"), | |
| bsv_sync_mode=os.environ.get("BSV_SYNC_MODE", "false").lower() == "true", | |
| telemetry_enabled=os.environ.get("TELEMETRY_ENABLED", str(defaults["telemetry_enabled"])).lower() == "true", | |
| telemetry_endpoint=os.environ.get("TELEMETRY_ENDPOINT"), | |
| rate_limit_per_minute=int(os.environ.get("RATE_LIMIT_PER_MINUTE", defaults["rate_limit_per_minute"])), | |
| rate_limit_burst=int(os.environ.get("RATE_LIMIT_BURST", defaults["rate_limit_burst"])), | |
| ) | |
| # Validate BSV config for live mode | |
| if config.bsv_mode == BSVMode.LIVE and not config.bsv_api_key: | |
| print("WARNING: BSV_MODE=live but no BSV_API_KEY set. Falling back to simulated.") | |
| config.bsv_mode = BSVMode.SIMULATED | |
| return config | |
| # Global config instance | |
| _config: Optional[AppConfig] = None | |
| def get_config() -> AppConfig: | |
| """Get or create the global config instance.""" | |
| global _config | |
| if _config is None: | |
| _config = load_config() | |
| return _config | |
| def reload_config() -> AppConfig: | |
| """Force reload configuration from environment.""" | |
| global _config | |
| _config = load_config() | |
| return _config | |