2025-10-18 06:14:03 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2025-10-19 09:24:23 +00:00
|
|
|
from typing import Annotated, AsyncGenerator, Optional
|
2025-10-18 06:14:03 +00:00
|
|
|
|
|
|
|
|
import aiosqlite
|
|
|
|
|
from fastapi import Cookie, Depends, Request
|
|
|
|
|
from fastapi.responses import JSONResponse
|
|
|
|
|
|
|
|
|
|
import db
|
|
|
|
|
import persons
|
|
|
|
|
from common import ProblemDetails
|
|
|
|
|
from settings import settings
|
|
|
|
|
|
|
|
|
|
|
2025-10-19 02:12:28 +00:00
|
|
|
# Dependency to create SQLite connection with PRAGMAs and per-request transaction
|
2025-10-18 06:14:03 +00:00
|
|
|
async def get_db() -> AsyncGenerator[aiosqlite.Connection, None]:
|
|
|
|
|
sql_db = await db.connect(settings.database_path)
|
2025-10-19 02:12:28 +00:00
|
|
|
# Connection-level configuration
|
2025-10-18 06:14:03 +00:00
|
|
|
try:
|
2025-10-19 02:12:28 +00:00
|
|
|
# Enable FK enforcement
|
|
|
|
|
await sql_db.execute("PRAGMA foreign_keys=ON;")
|
|
|
|
|
# Prefer WAL for better concurrency; ignore result
|
|
|
|
|
async with sql_db.execute("PRAGMA journal_mode=WAL;") as _:
|
|
|
|
|
await _.fetchone()
|
|
|
|
|
# Reasonable durability/perf tradeoff
|
|
|
|
|
await sql_db.execute("PRAGMA synchronous=NORMAL;")
|
|
|
|
|
# Begin a transaction for the whole request
|
|
|
|
|
await sql_db.execute("BEGIN;")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
yield sql_db
|
|
|
|
|
await sql_db.commit()
|
|
|
|
|
except Exception:
|
|
|
|
|
await sql_db.rollback()
|
|
|
|
|
raise
|
2025-10-18 06:14:03 +00:00
|
|
|
finally:
|
|
|
|
|
await sql_db.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def cookie_person(
|
2025-10-19 13:12:16 +00:00
|
|
|
user_id: Optional[int] = Cookie(None, alias="user_id"),
|
|
|
|
|
conn: aiosqlite.Connection = Depends(get_db),
|
2025-10-18 06:14:03 +00:00
|
|
|
) -> Optional[persons.Person]:
|
2025-10-19 13:12:16 +00:00
|
|
|
if user_id is None:
|
|
|
|
|
return None
|
2025-10-18 06:14:03 +00:00
|
|
|
return await persons.get_by_id(conn, user_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def error_response(request: Optional[Request], status_code: int, message: str) -> JSONResponse:
|
|
|
|
|
body = ProblemDetails(
|
|
|
|
|
title=message,
|
|
|
|
|
status=status_code,
|
|
|
|
|
type=f"https://httpstatuses.com/{status_code}",
|
|
|
|
|
instance=str(request.url) if request else None,
|
|
|
|
|
)
|
|
|
|
|
return JSONResponse(
|
|
|
|
|
content=body.model_dump(by_alias=True),
|
|
|
|
|
status_code=status_code,
|
|
|
|
|
media_type="application/problem+json",
|
|
|
|
|
)
|