- 虚拟用户管理(昵称/头像/性别/简介/邮箱同步到目标平台) - AI互动调度(点赞/收藏/评论/转发) - 日志时间改为北京时间 - 评论达上限后继续执行点赞收藏转发 - 一键登出全部功能 - 浅色主题UI
66 lines
1.6 KiB
Python
66 lines
1.6 KiB
Python
"""
|
|
AI虚拟用户新闻互动系统 - 后端主入口
|
|
"""
|
|
import asyncio
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.core.config import settings
|
|
from app.core.database import init_db
|
|
from app.core.logger import logger
|
|
from app.api import router
|
|
from app.services.scheduler import scheduler_service
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""应用生命周期管理"""
|
|
logger.info("🚀 AI虚拟用户新闻互动系统启动中...")
|
|
# 初始化数据库
|
|
await init_db()
|
|
# 启动调度器
|
|
await scheduler_service.start()
|
|
logger.info("✅ 系统启动完成")
|
|
yield
|
|
# 关闭调度器
|
|
await scheduler_service.stop()
|
|
logger.info("🛑 系统已关闭")
|
|
|
|
|
|
app = FastAPI(
|
|
title="AI虚拟用户新闻互动系统",
|
|
description="基于AI驱动的虚拟用户新闻互动自动化平台",
|
|
version="1.0.0",
|
|
lifespan=lifespan,
|
|
docs_url="/api/docs",
|
|
redoc_url="/api/redoc",
|
|
)
|
|
|
|
# CORS配置
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 注册路由
|
|
app.include_router(router, prefix="/api")
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
return {"status": "ok", "service": "ai-virtual-news-backend"}
|
|
|
|
|
|
@app.exception_handler(Exception)
|
|
async def global_exception_handler(request, exc):
|
|
logger.error(f"全局异常: {exc}")
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"code": 500, "message": f"服务器内部错误: {str(exc)}"},
|
|
)
|