mirror of
https://github.com/stardrophere/InsightRadar.git
synced 2026-06-05 23:56:36 +08:00
big update
This commit is contained in:
+37
-3
@@ -1,12 +1,24 @@
|
||||
# app/main.py
|
||||
import logging
|
||||
import os
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 统一配置日志格式和级别,确保 delivery_service 等的 INFO 日志可见
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
# 降低 APScheduler 运行心跳日志,避免每分钟刷屏
|
||||
logging.getLogger("apscheduler").setLevel(logging.WARNING)
|
||||
|
||||
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.services.delivery_service import check_and_deliver
|
||||
from app.database import engine
|
||||
from app.models.models import Base
|
||||
|
||||
@@ -47,14 +59,24 @@ async def lifespan(app: FastAPI):
|
||||
id='ai_summary_job',
|
||||
replace_existing=True
|
||||
)
|
||||
# 推送调度:每分钟检查是否有用户需要接收邮件推送
|
||||
scheduler.add_job(
|
||||
check_and_deliver,
|
||||
'interval',
|
||||
minutes=1,
|
||||
id='delivery_check_job',
|
||||
replace_existing=True,
|
||||
)
|
||||
|
||||
scheduler.start()
|
||||
print(f"定时抓取任务已启动,每 {CRAWL_INTERVAL} 分钟执行一次")
|
||||
print(f"AI 摘要生成任务已启动,每 {SUMMARY_INTERVAL} 分钟执行一次")
|
||||
print("邮件推送调度已启动,每分钟检查一次")
|
||||
|
||||
# 为了测试方便,启动时立即执行一次
|
||||
await fetch_and_save_trending_data()
|
||||
# await fetch_and_save_trending_data()
|
||||
|
||||
await generate_unified_summaries()
|
||||
# await generate_unified_summaries()
|
||||
|
||||
yield # 此时 FastAPI 开始接受请求
|
||||
|
||||
@@ -67,7 +89,19 @@ async def lifespan(app: FastAPI):
|
||||
app = FastAPI(title="AI 新闻聚合引擎 API", lifespan=lifespan)
|
||||
|
||||
# ==========================================
|
||||
# 2. 挂载路由总线
|
||||
# 2. CORS 中间件:允许前端开发服务器跨域请求
|
||||
# ==========================================
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
# allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
|
||||
allow_origins=["*"],
|
||||
# allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# ==========================================
|
||||
# 3. 挂载路由总线
|
||||
# ==========================================
|
||||
# 版本控制
|
||||
app.include_router(api_router, prefix="/api/v1")
|
||||
|
||||
Reference in New Issue
Block a user