- Implement kordoc /parse endpoint (HWP/HWPX/PDF via kordoc lib, text files direct read, images flagged for OCR) - Add queue consumer with APScheduler (1min interval, stage chaining extract→classify→embed, stale item recovery, retry logic) - Add extract worker (kordoc HTTP call + direct text read) - Add classify worker (Qwen3.5 AI classification with think-tag stripping and robust JSON extraction from AI responses) - Add embed worker (GPU server nomic-embed-text, graceful failure) - Add DEVONthink migration script with folder mapping for 16 DBs, dry-run mode, batch commits, and idempotent file_path UNIQUE - Enhance ai/client.py with strip_thinking() and parse_json_response() Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
109 lines
3.4 KiB
Python
109 lines
3.4 KiB
Python
"""hyungi_Document_Server — FastAPI 엔트리포인트"""
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import RedirectResponse
|
|
from sqlalchemy import func, select, text
|
|
|
|
from api.auth import router as auth_router
|
|
from api.setup import router as setup_router
|
|
from core.config import settings
|
|
from core.database import async_session, engine, init_db
|
|
from models.user import User
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""앱 시작/종료 시 실행되는 lifespan 핸들러"""
|
|
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
|
from workers.queue_consumer import consume_queue
|
|
|
|
# 시작: DB 연결 확인
|
|
await init_db()
|
|
|
|
# APScheduler: 큐 소비자 1분 간격 실행
|
|
scheduler = AsyncIOScheduler()
|
|
scheduler.add_job(consume_queue, "interval", minutes=1, id="queue_consumer")
|
|
scheduler.start()
|
|
|
|
yield
|
|
|
|
# 종료: 스케줄러 → DB 순서로 정리
|
|
scheduler.shutdown(wait=False)
|
|
await engine.dispose()
|
|
|
|
|
|
app = FastAPI(
|
|
title="hyungi_Document_Server",
|
|
description="Self-hosted PKM 웹 애플리케이션 API",
|
|
version="2.0.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
# ─── 라우터 등록 ───
|
|
app.include_router(setup_router, prefix="/api/setup", tags=["setup"])
|
|
app.include_router(auth_router, prefix="/api/auth", tags=["auth"])
|
|
|
|
# TODO: Phase 2에서 추가
|
|
# app.include_router(documents.router, prefix="/api/documents", tags=["documents"])
|
|
# app.include_router(search.router, prefix="/api/search", tags=["search"])
|
|
# app.include_router(tasks.router, prefix="/api/tasks", tags=["tasks"])
|
|
# app.include_router(dashboard.router, prefix="/api/dashboard", tags=["dashboard"])
|
|
# app.include_router(export.router, prefix="/api/export", tags=["export"])
|
|
|
|
|
|
# ─── 셋업 미들웨어: 유저 0명이면 /setup으로 리다이렉트 ───
|
|
SETUP_BYPASS_PREFIXES = (
|
|
"/api/setup", "/setup", "/health", "/docs", "/openapi.json", "/redoc",
|
|
)
|
|
|
|
|
|
@app.middleware("http")
|
|
async def setup_redirect_middleware(request: Request, call_next):
|
|
path = request.url.path
|
|
# 바이패스 경로는 항상 통과
|
|
if any(path.startswith(p) for p in SETUP_BYPASS_PREFIXES):
|
|
return await call_next(request)
|
|
|
|
# 유저 존재 여부 확인
|
|
try:
|
|
async with async_session() as session:
|
|
result = await session.execute(select(func.count(User.id)))
|
|
user_count = result.scalar()
|
|
if user_count == 0:
|
|
return RedirectResponse(url="/setup")
|
|
except Exception:
|
|
pass # DB 연결 실패 시 통과 (health에서 확인 가능)
|
|
|
|
return await call_next(request)
|
|
|
|
|
|
# ─── 셋업 페이지 라우트 (API가 아닌 HTML 페이지) ───
|
|
@app.get("/setup")
|
|
async def setup_page_redirect(request: Request):
|
|
"""셋업 위자드 페이지로 포워딩"""
|
|
from api.setup import setup_page
|
|
from core.database import get_session
|
|
|
|
async for session in get_session():
|
|
return await setup_page(request, session)
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""헬스체크 — DB 연결 상태 포함"""
|
|
db_ok = False
|
|
try:
|
|
async with engine.connect() as conn:
|
|
await conn.execute(text("SELECT 1"))
|
|
db_ok = True
|
|
except Exception:
|
|
pass
|
|
|
|
return {
|
|
"status": "ok" if db_ok else "degraded",
|
|
"version": "2.0.0",
|
|
"database": "connected" if db_ok else "disconnected",
|
|
}
|