feat(01-05): wire main.py lifespan+health and rewrite documents+topics to async session

- Rewrite main.py lifespan: MinIO client created at startup, docuvault bucket
  auto-created if missing, stored on app.state.minio; engine.dispose() on shutdown
- Extend /health endpoint: probes PostgreSQL (SELECT 1) and MinIO (bucket_exists)
  returning {"status": "ok"|"degraded", "checks": {"postgres": ..., "minio": ...}}
- Rewrite api/documents.py: all routes inject session: AsyncSession = Depends(get_db);
  save_upload/save_metadata/list_metadata/get_metadata/delete_document all async;
  upload handler queues extract_and_classify.delay() instead of inline classification;
  /classify endpoint retains synchronous await classifier.classify_document() for
  backward-compatible immediate response
- Rewrite api/topics.py: all routes inject session dependency; all storage calls
  are async with session parameter; Pydantic models TopicCreate/TopicUpdate/
  SuggestRequest preserved verbatim
This commit is contained in:
curo1305
2026-05-22 09:47:00 +02:00
parent 32d67de1ca
commit c1931fd566
3 changed files with 120 additions and 42 deletions
+23 -15
View File
@@ -1,6 +1,9 @@
from fastapi import APIRouter, HTTPException
from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from services import storage, classifier
from sqlalchemy.ext.asyncio import AsyncSession
from deps.db import get_db
from services import classifier, storage
router = APIRouter(prefix="/api/topics", tags=["topics"])
@@ -22,24 +25,29 @@ class SuggestRequest(BaseModel):
@router.get("")
async def list_topics():
topics = storage.load_topics()
counts = storage.topic_doc_counts()
async def list_topics(session: AsyncSession = Depends(get_db)):
topics = await storage.load_topics(session)
counts = await storage.topic_doc_counts(session)
for t in topics:
t["doc_count"] = counts.get(t["name"], 0)
return {"topics": topics}
@router.post("")
async def create_topic(body: TopicCreate):
topic = storage.create_topic(body.name, body.description, body.color)
async def create_topic(body: TopicCreate, session: AsyncSession = Depends(get_db)):
topic = await storage.create_topic(session, body.name, body.description, body.color)
topic["doc_count"] = 0
return topic
@router.patch("/{topic_id}")
async def update_topic(topic_id: str, body: TopicUpdate):
topic = storage.update_topic(
async def update_topic(
topic_id: str,
body: TopicUpdate,
session: AsyncSession = Depends(get_db),
):
topic = await storage.update_topic(
session,
topic_id,
name=body.name,
description=body.description,
@@ -47,26 +55,26 @@ async def update_topic(topic_id: str, body: TopicUpdate):
)
if topic is None:
raise HTTPException(404, "Topic not found")
counts = storage.topic_doc_counts()
counts = await storage.topic_doc_counts(session)
topic["doc_count"] = counts.get(topic["name"], 0)
return topic
@router.delete("/{topic_id}")
async def delete_topic(topic_id: str):
name = storage.delete_topic(topic_id)
async def delete_topic(topic_id: str, session: AsyncSession = Depends(get_db)):
name = await storage.delete_topic(session, topic_id)
if name is None:
raise HTTPException(404, "Topic not found")
return {"success": True, "removed_from_documents": True}
@router.post("/suggest")
async def suggest_topics(body: SuggestRequest):
meta = storage.get_metadata(body.document_id)
async def suggest_topics(body: SuggestRequest, session: AsyncSession = Depends(get_db)):
meta = await storage.get_metadata(session, body.document_id)
if meta is None:
raise HTTPException(404, "Document not found")
try:
suggestions = await classifier.suggest_topics_for_document(body.document_id)
suggestions = await classifier.suggest_topics_for_document(session, body.document_id)
except Exception as e:
raise HTTPException(500, f"Suggestion failed: {e}")
return {"suggested": suggestions}