beatmatchr/backend/config.py
2025-11-10 16:12:43 -05:00

72 lines
2.6 KiB
Python

"""Application configuration utilities."""
from __future__ import annotations
import os
from functools import lru_cache
from typing import Any, Dict
class Settings:
"""Configuration values loaded from environment variables.
The defaults are tuned for local development and match the docker-compose
configuration provided in this repository. Each value can be overridden by
setting the corresponding environment variable before starting the
application.
"""
def __init__(self) -> None:
self.postgres_user: str = os.getenv("POSTGRES_USER", "beatmatchr")
self.postgres_password: str = os.getenv("POSTGRES_PASSWORD", "beatmatchr")
self.postgres_db: str = os.getenv("POSTGRES_DB", "beatmatchr")
self.postgres_host: str = os.getenv("POSTGRES_HOST", "localhost")
self.postgres_port: int = int(os.getenv("POSTGRES_PORT", "5432"))
self.redis_host: str = os.getenv("REDIS_HOST", "localhost")
self.redis_port: int = int(os.getenv("REDIS_PORT", "6379"))
self.redis_db: int = int(os.getenv("REDIS_DB", "0"))
self.database_url: str = os.getenv(
"DATABASE_URL",
(
"postgresql+asyncpg://"
f"{self.postgres_user}:{self.postgres_password}"
f"@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}"
),
)
redis_base_url = f"redis://{self.redis_host}:{self.redis_port}/{self.redis_db}"
self.redis_url: str = os.getenv("REDIS_URL", redis_base_url)
self.celery_broker_url: str = os.getenv("CELERY_BROKER_URL", self.redis_url)
self.celery_result_backend: str = os.getenv(
"CELERY_RESULT_BACKEND", self.redis_url
)
def dict(self) -> Dict[str, Any]:
"""Return the settings as a plain dictionary (useful for debugging)."""
return {
"postgres_user": self.postgres_user,
"postgres_password": self.postgres_password,
"postgres_db": self.postgres_db,
"postgres_host": self.postgres_host,
"postgres_port": self.postgres_port,
"database_url": self.database_url,
"redis_host": self.redis_host,
"redis_port": self.redis_port,
"redis_db": self.redis_db,
"redis_url": self.redis_url,
"celery_broker_url": self.celery_broker_url,
"celery_result_backend": self.celery_result_backend,
}
@lru_cache()
def get_settings() -> Settings:
"""Return a cached instance of :class:`Settings`."""
return Settings()
settings = get_settings()