diff --git a/backend/app/__init.py__ b/backend/app/__init.py__ new file mode 100644 index 0000000..e69de29 diff --git a/backend/app/database.py b/backend/app/database.py new file mode 100644 index 0000000..294d7e7 --- /dev/null +++ b/backend/app/database.py @@ -0,0 +1,26 @@ +import os +from collections.abc import AsyncGenerator + +from dotenv import load_dotenv +from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine +from sqlmodel import SQLModel + +load_dotenv() + +DATABASE_URL = os.environ["DATABASE_URL"] + +engine = create_async_engine(DATABASE_URL, echo=False) + +_session_factory = async_sessionmaker(engine, expire_on_commit=False) + + +async def get_session() -> AsyncGenerator[AsyncSession]: + """FastAPI dependency — yields one session per request, closed on exit.""" + async with _session_factory() as session: + yield session + + +async def create_db_and_tables() -> None: + """Create every table registered in SQLModel.metadata (runs on startup).""" + async with engine.begin() as conn: + await conn.run_sync(SQLModel.metadata.create_all) diff --git a/backend/app/main.py b/backend/app/main.py new file mode 100644 index 0000000..cfd2dd0 --- /dev/null +++ b/backend/app/main.py @@ -0,0 +1,22 @@ +from collections.abc import AsyncGenerator +from contextlib import asynccontextmanager + +from fastapi import FastAPI + +from app.database import create_db_and_tables + + +# lifespan replaces the deprecated on_event("startup") pattern. +# Code before `yield` runs on startup; code after runs on shutdown. +@asynccontextmanager +async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: + await create_db_and_tables() + yield + + +app = FastAPI(lifespan=lifespan) + + +@app.get("/health") +def health() -> dict[str, str]: + return {"status": "ok"} diff --git a/backend/main.py b/backend/main.py deleted file mode 100644 index 997a4ad..0000000 --- a/backend/main.py +++ /dev/null @@ -1,6 +0,0 @@ -def main(): - print("Hello from backend!") - - -if __name__ == "__main__": - main() diff --git a/backend/pyproject.toml b/backend/pyproject.toml index f13e8dc..93e127c 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -4,7 +4,12 @@ version = "0.1.0" description = "Add your description here" readme = "README.md" requires-python = ">=3.12" -dependencies = [] +dependencies = [ + "fastapi[standard]>=0.115", + "sqlmodel>=0.0.22", + "asyncpg>=0.30", + "python-dotenv>=1.0", +] [tool.ruff] target-version = "py313" line-length = 88