Files
InsightRadar/backend/app/main.py
T
2026-03-11 01:33:21 +08:00

80 lines
2.2 KiB
Python

# app/main.py
import os
from contextlib import asynccontextmanager
from fastapi import FastAPI
from dotenv import load_dotenv
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from app.services.fetcher_service import fetch_and_save_trending_data
from app.services.summary_service import generate_unified_summaries
from app.database import engine
from app.models.models import Base
# 路由总线
from app.api.router import api_router
load_dotenv()
CRAWL_INTERVAL = int(os.getenv("CRAWL_INTERVAL_MINUTES", 10))
SUMMARY_INTERVAL = int(os.getenv("SUMMARY_INTERVAL_MINUTES", 30))
scheduler = AsyncIOScheduler()
# ==========================================
# 1. 生命周期管理:App 启动时自动建表 & 启动调度器
# ==========================================
@asynccontextmanager
async def lifespan(app: FastAPI):
# 1. 数据库建表
print("正在初始化数据库表...")
Base.metadata.create_all(bind=engine)
print("数据库表初始化完成!")
# 2. 配置并启动定时任务
scheduler.add_job(
fetch_and_save_trending_data,
'interval',
minutes=CRAWL_INTERVAL,
id='trending_fetch_job',
replace_existing=True
)
# 平台摘要
scheduler.add_job(
generate_unified_summaries,
'interval',
minutes=SUMMARY_INTERVAL,
id='ai_summary_job',
replace_existing=True
)
scheduler.start()
print(f"定时抓取任务已启动,每 {CRAWL_INTERVAL} 分钟执行一次")
print(f"AI 摘要生成任务已启动,每 {SUMMARY_INTERVAL} 分钟执行一次")
# 为了测试方便,启动时立即执行一次
await fetch_and_save_trending_data()
await generate_unified_summaries()
yield # 此时 FastAPI 开始接受请求
# 优雅关闭
scheduler.shutdown()
print("定时任务已安全关闭")
# 初始化 FastAPI
app = FastAPI(title="AI 新闻聚合引擎 API", lifespan=lifespan)
# ==========================================
# 2. 挂载路由总线
# ==========================================
# 版本控制
app.include_router(api_router, prefix="/api/v1")
# 健康检查
@app.get("/", tags=["健康检查"])
async def root():
return {"message": "Welcome to AI News Aggregator API", "status": "ok"}