|
| 1 | +import os |
| 2 | +from pydantic import BaseModel, Field |
| 3 | +from dotenv import load_dotenv |
| 4 | + |
| 5 | +load_dotenv() |
| 6 | + |
| 7 | +class Settings(BaseModel): |
| 8 | + """Application settings loaded from environment variables.""" |
| 9 | + |
| 10 | + # MongoDB Configuration |
| 11 | + mongo_uri: str = Field(..., description="MongoDB connection URI") |
| 12 | + mongo_database_name: str = Field(default="exosphere-state-manager", description="MongoDB database name") |
| 13 | + state_manager_secret: str = Field(..., description="Secret key for API authentication") |
| 14 | + secrets_encryption_key: str = Field(..., description="Key for encrypting secrets") |
| 15 | + trigger_workers: int = Field(default=1, description="Number of workers to run the trigger cron") |
| 16 | + |
| 17 | + # Cleanup / Retention Configs |
| 18 | + trigger_retention_days: int = Field(default=30, description="How many days to retain old triggers") |
| 19 | + cleanup_interval_minutes: int = Field(default=60, description="Interval (minutes) between cleanup runs") |
| 20 | + |
| 21 | + @classmethod |
| 22 | + def from_env(cls) -> "Settings": |
| 23 | + return cls( |
| 24 | + mongo_uri=os.getenv("MONGO_URI"), # type: ignore |
| 25 | + mongo_database_name=os.getenv("MONGO_DATABASE_NAME", "exosphere-state-manager"), # type: ignore |
| 26 | + state_manager_secret=os.getenv("STATE_MANAGER_SECRET"), # type: ignore |
| 27 | + secrets_encryption_key=os.getenv("SECRETS_ENCRYPTION_KEY"), # type: ignore |
| 28 | + trigger_workers=int(os.getenv("TRIGGER_WORKERS", 1)), # type: ignore |
| 29 | + |
| 30 | + # NEW CONFIGS |
| 31 | + trigger_retention_days=int(os.getenv("TRIGGER_RETENTION_DAYS", 30)), # type: ignore |
| 32 | + cleanup_interval_minutes=int(os.getenv("CLEANUP_INTERVAL_MINUTES", 60)) # type: ignore |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +# Global settings instance - will be updated when get_settings() is called |
| 37 | +_settings = None |
| 38 | + |
| 39 | + |
| 40 | +def get_settings() -> Settings: |
| 41 | + """Get the global settings instance, reloading from environment if needed.""" |
| 42 | + global _settings |
| 43 | + _settings = Settings.from_env() |
| 44 | + return _settings |
| 45 | + |
| 46 | + |
| 47 | +# Initialize settings |
| 48 | +settings = get_settings() |
0 commit comments