cf5103bbca
主要变更: - 数据库: SQLite → PostgreSQL (yzr_nr) - 选题系统: 硬编码字段 → 配置化 (TopicField/TopicConfigField/TopicStatusConfig) - 新增模型: ContentCalendar, ContentMetrics, MediaAsset, PlatformConfig, ContentTask - 新增 API: topic-config, calendar, metrics, assets, tasks, platform-config - 数据迁移: 现有选题数据迁移到新 schema (field_id/tags/custom_data/scoring_data) - 初始化数据: 10个领域, 5种状态, 3个平台配置 服务运行: http://localhost:8001 默认账号: admin / admin123
83 lines
2.7 KiB
Python
83 lines
2.7 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, optimizer_logs, cases, task_logs, llm_configs, system_configs, topic_config, calendar, metrics, assets, tasks, platform_config
|
|
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)
|
|
app.include_router(optimizer_logs.router)
|
|
app.include_router(cases.router)
|
|
app.include_router(task_logs.router)
|
|
app.include_router(llm_configs.router)
|
|
app.include_router(system_configs.router)
|
|
app.include_router(topic_config.router)
|
|
app.include_router(calendar.router)
|
|
app.include_router(metrics.router)
|
|
app.include_router(assets.router)
|
|
app.include_router(tasks.router)
|
|
app.include_router(platform_config.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)
|