Files
web-inspector/backend/app/core/database.py
jungwoo choi 8326c84be9 feat: 3-mode inspection with tabbed UI + batch upload
- Add batch inspection backend (multipart upload, SSE streaming, MongoDB)
- Add tabbed UI (single page / site crawling / batch upload) on home and history pages
- Add batch inspection progress, result pages with 2-panel layout
- Rename "사이트 전체" to "사이트 크롤링" across codebase
- Add python-multipart dependency for file upload
- Consolidate nginx SSE location for all inspection types

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-13 19:15:27 +09:00

60 lines
1.9 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)])
# Create indexes - batch_inspections
await _db.batch_inspections.create_index("batch_inspection_id", unique=True)
await _db.batch_inspections.create_index([("name", 1), ("created_at", -1)])
await _db.batch_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