generated from geekiot/python-template
feature/redis #8
5 changed files with 69 additions and 6 deletions
|
|
@ -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,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.")
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue