From 4848a9e9394b283022085a6305d00f94b11cd703 Mon Sep 17 00:00:00 2001 From: l3wdfut4pwr Date: Mon, 27 Apr 2026 13:45:09 +0300 Subject: add username change and logout --- app/utils/__init__.py | 17 ---------------- app/utils/cors.py | 3 --- app/utils/create_tables.py | 7 +++++-- app/utils/db.py | 49 ++++++++++++++++++++++++++++++---------------- app/utils/env.py | 5 +++++ 5 files changed, 42 insertions(+), 39 deletions(-) (limited to 'app/utils') diff --git a/app/utils/__init__.py b/app/utils/__init__.py index a05c4d1..e69de29 100644 --- a/app/utils/__init__.py +++ b/app/utils/__init__.py @@ -1,17 +0,0 @@ -from .cors import setup_cors -from .create_tables import init_db -from .db import Base, async_session, engine, get_async_session -from .hash_cfg import hash_password, verify_password -from .logger_cfg import logger - -__all__ = [ - "engine", - "async_session", - "get_async_session", - "Base", - "setup_cors", - "init_db", - "hash_password", - "verify_password", - "logger", -] diff --git a/app/utils/cors.py b/app/utils/cors.py index e7b54e8..71dadbb 100644 --- a/app/utils/cors.py +++ b/app/utils/cors.py @@ -1,10 +1,7 @@ import os -from dotenv import load_dotenv from fastapi.middleware.cors import CORSMiddleware -load_dotenv() - def setup_cors(app): diff --git a/app/utils/create_tables.py b/app/utils/create_tables.py index e438a03..0d2eaa2 100644 --- a/app/utils/create_tables.py +++ b/app/utils/create_tables.py @@ -1,7 +1,10 @@ from app.models.user import Base -from app.utils.db import engine +from app.utils.db import get_engine -async def init_db(): +async def init_db() -> None: + + engine = get_engine() + async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) diff --git a/app/utils/db.py b/app/utils/db.py index 5531998..d3c7318 100644 --- a/app/utils/db.py +++ b/app/utils/db.py @@ -1,7 +1,6 @@ import os -from typing import AsyncGenerator +from typing import AsyncGenerator, Optional -from dotenv import load_dotenv from sqlalchemy.ext.asyncio import ( AsyncEngine, AsyncSession, @@ -10,29 +9,45 @@ from sqlalchemy.ext.asyncio import ( ) from sqlalchemy.orm import DeclarativeBase -load_dotenv() +engine: Optional[AsyncEngine] = None +async_session: Optional[async_sessionmaker[AsyncSession]] = None -DATABASE_URL = os.getenv("DATABASE_URL") -if not DATABASE_URL: - raise ValueError("DATABASE_URL not found in .env") +class Base(DeclarativeBase): + pass -engine: AsyncEngine = create_async_engine( - DATABASE_URL, - echo=True, - pool_pre_ping=True, -) -async_session = async_sessionmaker( - bind=engine, - expire_on_commit=False, -) +def init_db_engine() -> None: + global engine, async_session + database_url = os.getenv("DATABASE_URL") -class Base(DeclarativeBase): - pass + if not database_url: + raise RuntimeError("DATABASE_URL not found in environment") + + engine = create_async_engine( + database_url, + echo=True, + pool_pre_ping=True, + ) + + async_session = async_sessionmaker( + bind=engine, + expire_on_commit=False, + ) + + +def get_engine() -> AsyncEngine: + if engine is None: + raise RuntimeError( + "DB engine not initialized. Call init_db_engine() first." + ) + return engine async def get_async_session() -> AsyncGenerator[AsyncSession, None]: + if async_session is None: + raise RuntimeError("DB not initialized. Call init_db_engine() first.") + async with async_session() as session: yield session diff --git a/app/utils/env.py b/app/utils/env.py index e69de29..76b61a7 100644 --- a/app/utils/env.py +++ b/app/utils/env.py @@ -0,0 +1,5 @@ +from pathlib import Path + +from dotenv import load_dotenv + +load_dotenv(Path(__file__).resolve().parents[2] / ".env") -- cgit v1.3-3-g829e