版本1.0.4 - 发布前准备
- 修复system.py缩进错误 - 优化前端页面样式(待重构) - 改进API接口结构 - 完善文档和自动化脚本 - 平台基本功能稳定运行
This commit is contained in:
+28
-18
@@ -1,13 +1,13 @@
|
||||
# 宇之然内容创作平台 - 主应用入口
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
from contextlib import asynccontextmanager
|
||||
import uvicorn
|
||||
from starlette.staticfiles import StaticFiles
|
||||
|
||||
from app.database import init_db
|
||||
from core.security import SECRET_KEY
|
||||
@@ -16,14 +16,10 @@ from api import auth, topics, system, publishing, articles, logs, admin
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""应用生命周期管理"""
|
||||
# 启动时初始化数据库
|
||||
print("正在初始化数据库...")
|
||||
init_db()
|
||||
print("数据库初始化完成")
|
||||
|
||||
yield
|
||||
|
||||
# 关闭时清理资源
|
||||
print("应用关闭")
|
||||
|
||||
app = FastAPI(
|
||||
@@ -33,16 +29,16 @@ app = FastAPI(
|
||||
lifespan=lifespan
|
||||
)
|
||||
|
||||
# CORS配置(允许前端访问)
|
||||
# CORS配置
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # 生产环境应指定具体域名
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# 路由注册
|
||||
# API 路由(必须先于静态文件注册)
|
||||
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=["系统状态"])
|
||||
@@ -51,14 +47,34 @@ 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()}
|
||||
|
||||
# 独立页面路由(必须在 SPA catch-all 之前)
|
||||
@app.get("/topics.html")
|
||||
async def topics_page():
|
||||
return FileResponse("static/topics.html")
|
||||
|
||||
@app.get("/logs.html")
|
||||
async def logs_page():
|
||||
return FileResponse("static/logs.html")
|
||||
|
||||
@app.get("/users.html")
|
||||
async def users_page():
|
||||
return FileResponse("static/users.html")
|
||||
|
||||
# 静态文件(不干扰API)
|
||||
app.mount("/static", StaticFiles(directory="static"), name="static")
|
||||
|
||||
# SPA:所有非 API 路径返回 index.html(最后注册)
|
||||
@app.get("/{full_path:path}")
|
||||
async def serve_spa(full_path: str):
|
||||
return FileResponse("static/index.html")
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def global_exception_handler(request: Request, exc: Exception):
|
||||
"""全局异常处理器"""
|
||||
import traceback
|
||||
print(f"全局异常: {exc}")
|
||||
print(f"堆栈跟踪:\n{traceback.format_exc()}")
|
||||
@@ -69,10 +85,4 @@ async def global_exception_handler(request: Request, exc: Exception):
|
||||
}
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(
|
||||
"main:app",
|
||||
host="0.0.0.0",
|
||||
port=8001,
|
||||
reload=True, # 开发环境启用热重载
|
||||
log_level="info"
|
||||
)
|
||||
uvicorn.run("main:app", host="0.0.0.0", port=8001, reload=True, log_level="info")
|
||||
|
||||
Reference in New Issue
Block a user