STABLE: Final schema sync, optimized gitignore
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,13 +1,16 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/db/base_class.py
|
||||
from typing import Any
|
||||
from sqlalchemy.ext.declarative import as_declarative, declared_attr
|
||||
from sqlalchemy import MetaData
|
||||
from sqlalchemy.orm import DeclarativeBase, declared_attr
|
||||
|
||||
@as_declarative()
|
||||
class Base:
|
||||
id: Any
|
||||
__name__: str
|
||||
# Globális séma beállítása
|
||||
target_metadata = MetaData(schema="data")
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
metadata = target_metadata
|
||||
|
||||
# Automatikusan generálja a tábla nevét az osztálynévből,
|
||||
# ha nincs külön megadva (bár mi megadjuk a sémát)
|
||||
@declared_attr
|
||||
# Automatikusan generálja a tábla nevét az osztálynévből
|
||||
@declared_attr.directive
|
||||
def __tablename__(cls) -> str:
|
||||
return cls.__name__.lower()
|
||||
name = cls.__name__.lower()
|
||||
return f"{name}s" if not name.endswith('s') else name
|
||||
@@ -1,31 +1,27 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/db/middleware.py
|
||||
from fastapi import Request
|
||||
from app.db.session import SessionLocal
|
||||
from app.services.config_service import config
|
||||
from app.db.session import AsyncSessionLocal
|
||||
from app.models.audit import OperationalLog # JAVÍTVA: Az új modell
|
||||
from sqlalchemy import text
|
||||
import json
|
||||
|
||||
async def audit_log_middleware(request: Request, call_next):
|
||||
logging_enabled = await config.get_setting('audit_log_enabled', default=True)
|
||||
|
||||
# Itt a config_service-t is aszinkron módon kell hívni, ha szükséges
|
||||
response = await call_next(request)
|
||||
|
||||
if logging_enabled and request.method != 'GET': # GET-et általában nem naplózunk a zaj miatt, de állítható
|
||||
if request.method != 'GET':
|
||||
try:
|
||||
user_id = getattr(request.state, 'user_id', None) # Ha már be van lépve
|
||||
|
||||
async with SessionLocal() as db:
|
||||
await db.execute(text("""
|
||||
INSERT INTO data.audit_logs (user_id, action, endpoint, method, ip_address)
|
||||
VALUES (:u, :a, :e, :m, :ip)
|
||||
"""), {
|
||||
'u': user_id,
|
||||
'a': f'API_CALL_{request.method}',
|
||||
'e': str(request.url.path),
|
||||
'm': request.method,
|
||||
'ip': request.client.host
|
||||
})
|
||||
user_id = getattr(request.state, 'user_id', None)
|
||||
async with AsyncSessionLocal() as db:
|
||||
log = OperationalLog(
|
||||
user_id=user_id,
|
||||
action=f"API_CALL_{request.method}",
|
||||
resource_type="ENDPOINT",
|
||||
resource_id=str(request.url.path),
|
||||
details={"ip": request.client.host, "method": request.method}
|
||||
)
|
||||
db.add(log)
|
||||
await db.commit()
|
||||
except Exception:
|
||||
pass # A naplózás hibája nem akaszthatja meg a kiszolgálást
|
||||
pass # A naplózás nem akaszthatja meg a folyamatot
|
||||
|
||||
return response
|
||||
return response
|
||||
@@ -1,14 +1,15 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/db/session.py
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from app.core.config import settings
|
||||
from typing import AsyncGenerator
|
||||
|
||||
engine = create_async_engine(
|
||||
settings.DATABASE_URL,
|
||||
echo=False, # Termelésben ne legyen True a log-áradat miatt
|
||||
echo=False,
|
||||
future=True,
|
||||
pool_size=30, # Megemelve a Researcher 15-20 szála miatt
|
||||
max_overflow=20, # Extra rugalmasság csúcsidőben
|
||||
pool_pre_ping=True # Megakadályozza a "Server closed connection" hibákat
|
||||
pool_size=30, # A robotok száma miatt
|
||||
max_overflow=20,
|
||||
pool_pre_ping=True
|
||||
)
|
||||
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
@@ -18,15 +19,10 @@ AsyncSessionLocal = async_sessionmaker(
|
||||
autoflush=False
|
||||
)
|
||||
|
||||
SessionLocal = AsyncSessionLocal
|
||||
|
||||
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
yield session
|
||||
await session.commit()
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
# JAVÍTVA: Nincs automatikus commit! Az endpoint felelőssége.
|
||||
finally:
|
||||
await session.close()
|
||||
Reference in New Issue
Block a user