refactor(backend): refactor project, add database migrations support

This commit is contained in:
2025-09-24 19:42:04 +02:00
parent b880670929
commit 3c8ad5f74f
33 changed files with 408 additions and 77 deletions

2
backend/app/core/.keep Normal file
View File

@@ -0,0 +1,2 @@
# Konfigurace a utility

View File

4
backend/app/core/base.py Normal file
View File

@@ -0,0 +1,4 @@
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
Base: DeclarativeMeta = declarative_base()

35
backend/app/core/db.py Normal file
View File

@@ -0,0 +1,35 @@
import os
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from app.core.base import Base # Import Base z nového souboru
DATABASE_URL = os.getenv("DATABASE_URL")
if not DATABASE_URL:
mariadb_host = os.getenv("MARIADB_HOST", "localhost")
mariadb_port = os.getenv("MARIADB_PORT", "3306")
mariadb_db = os.getenv("MARIADB_DB", "group_project")
mariadb_user = os.getenv("MARIADB_USER", "root")
mariadb_password = os.getenv("MARIADB_PASSWORD", "strongpassword")
if mariadb_host and mariadb_db and mariadb_user and mariadb_password:
DATABASE_URL = f"mysql+asyncmy://{mariadb_user}:{mariadb_password}@{mariadb_host}:{mariadb_port}/{mariadb_db}"
else:
raise Exception("Only MariaDB is supported. Please set the DATABASE_URL environment variable.")
# Explicitní import všech modelů, aby byly registrovány v Base.metadata
from app.models.user import User
from app.models.transaction import Transaction
from app.models.user import User
# Pokud máš další modely, importuj je zde:
# from app.models.other_model import OtherModel
ssl_enabled = os.getenv("MARIADB_HOST", "localhost") != "localhost"
connect_args = {"ssl": {"ssl": True}} if ssl_enabled else {}
engine = create_async_engine(
DATABASE_URL,
pool_pre_ping=True,
echo=os.getenv("SQL_ECHO", "0") == "1",
connect_args=connect_args,
)
async_session_maker = async_sessionmaker(engine, expire_on_commit=False)

36
backend/app/core/queue.py Normal file
View File

@@ -0,0 +1,36 @@
import json
import os
from typing import Any, Dict
import asyncio
RABBITMQ_URL = os.getenv("RABBITMQ_URL") or (
f"amqp://{os.getenv('RABBITMQ_USERNAME', 'user')}:"
f"{os.getenv('RABBITMQ_PASSWORD', 'bitnami123')}@"
f"{os.getenv('RABBITMQ_HOST', 'localhost')}:"
f"{os.getenv('RABBITMQ_PORT', '5672')}"
)
QUEUE_NAME = os.getenv("MAIL_QUEUE", "mail_queue")
async def _publish_async(message: Dict[str, Any]) -> None:
import aio_pika
connection = await aio_pika.connect_robust(RABBITMQ_URL)
try:
channel = await connection.channel()
await channel.declare_queue(QUEUE_NAME, durable=True)
body = json.dumps(message).encode("utf-8")
await channel.default_exchange.publish(
aio_pika.Message(body=body, delivery_mode=aio_pika.DeliveryMode.PERSISTENT),
routing_key=QUEUE_NAME,
)
finally:
await connection.close()
def enqueue_email(to: str, subject: str, body: str) -> None:
message = {"type": "email", "to": to, "subject": subject, "body": body}
try:
loop = asyncio.get_running_loop()
loop.create_task(_publish_async(message))
except RuntimeError:
asyncio.run(_publish_async(message))
# ...existing code...