- 4개 검사 엔진: HTML/CSS, 접근성(WCAG), SEO, 성능/보안 (총 50개 항목) - FastAPI 백엔드 (9개 API, SSE 실시간 진행, PDF/JSON 리포트) - Next.js 15 프론트엔드 (6개 페이지, 29개 컴포넌트, 반원 게이지 차트) - Docker Compose 배포 (Backend:8011, Frontend:3011, MongoDB:27022, Redis:6392) - 전체 테스트 32/32 PASS Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
"""
|
|
MongoDB connection management using Motor async driver.
|
|
"""
|
|
|
|
import logging
|
|
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase
|
|
from app.core.config import get_settings
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_client: AsyncIOMotorClient | None = None
|
|
_db: AsyncIOMotorDatabase | None = None
|
|
|
|
|
|
async def connect_db() -> None:
|
|
"""Establish MongoDB connection and create indexes."""
|
|
global _client, _db
|
|
settings = get_settings()
|
|
_client = AsyncIOMotorClient(settings.MONGODB_URL)
|
|
_db = _client[settings.DB_NAME]
|
|
|
|
# Create indexes
|
|
await _db.inspections.create_index("inspection_id", unique=True)
|
|
await _db.inspections.create_index([("url", 1), ("created_at", -1)])
|
|
await _db.inspections.create_index([("created_at", -1)])
|
|
|
|
# Verify connection
|
|
await _client.admin.command("ping")
|
|
logger.info("MongoDB connected successfully: %s", settings.DB_NAME)
|
|
|
|
|
|
async def close_db() -> None:
|
|
"""Close MongoDB connection."""
|
|
global _client, _db
|
|
if _client is not None:
|
|
_client.close()
|
|
_client = None
|
|
_db = None
|
|
logger.info("MongoDB connection closed")
|
|
|
|
|
|
def get_db() -> AsyncIOMotorDatabase:
|
|
"""
|
|
Get database instance.
|
|
Uses 'if db is None' pattern for pymongo>=4.9 compatibility.
|
|
"""
|
|
if _db is None:
|
|
raise RuntimeError("Database is not connected. Call connect_db() first.")
|
|
return _db
|