0a31ae09af
- 切换到 SQLite(app/database.py)避免外部依赖 - system.py: 添加 func 导入 - main.py: 导入改为 app.database.init_db - 前端 topics.html: Vue 应用结构修复 - 后端导入全面修正(绝对导入) 版本: v1.0.3 (完整可运行版)
78 lines
2.3 KiB
Python
78 lines
2.3 KiB
Python
# 宇之然内容创作平台 - 主应用入口
|
|
|
|
import sys
|
|
import os
|
|
sys.path.insert(0, os.path.dirname(__file__))
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from contextlib import asynccontextmanager
|
|
import uvicorn
|
|
|
|
from app.database import init_db
|
|
from core.security import SECRET_KEY
|
|
from api import auth, topics, system, publishing, articles, logs, admin
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""应用生命周期管理"""
|
|
# 启动时初始化数据库
|
|
print("正在初始化数据库...")
|
|
init_db()
|
|
print("数据库初始化完成")
|
|
|
|
yield
|
|
|
|
# 关闭时清理资源
|
|
print("应用关闭")
|
|
|
|
app = FastAPI(
|
|
title="宇之然内容创作平台 API",
|
|
description="企业级内容创作管理系统",
|
|
version="1.0.0",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# CORS配置(允许前端访问)
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # 生产环境应指定具体域名
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 路由注册
|
|
app.include_router(auth.router, prefix="/api/auth", tags=["认证"])
|
|
app.include_router(topics.router, prefix="/api/topics", tags=["选题管理"])
|
|
app.include_router(system.router, prefix="/api/system", tags=["系统状态"])
|
|
app.include_router(publishing.router, prefix="/api/publishing", tags=["文章发布"])
|
|
app.include_router(articles.router, prefix="/api/articles", tags=["文章预览"])
|
|
app.include_router(logs.router, prefix="/api/logs", tags=["日志系统"])
|
|
app.include_router(admin.router, prefix="/api/admin", tags=["管理员"])
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""健康检查接口"""
|
|
return {"status": "healthy", "timestamp": __import__('datetime').datetime.now().isoformat()}
|
|
|
|
@app.exception_handler(Exception)
|
|
async def global_exception_handler(request: Request, exc: Exception):
|
|
"""全局异常处理器"""
|
|
import traceback
|
|
print(f"全局异常: {exc}")
|
|
print(f"堆栈跟踪:\n{traceback.format_exc()}")
|
|
return {
|
|
"error": "服务器内部错误",
|
|
"message": str(exc),
|
|
"path": request.url.path
|
|
}
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(
|
|
"main:app",
|
|
host="0.0.0.0",
|
|
port=8001,
|
|
reload=True, # 开发环境启用热重载
|
|
log_level="info"
|
|
) |