From 2779098b1e6cedb3d486566bc3a5132ec609f8e4 Mon Sep 17 00:00:00 2001 From: geekiot Date: Thu, 30 Oct 2025 18:24:24 +0500 Subject: [PATCH 1/3] Add [redis]: add new env-vars for config, update docs --- src/config/utils.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/config/utils.py b/src/config/utils.py index 8f21dea..19813a2 100644 --- a/src/config/utils.py +++ b/src/config/utils.py @@ -28,6 +28,26 @@ class Settings(BaseSettings): The .env file is located in the project root. Env Vars: + postgres_host (str): + PostgreSQL host name. + Defaults to "postgres". + postgres_port (int): + PostgreSQL port. + Defaults to 5432. + postgres_user (str): + PostgreSQL user name. + postgres_pass (str): + PostgreSQL user password. + postgres_name (str): + PostgreSQL database name. + + redis_host (str): + Redis host name. + Defaults to "redis" + redis_port (int): + Redis port. + Defaults to 6379. + bot_token (str): Telegram API bot token. listen_logging (bool): @@ -51,12 +71,16 @@ class Settings(BaseSettings): @property def database_url(self): + """Get PostgreSQL database url.""" return ( f"postgresql+asyncpg://{self.postgres_user}:" f"{self.postgres_pass}@{self.postgres_host}:" f"{self.postgres_port}/{self.postgres_name}" ) + redis_host: str = Field(frozen=True, default="redis") + redis_port: int = Field(frozen=True, default=6379) + bot_token: str = Field(frozen=True) listen_logging: bool = Field( -- 2.47.3 From 7f3f601aa7270a752ae4cf67104cc87c52996cea Mon Sep 17 00:00:00 2001 From: geekiot Date: Thu, 30 Oct 2025 18:24:56 +0500 Subject: [PATCH 2/3] Add [redis]: add redis image for docker-compose --- docker/docker-compose.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 052ebee..31e8697 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -6,6 +6,9 @@ services: dockerfile: ./docker/Dockerfile command: sh -c "uv run --active --script ./scripts/pybabel.py compile && uv run ./src/main.py" restart: always + depends_on: + - database + - redis database: container_name: ${POSTGRES_HOST} @@ -17,3 +20,15 @@ services: POSTGRES_USER: ${POSTGRES_USER} POSTGRES_PASSWORD: ${POSTGRES_PASS} POSTGRES_DB: ${POSTGRES_NAME} + + redis: + container_name: ${REDIS_HOST} + image: redis:8.2.2 + command: + - redis-server + restart: always + ports: + - 127.0.0.1:${REDIS_PORT}:${REDIS_PORT} + environment: + REDIS_HOST: ${REDIS_HOST} + REDIS_PORT: ${REDIS_PORT} -- 2.47.3 From 293a3c66b9849d6217914cc35325da0dea677b74 Mon Sep 17 00:00:00 2001 From: geekiot Date: Thu, 30 Oct 2025 18:29:02 +0500 Subject: [PATCH 3/3] Add [redis]: add redis FSM storage --- src/bot/__init__.py | 5 ++++- src/main.py | 9 +++++++-- src/redis_client/__init__.py | 22 +++++++++++++++++++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/bot/__init__.py b/src/bot/__init__.py index ae97876..fd3daba 100644 --- a/src/bot/__init__.py +++ b/src/bot/__init__.py @@ -2,6 +2,7 @@ __all__ = ["start_bot"] from aiogram import Bot, Dispatcher +from aiogram.fsm.storage.redis import RedisStorage from loguru import logger as loguru_logger from redis.asyncio.client import Redis from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker @@ -26,7 +27,9 @@ async def start_bot( """ bot = Bot(bot_token) - dispatcher = Dispatcher() + dispatcher = Dispatcher( + storage=RedisStorage(redis=redis_client), + ) include_routers(dispatcher) loguru_logger.debug("Routers have been successfully included.") diff --git a/src/main.py b/src/main.py index c738f16..17de15b 100644 --- a/src/main.py +++ b/src/main.py @@ -3,7 +3,7 @@ import asyncio from bot import start_bot from config import configure_logger, settings from database import create_db, create_session -from redis_client import client +from redis_client import get_redis_client def main() -> None: @@ -24,10 +24,15 @@ def main() -> None: # FIXME: Add argument for create/drop db. asyncio.run(create_db()) + redis_client = get_redis_client( + host=settings.redis_host, + port=settings.redis_port, + ) + asyncio.run( start_bot( bot_token=settings.bot_token, - redis_client=client, + redis_client=redis_client, database_session=session, ) ) diff --git a/src/redis_client/__init__.py b/src/redis_client/__init__.py index c020937..f2425d4 100644 --- a/src/redis_client/__init__.py +++ b/src/redis_client/__init__.py @@ -1,7 +1,23 @@ -__all__ = ["client"] +__all__ = ["get_redis_client"] from redis.asyncio.client import Redis -# TODO: Add redis client. -client: Redis = ... + +def get_redis_client(host: str, port: int) -> Redis: + """ + Get async redis client. + + Args: + host (str): redis host. + port (int): redis port. + + Returns: + Redis: async redis client. + """ + client = Redis( + host=host, + port=port, + ) + + return client -- 2.47.3