2bd3c83cf5
核心功能: - 新增 JWT 认证系统,支持管理员登录/登出 - 前后端合并为单一 FastAPI 应用 (端口 8001) - 系统概览页:6 个统计卡片,点击跳转筛选 - 选题管理页:批量操作 (刷新/创作/优化),时间列展示 - 系统日志页:整合日志查看功能 - 用户管理页:管理员可创建/删除用户 - 移动端适配:响应式布局,底部导航栏 - 标题居中显示 技术改进: - 添加 generated_at 字段支持创作时间记录 - 状态更新时自动同步 updated_at - 所有 API 路由添加 JWT 认证保护 - 前端 authFetch 封装自动附加 Token - 升级 FastAPI 0.136, Pydantic 2.13 等依赖 修复: - 修复 API 500 错误 (数据库列缺失) - 修复 formatRelativeTime 未定义错误 - 修复登录 Token 存储和自动附加逻辑
72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
import logging
|
|
from fastapi import FastAPI, Depends, HTTPException, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.responses import FileResponse
|
|
from sqlalchemy.orm import Session
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
from .database import engine, get_db, init_db
|
|
from .models import Base
|
|
from .api import topics, system, articles, publishing, auth, admin, audit
|
|
from .initial_data import import_initial_data
|
|
|
|
app = FastAPI(title="宇之然内容创作平台", version="0.1.0")
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 初始化数据库
|
|
Base.metadata.create_all(bind=engine)
|
|
init_db()
|
|
import_initial_data()
|
|
|
|
# 注册 API 路由
|
|
app.include_router(topics.router)
|
|
app.include_router(system.router)
|
|
app.include_router(articles.router)
|
|
app.include_router(publishing.router)
|
|
app.include_router(auth.router)
|
|
app.include_router(admin.router)
|
|
app.include_router(audit.router)
|
|
|
|
# 挂载前端
|
|
FRONTEND_DIR = Path(__file__).parent.parent.parent / "frontend"
|
|
if FRONTEND_DIR.exists() and (FRONTEND_DIR / "index.html").exists():
|
|
app.mount("/", StaticFiles(directory=str(FRONTEND_DIR), html=True), name="frontend")
|
|
logger.info(f"Frontend mounted at / from {FRONTEND_DIR}")
|
|
|
|
# SW 特殊处理
|
|
sw_path = FRONTEND_DIR / "sw.js"
|
|
if sw_path.exists():
|
|
@app.get("/sw.js")
|
|
async def service_worker():
|
|
return FileResponse(
|
|
sw_path,
|
|
media_type="application/javascript",
|
|
headers={"Cache-Control": "no-cache", "Service-Worker-Allowed": "/"}
|
|
)
|
|
|
|
# 离线页面
|
|
offline_path = FRONTEND_DIR / "offline.html"
|
|
if offline_path.exists():
|
|
@app.get("/offline.html")
|
|
async def offline_page():
|
|
return FileResponse(offline_path, media_type="text/html")
|
|
else:
|
|
@app.get("/")
|
|
def root():
|
|
return {"service": "API only", "docs": "/docs"}
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run("app.main:app", host="0.0.0.0", port=8001)
|