feat: add base FastAPI project structure with static frontend and API v1

This commit is contained in:
Lino Mallevaey
2025-08-18 00:22:50 +02:00
parent ff5026b8f7
commit 49bcb38261
14 changed files with 487 additions and 13 deletions

View File

@@ -0,0 +1,10 @@
"""
Database package for MokPyo application.
- Expose engine and session objects for easy imports
"""
__version__ = "1.0.0"
from .engine import engine, async_engine
from .session import SessionLocal, AsyncSessionLocal, get_db, get_async_db

View File

@@ -0,0 +1,24 @@
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine
from sqlalchemy import create_engine
from app.core.config import settings
# =========================
# SYNCHRONOUS ENGINE
# =========================
# Pour opérations sync classiques
engine = create_engine(
settings.DATABASE_URL.replace("+aiomysql", ""), # remove async part if present
echo=settings.DEBUG,
future=True
)
# =========================
# ASYNCHRONOUS ENGINE
# =========================
# Pour opérations async avec async SQLAlchemy
async_engine: AsyncEngine = create_async_engine(
settings.DATABASE_URL if "+aiomysql" in settings.DATABASE_URL else settings.DATABASE_URL.replace("mysql+pymysql", "mysql+aiomysql"),
echo=settings.DEBUG,
future=True
)

View File

@@ -0,0 +1,45 @@
from typing import AsyncGenerator, Generator
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.asyncio import AsyncSession
from app.db.engine import engine, async_engine
# =========================
# SYNCHRONOUS SESSION
# =========================
SessionLocal = sessionmaker(
bind=engine,
autocommit=False,
autoflush=False,
future=True
)
def get_db() -> Generator:
"""
Yield a synchronous SQLAlchemy session
Usage: Depends(get_db)
"""
db = SessionLocal()
try:
yield db
finally:
db.close()
# =========================
# ASYNCHRONOUS SESSION
# =========================
AsyncSessionLocal = sessionmaker(
bind=async_engine,
class_=AsyncSession,
expire_on_commit=False,
autocommit=False,
autoflush=False,
future=True
)
async def get_async_db() -> AsyncGenerator:
"""
Yield an asynchronous SQLAlchemy session
Usage: Depends(get_async_db)
"""
async with AsyncSessionLocal() as session:
yield session