Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added backend/app/__init.py__
Empty file.
26 changes: 26 additions & 0 deletions backend/app/database.py
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 22 additions & 0 deletions backend/app/main.py
Original file line number Diff line number Diff line change
@@ -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"}
6 changes: 0 additions & 6 deletions backend/main.py

This file was deleted.

7 changes: 6 additions & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading