From ee00848fdc24acefca5c512e827558a0151b1e6f Mon Sep 17 00:00:00 2001 From: Gilles Soulier Date: Sun, 24 May 2026 04:49:41 +0200 Subject: [PATCH] feat: configuration FastAPI et moteur SQLAlchemy async --- backend/app/core/config.py | 21 +++++++++++++++++++++ backend/app/core/database.py | 15 +++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 backend/app/core/config.py create mode 100644 backend/app/core/database.py diff --git a/backend/app/core/config.py b/backend/app/core/config.py new file mode 100644 index 0000000..f7f7cf6 --- /dev/null +++ b/backend/app/core/config.py @@ -0,0 +1,21 @@ +from pydantic_settings import BaseSettings, SettingsConfigDict +from pathlib import Path + + +class Settings(BaseSettings): + model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8") + + database_url: str = "postgresql+asyncpg://homehub:homehub@localhost:5432/homehub" + upload_dir: str = "/uploads" + cors_origins: str = "http://localhost:3000" + + @property + def cors_origins_list(self) -> list[str]: + return [o.strip() for o in self.cors_origins.split(",")] + + @property + def upload_path(self) -> Path: + return Path(self.upload_dir) + + +settings = Settings() diff --git a/backend/app/core/database.py b/backend/app/core/database.py new file mode 100644 index 0000000..5cea3e5 --- /dev/null +++ b/backend/app/core/database.py @@ -0,0 +1,15 @@ +from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession +from sqlalchemy.orm import DeclarativeBase +from app.core.config import settings + +engine = create_async_engine(settings.database_url, pool_pre_ping=True) +AsyncSessionLocal = async_sessionmaker(engine, expire_on_commit=False) + + +class Base(DeclarativeBase): + pass + + +async def get_session() -> AsyncSession: + async with AsyncSessionLocal() as session: + yield session