Files
SnapAndAnaly/backend/app/main.py
T
congsh 5c028d7952 Initial commit: snapAna 截图智能整理工具
包含 FastAPI 后端、React 前端、队列/OCR/标签/待办等完整功能。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-27 15:45:50 +08:00

61 lines
1.5 KiB
Python

"""FastAPI 应用入口。"""
from __future__ import annotations
import asyncio
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api import screenshots, settings_api, todos, watch
from app.core.config import settings
from app.core.db import init_db
from app.core.logger import get_logger, setup_logging
from app.services.watcher import watcher_service
from app.services.worker import worker
setup_logging(settings.debug)
logger = get_logger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI): # noqa: ARG001
"""启动时初始化 DB、启动监听器与分析 worker。"""
init_db()
loop = asyncio.get_running_loop()
async def notify() -> None:
worker.notify()
watcher_service.start(loop, notify)
await worker.start()
logger.info("snapAna 启动完成 @ http://%s:%d", settings.host, settings.port)
try:
yield
finally:
watcher_service.stop()
await worker.stop()
app = FastAPI(title="snapAna", version="0.1.0", lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(screenshots.router)
app.include_router(todos.router)
app.include_router(settings_api.router)
app.include_router(watch.router)
@app.get("/api/health")
def health() -> dict:
"""健康检查。"""
return {"status": "ok", "version": "0.1.0"}