From c3bc6ef9f005ae8f2e1e161e5168489d51ca2617 Mon Sep 17 00:00:00 2001 From: k1nq Date: Sat, 29 Nov 2025 11:34:57 +0500 Subject: [PATCH] feat: enhance configuration for database and Redis integration in Docker setup --- app/core/config.py | 22 +++++++++++++++++++--- docker-compose.yml | 31 +++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app/core/config.py b/app/core/config.py index 08cd870..0e20e16 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -11,9 +11,15 @@ class Settings(BaseSettings): project_name: str = "Test Task CRM" version: str = "0.1.0" api_v1_prefix: str = "/api/v1" - database_url: str = Field( - default="postgresql+asyncpg://postgres:postgres@0.0.0.0:5432/test_task_crm", - description="SQLAlchemy async connection string", + db_host: str = Field(default="localhost", description="Database hostname") + db_port: int = Field(default=5432, description="Database port") + db_name: str = Field(default="test_task_crm", description="Database name") + db_user: str = Field(default="postgres", description="Database user") + db_password: SecretStr = Field(default=SecretStr("postgres"), description="Database user password") + database_url_override: str | None = Field( + default=None, + alias="DATABASE_URL", + description="Optional full SQLAlchemy URL override", ) sqlalchemy_echo: bool = False jwt_secret_key: SecretStr = Field(default=SecretStr("change-me")) @@ -29,5 +35,15 @@ class Settings(BaseSettings): description="Maximum backoff (ms) for retrying cache writes/invalidation", ) + @property + def database_url(self) -> str: + if self.database_url_override: + return self.database_url_override + password = self.db_password.get_secret_value() + return ( + f"postgresql+asyncpg://{self.db_user}:{password}@" + f"{self.db_host}:{self.db_port}/{self.db_name}" + ) + settings = Settings() diff --git a/docker-compose.yml b/docker-compose.yml index cf8d2b3..1ecc8a2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,22 +7,45 @@ services: env_file: - .env environment: - DATABASE_URL: postgresql+asyncpg://postgres:postgres@postgres:5432/test_task_crm + PROJECT_NAME: ${PROJECT_NAME} + VERSION: ${VERSION} + API_V1_PREFIX: ${API_V1_PREFIX} + DB_HOST: ${DB_HOST:-postgres} + DB_PORT: ${DB_PORT} + DB_NAME: ${DB_NAME} + DB_USER: ${DB_USER} + DB_PASSWORD: ${DB_PASSWORD} + SQLALCHEMY_ECHO: ${SQLALCHEMY_ECHO} + JWT_SECRET_KEY: ${JWT_SECRET_KEY} + JWT_ALGORITHM: ${JWT_ALGORITHM} + ACCESS_TOKEN_EXPIRE_MINUTES: ${ACCESS_TOKEN_EXPIRE_MINUTES} + REFRESH_TOKEN_EXPIRE_DAYS: ${REFRESH_TOKEN_EXPIRE_DAYS} + REDIS_ENABLED: ${REDIS_ENABLED} + REDIS_URL: ${REDIS_URL:-redis://redis:6379/0} + ANALYTICS_CACHE_TTL_SECONDS: ${ANALYTICS_CACHE_TTL_SECONDS} + ANALYTICS_CACHE_BACKOFF_MS: ${ANALYTICS_CACHE_BACKOFF_MS} ports: - "8000:8000" depends_on: - postgres + - redis postgres: image: postgres:16-alpine environment: - POSTGRES_DB: test_task_crm - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres + POSTGRES_DB: ${DB_NAME} + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASSWORD} volumes: - postgres_data:/var/lib/postgresql/data ports: - "5432:5432" + redis: + image: redis:7-alpine + command: redis-server --save "" --appendonly no + ports: + - "6379:6379" + volumes: postgres_data: