도메인 하위 링크를 BFS로 자동 크롤링하여 페이지별 검사 수행. - BFS 링크 크롤러 (같은 도메인 필터링, max_pages/max_depth 설정) - 사이트 검사 오케스트레이션 (크롤링→순차 검사→집계) - SSE 실시간 진행 상태 (크롤링/검사/완료) - 페이지 트리 + 집계 결과 UI - UrlInputForm에 "사이트 전체 검사" 버튼 추가 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
55 lines
1.7 KiB
Python
55 lines
1.7 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 - inspections
|
|
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)])
|
|
|
|
# Create indexes - site_inspections
|
|
await _db.site_inspections.create_index("site_inspection_id", unique=True)
|
|
await _db.site_inspections.create_index([("domain", 1), ("created_at", -1)])
|
|
await _db.site_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
|